CmdrZin
15may09
rev: 23may09

Plan 2

Plan 2: Compile my own Client to print out messages in raw form
NOTE: One BIG item missing in all this is a BUILD discussion or example. One with Maven would be nice or straight java commands, or NetBeans even.

From Library area on main web site

Project Darkstar Hands-On Lab  link to instructions and download  Developers  May 2008
This Hands-on Lab introduces programming game servers using Project Darkstar from Sun Labs. It steps attendees through the construction of a simple textual Multi-User Dungeon (MUD)
Author(s): Jeff Kesselman, Dan Templeton
Keywords: JavaOne 2008, how-to, tutorial
Downloaded lab to Windows machine and opened the index.html file.
Plan 2 Changed to working through the JavaOne 7400 Lab and build a text based MUD.

Exercise 0: Info

Exercise 1: Follow steps 1: 1-4 for NetBeans 6.5
To resolve import errors, had to add to Libraries
    sgs-client.jar
    sgs-server-api-0.9.9.jar
also added
    sgs-client-0.9.9-sources.jar
to be able to look at source code.

Many errors is source code
(see fixes doc)

Ex1: Step 2 read Lesson and Tutorials again.
Replace pieces of code in SimpleServer.java with code from
    ..\solutions\exercise1\com\sun\sgs\darkmud\SimpleServer.java
Convert byte[] to ByteBuffer to send massages.
Add sendMessage also.
Build successful..woo hoo.
Try to run
    java.lang.NoClassDefFoundError: com/sun/sgs/impl/kernel/Kernel
hmm..errors
Try adding sgs-server-0.9.9.jar to Libraries
Better, errors..don't have database??..try clean build from new NetBeans project.

ref1: Server API Programming Guide.pdf "Project Darkstar Server Application Tutorial 03/18/09"
NetBeans 6.5
File: New Project
    1. Choose Project: Java: Java Class Library
    2. Name and Location: myDarkMUD
        Project Locaton: C:\DATA\GameDevelopment\myDarkMUD
        Project Folder: C:\DATA\GameDevelopment\myDarkMUD\myDarkMUD
ref1 pg9:  "... API classes are in com.sun.sgs.app.* package." Nice, but which *.jar???
Found in sgs-serverapi-0.9.9.jar. So, RMC Libraries->Add Jar/Folder... and browse to where Darkstar server lib is and add sgs-serverapi-0.9.9.jar to Libraries. Used Relative Path.
Next, RMC Source Pakages->New->Java Package to add a folder named com.sun.sgs.darkmud so the packages will match up with the source code from the 7400 DarkMud example.
Next, copy SimpleServer.java file from 7400 Darkmud into the package. hmm..no errors..good sign.
Build MyDarkMUD project..SUCCESSFUL..so far so good.
Results in
    C:\DATA\GameDevelopment\myDarkMUD\myDarkMUD\dist\myDarkMUD.jar
file
Copy this file into sgs-server-dist-0.9.9\deploy folder just like for the tutorial.
Go to tutorial\conf\ and copy and edit:
    HelloWorld.boot to myDarkMUD.boot
        Change SGS_PROPERTIES to use myDarkMUD.properties file.
    HelloWorld.properties to myDarkMUD.properties
        Change .app.name to use myDarkMUD instead of HelloWorld
        Change .app.root to use myDarkMUD instead of HelloWorld
            This seems to set up the database automatically.
        Change .app.listener to use myDarkMUD instead of HelloWorld
opps..Refactor SimpleServer.java to myDarkMUD.java so that the *.jar filename matches the class...rebuild..then copy into deploy folder to replace first jar file.
ref1 pg 14: Open a command line window and run server using
    java -jar bin/sgs-boot.jar tutorial/conf/myDarkMUD.boot
from the server distribution directory..mine is at
    C:\DATA\GameDevelopment\DarkStarGameEngine\DSG_0_9_9\sgs-server-dist-0.9.9\sgs-server-dist-0.9.9
