Chat Tutorial in NetBeans/Server: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>SimonSmall Added TCP port (server.ini) |
imported>SimonSmall Restructured |
||
| Line 1: | Line 1: | ||
= Introduction = |
= Introduction = |
||
We need some additional files that extend the Marauroa engine to have the behaviours that we want. These are the World class and the Rule class (you can use your own names for these, and those names must be entered in the server.ini file), with some files covering configuration. You can use the values suggested here, or your own. If using your own, make sure that you enter them correctly. |
|||
This example is using the h2 database to store data. MySQL can also be used but this is not covered here. |
|||
= NetBeans Project (server) = |
= NetBeans Project (server) = |
||
We need some additional files that extend the Marauroa engine to have the behaviours that we want; these are the World class and the Rule class (you can use your own names for these, and those names must be entered in the server.ini file). |
|||
Start the NetBeans IDE. Close all open projects. |
Start the NetBeans IDE. Close all open projects. |
||
Click New Project, choose Java and Java Class Library. Click Next, then give the Project Name as server. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\server with DEVPATH being your chosen location. Click Finish. |
Click '''New Project''', choose '''Java''' and '''Java Class Library'''. Click '''Next''', then give the '''Project Name''' as '''server'''. Change the '''Project Location''' to '''DEVPATH\DevChat''' (DEVPATH, see above); the '''Project Folder''' is shown as '''DEVPATH\DevChat\server''' with DEVPATH being your chosen location. Click '''Finish'''. |
||
The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer. |
The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer. |
||
Create a new directory named '''libs''' (i.e. DEVPATH\libs) so that it can be used by the rest of the Chat code. |
|||
| ⚫ | |||
== Configuration choices == |
|||
There are a few choices to be made about the configuration of our server. They are covered below, so make sure that you are reading the correct instructions. |
|||
* This example is using the h2 database to store data. This uses a single file for storing the data, and does not need login credentials. You need to decide where the database file will be stored. MySQL can also be used but this is not covered here; you may need this if there is more to be stored in the database, or if you want a different security model. |
|||
* The server and client code can be written and linked to the Marauroa engine as a jar file or as a NetBeans project if you have the source code. This choice affects the actions you must take. |
|||
== Adding the Marauroa library == |
|||
Choose the correct way depending on if you are using the marauroa.jar file, of have created a marauroa project from the source code. |
|||
=== Using the Jar file === |
|||
Find the marauror.jar file from the download, and copy it to the '''libs''' directory (created above). Right click the '''Libraries''' branch of the server Project tree, and select '''Add Jar/Folder'''. Browse to the the '''libs''' directory for this project and select the '''marauroa.jar''' file. This will include the marauroa library with no view of the source code. |
|||
=== Using the marauroa project === |
|||
| ⚫ | |||
== World.java == |
== World.java == |
||
Right-click on the default package branch and add a new Java Class. Give the |
Right-click on the '''default package''' branch and add a new Java Class. Give the '''Class Name''' as '''World''' and '''Package''' as '''server'''. The '''default package''' will be replaced by the '''server''' package. Replace the template text with the following: |
||
<source lang="Java"> |
<source lang="Java"> |
||
/* |
/* |
||
| ⚫ | |||
* To change this template, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
*/ |
||
package server; |
package server; |
||
| Line 36: | Line 51: | ||
public class World extends RPWorld { |
public class World extends RPWorld { |
||
private static World instance; |
private static World instance; |
||
| ⚫ | |||
public static World get() { |
public static World get() { |
||
if (instance == null) { |
if (instance == null) { |
||
| Line 43: | Line 58: | ||
return instance; |
return instance; |
||
} |
} |
||
public void onInit() { |
public void onInit() { |
||
super.onInit(); |
super.onInit(); |
||
| Line 54: | Line 69: | ||
== Rule.java == |
== Rule.java == |
||
Right-click on the |
Right-click on the '''server''' package and add a new Java Class. Give the '''Class Name''' as '''Rule'''. Replace the template text with the following: |
||
<source lang="Java"> |
<source lang="Java"> |
||
/* |
/* |
||
* |
|||
* To change this template, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
*/ |
||
package server; |
package server; |
||
| Line 80: | Line 94: | ||
public class Rule implements IRPRuleProcessor { |
public class Rule implements IRPRuleProcessor { |
||
private static Rule instance; |
private static Rule instance; |
||
private World world = World.get(); |
private World world = World.get(); |
||
| Line 91: | Line 105: | ||
return instance; |
return instance; |
||
} |
} |
||
@Override |
@Override |
||
public void setContext(RPServerManager rpman) { |
public void setContext(RPServerManager rpman) { |
||
manager = rpman; |
manager = rpman; |
||
} |
} |
||
@Override |
@Override |
||
public boolean checkGameVersion(String game, String version) { |
public boolean checkGameVersion(String game, String version) { |
||
return game.equals("Chat"); |
return game.equals("Chat"); |
||
} |
} |
||
@Override |
@Override |
||
public synchronized void onTimeout(RPObject object) { |
public synchronized void onTimeout(RPObject object) { |
||
onExit(object); |
onExit(object); |
||
} |
} |
||
@Override |
@Override |
||
public synchronized boolean onExit(RPObject object) { |
public synchronized boolean onExit(RPObject object) { |
||
| Line 112: | Line 126: | ||
return true; |
return true; |
||
} |
} |
||
@Override |
@Override |
||
public synchronized boolean onInit(RPObject object) { |
public synchronized boolean onInit(RPObject object) { |
||
| Line 119: | Line 133: | ||
return true; |
return true; |
||
} |
} |
||
@Override |
@Override |
||
public synchronized void beginTurn() { |
public synchronized void beginTurn() { |
||
} |
} |
||
@Override |
@Override |
||
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) { |
public boolean onActionAdd(RPObject caster, RPAction action, List<RPAction> actionList) { |
||
return true; |
return true; |
||
} |
} |
||
@Override |
@Override |
||
public synchronized void endTurn() { |
public synchronized void endTurn() { |
||
} |
} |
||
@Override |
@Override |
||
public void execute(RPObject caster, RPAction action) { |
public void execute(RPObject caster, RPAction action) { |
||
| Line 163: | Line 177: | ||
} |
} |
||
} |
} |
||
@Override |
@Override |
||
public CharacterResult createCharacter(String username, String character, RPObject template) { |
public CharacterResult createCharacter(String username, String character, RPObject template) { |
||
| Line 191: | Line 205: | ||
== Building == |
== Building == |
||
If there are no errors in the package, select Run, |
If there are no errors in the package, select '''Run''', '''Build Project''' from the NetBeans menu bar. This will create '''server.jar''' in the '''dist''' directory. That file is required for the deployment. |
||
= Server Configuration = |
|||
= NetBeans Project (tools) = |
|||
When the server is started it will read a set of configuration values from the configuration file '''server.ini'''. The first thing we will create are the encryption keys. |
|||
In the NetBeans IDE, click New Project, choose Java and Java Application. Click Next, then give the Project Name as tools. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\tools with DEVPATH being your chosen location. Check that Create Main Class is ticked, and the class is tools.GenerateKeys before clicking Finish. |
|||
| ⚫ | |||
The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer. |
|||
This program will generate the keys required. Choose the correct way depending on if you are using the marauroa.jar file, or have created a marauroa project from the source code. |
|||
=== Using the Jar file === |
|||
| ⚫ | |||
Without the source code access to this program is limited using NetBeans. The easiest way is to run the program using a terminal window. Open a command / terminal window in the DEVPATH directory. Type the following lines (for a Windows system): |
|||
<pre> |
|||
java -classpath libs\marauroa.jar marauroa.tools.GenerateKeys |
|||
</pre> |
|||
For a UNIX / Linux system, use / instead of \ after the libs. |
|||
As the program runs in the window, accept the suggested value of 512 (i.e. just press Return). This creates the keys and writes them to the screen. Copy the lines into the server.ini file (see below). |
|||
=== Using the marauroa project === |
|||
Expand the Marauroa source file you have downloaded. Navigate to the src\marauroa\tools directory and open the GenerateKeys.java file there. Copy the contents of that into the NetBeans GenerateKeys.java file so that everything is replaced. Locate the package line, and change this from package marauroa.tools; to package tools; |
|||
In NetBeans, open the marauroa project, and expand the '''Source Packages''' branch. At the bottom of the list, expand the '''marauroa.tools''' branch. Right-click on '''GenerateKeys.java''' file and select '''Run File'''. Click in the '''Output''' window, and accept the suggested value of 512 (i.e. just press Return). You get: |
|||
<pre> |
<pre> |
||
run: |
run: |
||
# How long should the key be? [512]: |
# How long should the key be? [512]: |
||
# Using key of 512 bits. |
# Using key of 512 bits. |
||
| Line 222: | Line 246: | ||
</pre> |
</pre> |
||
where there are two long lines of numbers (shown above as 12345...). You need to copy those three number lines, and the comment line above them, for the server.ini file (see below). |
where there are two long lines of numbers (shown above as 12345...). You need to copy those three number lines, and the comment line above them, for the '''server.ini''' file (see below). |
||
== server.ini == |
== server.ini == |
||
In the server Project tree, right click on |
In the server Project tree, right click on '''Source Packages''', '''server''' branch and select '''New''', '''Other''', '''Other''', '''Empty file'''. Give the name as '''server.ini''' and click '''Finish'''. A new empty file named server.ini is created. |
||
Copy the following code into the server.ini window (note that the code below includes the Keys lines; don't copy them). |
Copy the following code into the server.ini window (note that the code below includes the Keys lines; don't copy them). Paste the four lines from the output of GenerateKeys into this file. |
||
<pre> |
<pre> |
||
| Line 265: | Line 289: | ||
This is using the h2 database. Configuration of others is possible. |
This is using the h2 database. Configuration of others is possible. |
||
The line starting jdbc_url=jdbc:h2:Chat gives the location and name of the database. This creates a database named Chat in the current (i.e. server) directory. See here [http://www.h2database.com/html/features.html#database_url] for the technical details. |
The line starting '''jdbc_url=jdbc:h2:Chat''' gives the location and name of the database. This creates a database named Chat in the current (i.e. server) directory. See here [http://www.h2database.com/html/features.html#database_url] for the technical details. |
||
=== TCP Port === |
=== TCP Port === |
||
| Line 273: | Line 297: | ||
=== Game classes === |
=== Game classes === |
||
The lines world= and ruleprocessor= specify the classes to be used for these features. If you chose different class names above, use those names (and package) here. |
The lines '''world=''' and '''ruleprocessor=''' specify the classes to be used for these features. If you chose different class names above, use those names (and package) here. |
||
=== Game Information === |
=== Game Information === |
||
| Line 283: | Line 307: | ||
Although you don't have to provide a Log configuration file, you can if you wish. |
Although you don't have to provide a Log configuration file, you can if you wish. |
||
Create another empty file, as you did with the server.ini file, with a name of log4j.properties and copy the following code into it (NetBeans remembers the file types you created, so Empty will be on the first selection screen) |
Create another empty file, as you did with the '''server.ini''' file, with a name of '''log4j.properties''' and copy the following code into it (NetBeans remembers the file types you created, so Empty will be on the first selection screen) |
||
<pre> |
<pre> |
||
| Line 305: | Line 329: | ||
= Completion = |
= Completion = |
||
You have created the two class files and compiled them into a java library, and created the server and logfile configuration files. The next step is to create the client files then follow the deployment instructions for preparing and testing your game. |
|||
See the Deployment instructions for which files you will need for your game. |
|||