Chat Tutorial in NetBeans/Text Client: Difference between revisions

From Arianne
Jump to navigation Jump to search
Content deleted Content added
imported>SimonSmall
Build the Project: Added log4j.jar library
imported>SimonSmall
Extra logging info
Line 1: Line 1:
= Introduction =
= Introduction =


These instructions are a continuation of the Chat Tutorial. This is a text based client that runs in a command / terminal window.
From the Design Objectives given in the Chat Tutorial, this is a text based client that runs in a command / terminal window. It is executed on the Server and establishes communications to the Server program, with no user interaction. It includes some error logging, comments and Javadoc output.

For more detail in using the NetBeans IDE, see the [[Using and configuring NetBeans]] page.


= NetBeans Project =
= NetBeans Project =
Line 14: Line 16:


=== Using the Jar file ===
=== Using the Jar file ===

Use this option unless you need to use the other methods.


Find the '''marauroa.jar''' file from the download, and copy it to the libs directory (created above) if it is not there already. 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.
Find the '''marauroa.jar''' file from the download, and copy it to the libs directory (created above) if it is not there already. 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.
Line 21: Line 25:
Right click the '''Libraries''' branch of the server Project tree, and select '''Add Project'''. Make sure you browse to the correct file location and select the '''marauroa''' project that you created earlier. This will include your marauroa library that you built, and you can see the source code.
Right click the '''Libraries''' branch of the server Project tree, and select '''Add Project'''. Make sure you browse to the correct file location and select the '''marauroa''' project that you created earlier. This will include your marauroa library that you built, and you can see the source code.


== Client Files ==
= Client Files =


The following files need to be created, as they are not present in the marauroa source file.
The following files need to be created, as they are not present in the marauroa source file.
Line 43: Line 47:
import marauroa.client.net.IPerceptionListener;
import marauroa.client.net.IPerceptionListener;
import marauroa.client.net.PerceptionHandler;
import marauroa.client.net.PerceptionHandler;
import marauroa.common.Log4J;
import marauroa.common.game.RPAction;
import marauroa.common.game.RPAction;
import marauroa.common.game.RPObject;
import marauroa.common.game.RPObject;
Line 57: Line 62:
private String[] available_characters;
private String[] available_characters;
private List<String> quotes = new ArrayList<String>();
private List<String> quotes = new ArrayList<String>();
private static marauroa.common.Logger logger = Log4J.getLogger(Client.class);

