Chat Tutorial in NetBeans/Text Client: Difference between revisions

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]]