package edu.ups.gamedev.examples.one;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNServer;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;
import com.jme.scene.Node;
import edu.ups.gamedev.player.PlayerSphere;
public class ExampleServer extends Example{
public static void main(String[] args) throws Exception{
new ExampleServer();
}
public ExampleServer() throws Exception{
final ExampleGame app = new ExampleGame();
app.setDialogBehaviour(2); //2 = always display properties dialog
new Thread() {
public void run() {
app.start(); //start the application
}
}.start(); //in its own thread
//initialize networking
InetSocketAddress serverReliable = new InetSocketAddress(InetAddress.getLocalHost(), 1500);
InetSocketAddress serverFast = new InetSocketAddress(InetAddress.getLocalHost(), 1501);
JGNServer server = new JGNServer(serverReliable, serverFast); //this is our primary server and the addresses it should use
JMEGraphicalController controller = new JMEGraphicalController(); //in charge of generating and applying sync messages
SynchronizationManager syncManager = new SynchronizationManager(server, controller); //create the server that will send and receive sync messages
syncManager.addSyncObjectManager(this);
JGN.createThread(server).start(); //create a new thread for the server and start it
JGN.createThread(syncManager).start(); //create and start a thread for the synchronization manager
//get localSphere from the game
Field field = ExampleGame.class.getDeclaredField("localSphere");
field.setAccessible(true);
PlayerSphere player = null;
while((player = (PlayerSphere)field.get(app)) == null) {
try {
Thread.sleep(100);
}catch(Exception e) {
System.err.println("Error while thread sleeping to get field:");
e.printStackTrace();
}
}
//get rootNode from the game
field = ExampleGame.class.getDeclaredField("scene");
field.setAccessible(true);
Node scene = (Node)field.get(app);
setScene(scene);
syncManager.register(player, new SynchronizeCreateMessage(), 50); //register the server's sphere
}
}