public static Client get() {
public static Client get() {
if (client == null) {
if (client == null) {
Line 64: Line 70:
return client;
return client;
}
}

protected Client() {
protected Client() {
super("log4j.properties");
super("client/log4j_client.properties");
world_objects = new HashMap<RPObject.ID, RPObject>();
world_objects = new HashMap<RPObject.ID, RPObject>();
handler = new PerceptionHandler(new PerceptionListener());
handler = new PerceptionHandler(new PerceptionListener());
}
}

public String[] GetAvailableCharacters() {
public String[] GetAvailableCharacters() {
return available_characters;
return available_characters;
}
}

String popQuote() {
String popQuote() {
if (quotes.isEmpty()) {
if (quotes.isEmpty()) {
Line 83: Line 89:
return result;
return result;
}
}

public void SendMessage(String text) {
public void SendMessage(String text) {
RPAction action;
RPAction action;
Line 91: Line 97:
send(action);
send(action);
}
}

@Override
@Override
protected void onPerception(MessageS2CPerception message) {
protected void onPerception(MessageS2CPerception message) {
Line 98: Line 104:
} catch (java.lang.Exception e) {
} catch (java.lang.Exception e) {
// Something weird happened while applying perception
// Something weird happened while applying perception
logger.warn("Applying perception: " + e);
}
}
}
}
Line 255: Line 262:
Close and Save the file.
Close and Save the file.


== Build the Project ==
== Adding Logging to the Client ==


To trace errors, or to monitor what parts of a program are executed, message statements can be inserted in the code. The log4j library provides a flexible and configurable way to do this. See [[Logging in Marauroa how this is done]] in Marauroa.
If there are no errors in the package, there is one last thing to do. Although this does not create a compile error it will create an error when you run the Text Client.

=== The logging configuration file ===

To specify the logging output right-click on the '''client''' package and add a new '''Empty File'''. Give the '''File Name''' as '''log4j_client.properties'''. Add the following lines to that file:

<pre>
# Set root logger level to INFO, and create two output configurations
log4j.rootLogger=INFO, Console, File

# Paste all log entries to the console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

# Paste all log entries to a daily log file
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=log/client.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

# Disabled debug messages... to avoid too many logs

</pre>

Close and Save the file.

=== Adding the log4j library ===

Although this does not create a compile error it will create an error when you run the Text Client.


Copy the '''log4j.jar''' file from the downloaded source files 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 log4j.jar file.
Copy the '''log4j.jar''' file from the downloaded source files 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 log4j.jar file.

== Build the Project ==


Finally, select '''Run''', '''Build Main Project''' from the NetBeans menu bar. This will create '''client.jar''' in the '''dist''' directory, and copy the jar files from the libs directory to the '''lib''' sub-directory of dist.
Finally, select '''Run''', '''Build Main Project''' from the NetBeans menu bar. This will create '''client.jar''' in the '''dist''' directory, and copy the jar files from the libs directory to the '''lib''' sub-directory of dist.
Line 265: Line 303:
== Completion ==
== Completion ==


You have created the two class files and compiled them into a java library. Now you can follow the deployment instructions for preparing and testing your game.
You have created the two class files and compiled them into a java library. Now you can follow the [[Chat Tutorial in NetBeans/Deploy Text Client|deployment instructions]] for preparing and testing your game.


[[Category:NetBeans]]
[[Category:NetBeans]]

Revision as of 08:37, 8 April 2012

Introduction

From the Design Objectives given in the Chat Tutorial, this is a text based client that runs in a command / terminal window. It is executed on the Server and establishes communications to the Server program, with no user interaction. It includes some error logging, comments and Javadoc output.

For more detail in using the NetBeans IDE, see the Using and configuring NetBeans page.

NetBeans Project

In the NetBeans IDE, close all open projects. Click New Project, choose Java and Java Application. Click Next, then give the Project Name as client. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\client with DEVPATH being your chosen location. Check that Create Main Class is ticked, and the Main Class is client.Test 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.

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

Use this option unless you need to use the other methods.

Find the marauroa.jar file from the download, and copy it to the libs directory (created above) if it is not there already. 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

Right click the Libraries branch of the server Project tree, and select Add Project. Make sure you browse to the correct file location and select the marauroa project that you created earlier. This will include your marauroa library that you built, and you can see the source code.

Client Files

The following files need to be created, as they are not present in the marauroa source file.

Client.java

Right-click on the client package and add a new Java Class. Give the Class Name as Client. Replace the template code with the following code:

<source lang="Java"> /*

*
*/

package client;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import marauroa.client.ClientFramework; import marauroa.client.net.IPerceptionListener; import marauroa.client.net.PerceptionHandler; import marauroa.common.Log4J; import marauroa.common.game.RPAction; import marauroa.common.game.RPObject; import marauroa.common.net.message.MessageS2CPerception; import marauroa.common.net.message.TransferContent;

/**

*
*/

public class Client extends ClientFramework {

 private PerceptionHandler handler;
 protected static Client client;
 private Map<RPObject.ID, RPObject> world_objects;
 private String[] available_characters;
 private List<String> quotes = new ArrayList<String>();
  private static marauroa.common.Logger logger = Log4J.getLogger(Client.class);
 public static Client get() {
   if (client == null) {
     client = new Client();
   }
   return client;
 }
 protected Client() {
   super("client/log4j_client.properties");
   world_objects = new HashMap<RPObject.ID, RPObject>();
   handler = new PerceptionHandler(new PerceptionListener());
 }
 public String[] GetAvailableCharacters() {
   return available_characters;
 }
 String popQuote() {
   if (quotes.isEmpty()) {
     return null;
   }
   String result = quotes.get(0);
   quotes.remove(0);
   return result;
 }
 public void SendMessage(String text) {
   RPAction action;
   action = new RPAction();
   action.put("type", "chat");
   action.put("text", text);
   send(action);
 }
 @Override
 protected void onPerception(MessageS2CPerception message) {
   try {
     handler.apply(message, world_objects);
   } catch (java.lang.Exception e) {
     // Something weird happened while applying perception
           logger.warn("Applying perception: " + e);
   }
 }
 @Override
 protected List<TransferContent> onTransferREQ(List<TransferContent> items) {
   return items;
 }
 @Override
 protected void onTransfer(List<TransferContent> items) {
 }
 @Override
 protected void onAvailableCharacters(String[] characters) {
   available_characters = characters;
 }
 @Override
 protected void onServerInfo(String[] info) {
   for (String s : info) {
     quotes.add(s);
   }
 }
 @Override
 protected String getGameName() {
   return "Chat";
 }
 @Override
 protected String getVersionNumber() {
   return "0.1";
 }
 @Override
 protected void onPreviousLogins(List<String> previousLogins) {
 }
 class PerceptionListener implements IPerceptionListener {
   @Override
   public boolean onAdded(RPObject object) {
     if (object.has("text")) {
       quotes.add("*" + object.get("from") + "* : " + object.get("text"));
     }
     return false;
   }
   @Override
   public boolean onModifiedAdded(RPObject object, RPObject changes) {
     return false;
   }
   @Override
   public boolean onModifiedDeleted(RPObject object, RPObject changes) {
     return false;
   }
   @Override
   public boolean onDeleted(RPObject object) {
     return false;
   }
   @Override
   public boolean onMyRPObject(RPObject added, RPObject deleted) {
     return false;
   }
   @Override
   public void onSynced() {
   }
   @Override
   public void onUnsynced() {
   }
   @Override
   public void onException(Exception e, marauroa.common.net.message.MessageS2CPerception perception) {
     e.printStackTrace();
     System.exit(-1);
   }
   @Override
   public boolean onClear() {
     return false;
   }
   public void onPerceptionBegin(byte type, int timestamp) {
   }
   public void onPerceptionEnd(byte type, int timestamp) {
   }
 }

} </source>

Close and Save the file.

Test.java

This file was created when you created the new Project. Add the following (or replace the existing) code:

<source lang="Java"> /*

*
*/

package client;

import marauroa.common.game.RPObject;

/**

*
*/

public class Test {

 @SuppressWarnings("SleepWhileInLoop")
 public static void main(String[] args) {
   boolean cond = true;
   Client my = Client.get();
   try {
     my.connect("localhost", 5555);
     if (args.length == 3) {
       my.createAccount(args[0], args[1], args[2]);
     }
     my.login(args[0], args[1]);
     if (my.GetAvailableCharacters().length == 0) {
       RPObject character = new RPObject();
       my.createCharacter(args[0], character);
     }
     my.chooseCharacter(args[0]);
   } catch (Exception e) {
     cond = false;
   }
   int i = 0;
   while (cond) {
     ++i;
     my.loop(0);
     if (i % 100 == 50) {
       my.SendMessage("test" + i);
     }
     String s = my.popQuote();
     while (s != null) {
       System.out.println(s);
       s = my.popQuote();
     }
     try {
       Thread.sleep(100);
     } catch (InterruptedException e) {
       cond = false;
     }
   }
 }

} </source>

Close and Save the file.

Adding Logging to the Client

To trace errors, or to monitor what parts of a program are executed, message statements can be inserted in the code. The log4j library provides a flexible and configurable way to do this. See Logging in Marauroa how this is done in Marauroa.

The logging configuration file

To specify the logging output right-click on the client package and add a new Empty File. Give the File Name as log4j_client.properties. Add the following lines to that file:

# Set root logger level to INFO, and create two output configurations
log4j.rootLogger=INFO, Console, File

# Paste all log entries to the console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

# Paste all log entries to a daily log file
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=log/client.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

# Disabled debug messages... to avoid too many logs

Close and Save the file.

Adding the log4j library

Although this does not create a compile error it will create an error when you run the Text Client.

Copy the log4j.jar file from the downloaded source files 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 log4j.jar file.

Build the Project

Finally, select Run, Build Main Project from the NetBeans menu bar. This will create client.jar in the dist directory, and copy the jar files from the libs directory to the lib sub-directory of dist.

Completion

You have created the two class files and compiled them into a java library. Now you can follow the deployment instructions for preparing and testing your game.