can't find myDarkMUD class..did make a db folder though.
oops..change .app.listener to com.sun.sgs.darkmud.myDarkMUD..still no class
hmm..copy in package-info.java file and rebuild..copy jar file into deploy..error..no class
hmm..recall reading sgs-boot.properties file comments on SGS_DEPLOY tag...it's set to  =${SGS_HOME}/tutorial
hmm..copy myDarkMUD.jar into tutorial directory where tutorial.jar is..AH hA..seems to work.
ref plan 1: client launch
Bring up simple client with another Command window and cd to the client tutorial and use
    java -cp tutorial-client.jar;../lib/sgs-client.jar com.sun.sgs.tutorial.client.lesson1.HelloUserClient
Woo Hoo ..client logs in!!!..but nothing else happens..BUT, we can compile and run..
Edit code to output text..no output..error on com.sun.sgs.impl.service....hmm..no .impl type classes in Library
Add sgs-server-0.9.9.jar to Libraries..rebuild..still errors when client connects.
Try using most of HelloEchoSessionListener code from ref1 pg 36-37
hmm..didn't like the private static final long serialVersionUID = 1L; entry..comment out...delete db folder..helped..but Kernel problem
Had to rewrite the code to use referenced objects for the session..
Basically replaced SimpleListener method with

    public SimpleListener(ClientSession session) {
        if (session == null) {
            throw new NullPointerException("null session");
        }
        sessionRef = AppContext.getDataManager().createReference(session);
        sessionName = session.getName();
    }
to use referenced for session
then a simple
    public void receivedMessage(ByteBuffer message) {
        ClientSession session = getSession();
        session.send(message);
    }
was used to echo back anything the user sent.
Plan 2 Exercise 1 part 1: successful...can compile and run server using NetBeans and command line. And use SimpleClient.
Now to fix JavaOne client..it logs in, but error when receiving messages or trying to send.

Load up 7400_Darkstar project. It sets the client as the main class. So just compile and run project.
Logged in at pop-up..no server messages..error message is

May 20, 2009 8:33:14 PM com.sun.sgs.client.simple.SimpleClient$SimpleClientConnectionListener handleSessionMessage
WARNING: SimpleClientListener.receivedMessage callback throws
java.nio.ReadOnlyBufferException
        at java.nio.ByteBuffer.array(ByteBuffer.java:942)
        at com.sun.sgs.darkmud.client.MudClient.receivedMessage(MudClient.java:238)
so probably ByteBuffer / byte[ ] problem.
try fixing MudClient.java with
    public void receivedMessage(ByteBuffer m) {
//        byte[] message = m.array();
//        String msg = new String(message);
        String msg = new String(m.array());
        appendOutput(msg, Flag.OUTPUT);
    }
build and run..same error..maybe the class has changed..go read API docs on SimpleClientListener
hmm..receivedMessage inherited from interface com.sun.sgs.client.ServerSessionListener..looks ok..but read-only exeption??
Try copying message to new buffer..ah ha..so fixed it with
    public void receivedMessage(ByteBuffer m) {
        byte[] message = new byte[m.capacity()];
        m.get(message);
        String msg = new String(message);
        appendOutput(msg, Flag.OUTPUT);
    }
Client now recieves server message and echo backs..Plan 2 Exercise 1: done..both DarkMUD server and DarkMUD client up
An IDEA..should be able to run the server out of the 7400_Darkstar.jar file by setting the boot properties..
Porting code from myDarkMUD.java into SimpleServer.java in 7400 project.
Making 7400_Darkstar.boot and .properties files..copy and edit myDarkMUD files
Simple replace except for
    com.sun.sgs.app.listener=com.sun.sgs.darkmud.SimpleServer
which should be the entry class.
Now use
    java -jar bin/sgs-boot.jar tutorial/conf/7400_Darkstar.boot
to start server...looks good..and run client from NetBeans 7400 project..Woo Hoo..logs in..and echos.
Can now use NetBean for all dev work..just copy jar file to server area and run from command line, the run client from NetBeans.
myDarkMUD project no longer needed.