Captive Imagination
September 10, 2010, 04:54:13 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Check out the teaser site for the first official game by Captive Imagination: http://www.galaxiesbeyond.com
 
   Home   Help Search Calendar Login Register  
Pages: [1] 2  All   Go Down
  Print  
Author Topic: How to connect / disconnect Clients (incl. JME Objects) correctly?? [SOLVED]  (Read 3338 times)
0 Members and 1 Guest are viewing this topic.
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« on: March 17, 2008, 08:59:35 AM »

Hey Guys..
Started my BachelorThesis last week, i'm quite new to JGN!
But the first Huh is rising.

Basicly i would implement an application, where multiple clients can join, can see each other and can disconnect.
Every client should be able to control a synchronized spatial in an underlying JME App. (a floor and some boxes)
It's quite the same approach as the FlagRush tutorial is... and the same problem.

I'm able to connect multiple clients, each one is able to control his spatial. (e.g. 4 clients in scene)
BUT: If i disconnect any client (e.g.#2) and then let a new client connect, the "old"(1,3,4) clients can't see his spatial moving.. It seems to be frozen in the initial position. The client himself is able to move, and sees all other spatials moving.
Same problem as it is in the FlagRush Tutorial.. (the "frozen" spatial/bike is invisible to the old clients)

Another "prob" in my app and in FlagRush is, that disconnected playerObjects remain for about 15 sec. before being removed from the scenegraph ->  I read that this is solved by "linking" the UDP/TCP connections. setConnectionLinking(boolean)

Has anybody some experience in adding / removing players in a synchronized application.
I'll upload my test soon.. ..write some comments and clean it up Wink

« Last Edit: March 25, 2008, 04:09:57 AM by .:emp...Imm0|82:. » Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #1 on: March 17, 2008, 09:57:21 AM »

Here the Code:

SimpleApp
Code:
package connectionTest;

import com.jme.app.SimpleGame;
import com.jme.input.FirstPersonHandler;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;

public class SimpleApp extends SimpleGame{

private Box box1,box2,box3,box4;

private Sphere camSphere = null;
private Node rootNode = null;
boolean appRunning = false;

public SimpleApp(){}

@Override
protected void simpleInitGame() {
appRunning = true;
rootNode = super.rootNode;

camSphere = new Sphere("camSphere", Vector3f.ZERO, 5,5,5);;

Box box1 = new Box("box1", Vector3f.ZERO, 5,5,5);
Box box2 = new Box("box2", Vector3f.ZERO, 5,5,5);
Box box3 = new Box("box3", Vector3f.ZERO, 5,5,5);
Box box4 = new Box("box4", Vector3f.ZERO, 5,5,5);
Box floor = new Box("floor", Vector3f.ZERO, 50,1,50);

// Sphere camSphere = new Sphere("camSphere", Vector3f.ZERO, 5,5,5);

box1.setLocalTranslation(20, 0, 0);
box2.setLocalTranslation(-20, 0, 0);
box3.setLocalTranslation(0, 0, 20);
box4.setLocalTranslation(0, 0, -20);

MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
ms.setDiffuse(ColorRGBA.randomColor());
box1.setRenderState(ms);
box2.setRenderState(ms);
box3.setRenderState(ms);
box4.setRenderState(ms);

rootNode.attachChild(box1);
rootNode.attachChild(box2);
rootNode.attachChild(box3);
rootNode.attachChild(box4);
rootNode.attachChild(floor);

input.removeAllFromAttachedHandlers();
input = new FirstPersonHandler(cam);
input.getFromAttachedHandlers(1).setActionSpeed(400);
}



@Override
protected void simpleUpdate() {
super.simpleUpdate();
camSphere.setLocalTranslation(cam.getLocation());

}

@Override
protected void quit() {
super.quit();
appRunning = false;
System.out.println("quit");
}



@Override
public void finish() {
super.finish();
appRunning = false;
System.out.println("finish");


}


public static void main(String[] args){
SimpleApp app = new SimpleApp();
app.start();


}


public Sphere getCamSphere() {
return camSphere;
}


public void setCamSphere(Sphere camSphere) {
this.camSphere = camSphere;
}

public boolean isAppRunning() {
return appRunning;
}

public void setAppRunning(boolean appRunning) {
this.appRunning = appRunning;
}

}


TestServer
Code:
package connectionTest;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Vector;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNConnection;
import com.captiveimagination.jgn.clientserver.JGNConnectionListener;
import com.captiveimagination.jgn.clientserver.JGNServer;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;

public class ConnectionTestServer {

Thread gameThread;
final SimpleApp gameApp;

private JGNServer server;
private InetSocketAddress fastServerAddress;
private InetSocketAddress reliableServerAddress;

private JGNConnectionListener clientConnectionListener;
private MessageListener messageListener;

private SynchronizationManager syncManager;
private TestSyncObjectManager syncObjectManager;

private JMEGraphicalController graficalController;
private Sphere sphere;
private Node scene;


public ConnectionTestServer()
{

gameApp = new SimpleApp();

gameThread = new Thread()
{
@Override
public void run()
{
gameApp.start();
}

};

gameThread.start();

setUpCommunication();

}



private void setUpCommunication()
{
// Setup communication
graficalController = new JMEGraphicalController();

try {
fastServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2000);
reliableServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1000);
server = new JGNServer(reliableServerAddress, fastServerAddress);
server.setConnectionLinking(true);

syncManager = new SynchronizationManager(server, graficalController);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

syncObjectManager = new TestSyncObjectManager();
syncManager.addSyncObjectManager(syncObjectManager);

clientConnectionListener = new JGNConnectionListener(){

@Override
public void connected(JGNConnection arg0) {
System.out.println("SERVER: +++++");
System.out.println("SERVER: CliConListener - client found \n "+"ClientID: "+arg0.getPlayerId());

try {
syncManager.update();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("SERVER: +++++");
}

@Override
public void disconnected(JGNConnection arg0) {

System.out.println("SERVER: -----");
System.out.println("SERVER: CliConListener - client lost \n"+"ClientID: "+arg0.getPlayerId());
try {
syncManager.update();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("SERVER: -----");
}

};
messageListener = new MessageListener(){

@Override
public void messageCertified(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageFailed(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageReceived(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageSent(Message message) {
// TODO Auto-generated method stub

}

};

server.addClientConnectionListener(clientConnectionListener);
server.addMessageListener(messageListener);

JGN.createThread(server).start();
    JGN.createThread(syncManager).start();
   
    //Get the spatial and the scene from the gameApp
    try {
    Field field = null;
    field = SimpleApp.class.getDeclaredField("camSphere");
field.setAccessible(true);

sphere = null;
scene = null;

while ((sphere = (Sphere)field.get(gameApp)) == null) {
    try {
    System.out.println("SERVER-THREAD: WAIT FOR GAMEAPP APPLYING SPHEREFIELD");
        Thread.sleep(100);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

field = SimpleApp.class.getDeclaredField("rootNode");
field.setAccessible(true);
scene = (Node) field.get(gameApp);

syncObjectManager.setScene(scene);

//register the spatial
syncManager.register(sphere, new SynchronizeCreateMessage(), 50);

    } catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while (gameApp.isAppRunning()) {
try {
Thread.sleep(25);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}



/**
* @param args
*/
public static void main(String[] args)
{

ConnectionTestServer myConTest = new ConnectionTestServer();

}
}

TestClient
Code:
package connectionTest;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.clientserver.JGNConnection;
import com.captiveimagination.jgn.clientserver.JGNConnectionListener;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;

public class ConnectionTestClient {

final Thread gameThread;
final SimpleApp gameApp;

private JGNClient client;
private InetSocketAddress reliableClientAddress;
private InetSocketAddress fastClientAddress;

private InetSocketAddress fastServerAddress;
private InetSocketAddress reliableServerAddress;

private JGNConnectionListener serverConnectionListener;
private MessageListener messageListener;

private SynchronizationManager syncManager;
private TestSyncObjectManager syncObjectManager;

private JMEGraphicalController graficalController;
private Sphere sphere = null;
private Node scene;

private short clientID;






public ConnectionTestClient(){

gameApp = new SimpleApp();

gameThread = new Thread()
{
@Override
public void run()
{
gameApp.start();
}

};

gameThread.start();
setUpCommunication();
}

// setup communication
private void setUpCommunication()
{


graficalController = new JMEGraphicalController();

try {
fastServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2000);
reliableServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1000);

reliableClientAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
fastClientAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);

client = new JGNClient(reliableClientAddress, fastClientAddress);

syncManager = new SynchronizationManager(client, graficalController);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

syncObjectManager = new TestSyncObjectManager();
syncManager.addSyncObjectManager(syncObjectManager);

serverConnectionListener = new JGNConnectionListener(){

@Override
public void connected(JGNConnection arg0) {
System.out.println("\nCLIENT: +++++++");
System.out.println("CLIENT: connected as ID: "+client.getPlayerId());
System.out.println("CLIENT: +++++++\n");
}

@Override
public void disconnected(JGNConnection arg0) {
System.out.println("\nCLIENT: ------");
System.out.println("CLIENT: disconnecting.... ID was: "+arg0.getPlayerId());

}

};
messageListener = new MessageListener(){

@Override
public void messageCertified(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageFailed(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageReceived(Message message) {
// TODO Auto-generated method stub

}

@Override
public void messageSent(Message message) {
// TODO Auto-generated method stub

}

};

client.addServerConnectionListener(serverConnectionListener);
client.addMessageListener(messageListener);

JGN.createThread(client).start();
    JGN.createThread(syncManager).start();
   
    //Get the spatial and the scene from the gameApp
    try {
    Field field = null;
    field = SimpleApp.class.getDeclaredField("camSphere");
field.setAccessible(true);

sphere = null;
scene = null;

while ((sphere = (Sphere)field.get(gameApp)) == null) {
    try {
    System.out.println("CLIENT-THREAD: WAIT FOR GAMEAPP APPLYING sphereFIELD");
        Thread.sleep(100);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

field = SimpleApp.class.getDeclaredField("rootNode");
field.setAccessible(true);
scene = (Node) field.get(gameApp);

try {
client.connectAndWait(reliableServerAddress, fastServerAddress, 1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
syncObjectManager.setScene(scene);
syncManager.register(sphere, new SynchronizeCreateMessage(), 50);



    } catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while(gameApp.isAppRunning()){
try {
Thread.sleep(25);
} catch (Exception e) {
// TODO: handle exception
}
}
try {
client.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}





/**
 * @param args
 */
public static void main(String[] args)
{

ConnectionTestClient myConTest = new ConnectionTestClient();

}

}


TestSyncObjManager
Code:
package connectionTest;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jgn.synchronization.message.SynchronizeRemoveMessage;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;

public class TestSyncObjectManager implements SyncObjectManager {

private Node scene;
private Sphere mySphere;
private MaterialState ms;

@Override
public Object create(SynchronizeCreateMessage scm) {

Future<Sphere> future = GameTaskQueueManager.getManager().update(new Callable<Sphere> () {

@Override
public Sphere call() throws Exception {
mySphere = new Sphere("myCamSphere", Vector3f.ZERO, 5,5,5);
scene.attachChild(mySphere);
                scene.updateGeometricState(0, true);
                mySphere.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
                scene.updateRenderState();
return mySphere;
}

});

try {
            return future.get();
        } catch(Exception exc) {
            exc.printStackTrace();
        }
       
return null;
}

@Override
public boolean remove(SynchronizeRemoveMessage srm, Object obj) {

Sphere s = null;
if(obj instanceof Sphere){
s = (Sphere)obj;
}else{
System.out.println("TestSyncObjManager: clID" +srm.getDestinationPlayerId()+ " häääää??");
return false;
}

return s.removeFromParent();
}

public Node getScene() {
return scene;
}

public void setScene(Node scene) {
this.scene = scene;
}

// public Sphere create(SynchronizeCreateMessage synchronizeCreateMessage,
// short playerId) {
// ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
// switch (playerId) {
// case 0:{
// ms.setAmbient(ColorRGBA.red);
// ms.setDiffuse(ColorRGBA.red);
// ms.setSpecular(ColorRGBA.red);
// ms.setEnabled(true);
// break;
// }
// case 1:{
// ms.setAmbient(ColorRGBA.blue);
// ms.setDiffuse(ColorRGBA.blue);
// ms.setSpecular(ColorRGBA.blue);
// ms.setEnabled(true);
// break;
// }
// case 2:{
// ms.setAmbient(ColorRGBA.green);
// ms.setDiffuse(ColorRGBA.green);
// ms.setSpecular(ColorRGBA.green);
// ms.setEnabled(true);
// break;
// }
//
// default:
// break;
// }
//
//
// Future<Sphere> future = GameTaskQueueManager.getManager().update(new Callable<Sphere> () {
//
// @Override
// public Sphere call() throws Exception {
// mySphere = new Sphere("myCamSphere", Vector3f.ZERO, 5,5,5);
// mySphere.setRenderState(ms);
// scene.attachChild(mySphere);
//                 scene.updateGeometricState(0, true);
//                 mySphere.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
//                 scene.updateRenderState();
// return mySphere;
// }
//
// });
//
//
// try {
//            return future.get();
//        } catch(Exception exc) {
//            exc.printStackTrace();
//        }
//       
// return null;
// }

}


I would like to fix this prob, that new client's spatials are frozen if other clients have disconnected before.
And what do i have to care about, if i would a "clean" disconnection?
Do i have to null the MessageListeners and ConnectionListeners?
..there will surely come up some more questions...

thanks
greetz
Imm0
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #2 on: March 17, 2008, 09:59:35 AM »

Hmmm...I'll have to get a look at this.  If you find some time to investigate this in the mean-time please feel free to submit a patch and I can get it applied.  It's very likely the synchronization system is not properly handling the disconnection of the client.
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #3 on: March 17, 2008, 10:34:42 AM »

ok, i'll have to trace this down Wink
I guess its good practice to get into JGN...

Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #4 on: March 18, 2008, 05:02:06 AM »

well, no success yet Embarrassed
It seems, that if a new client connects to a server and gets a clientID, which was in use before, the syncObjectManager won't update it's sphere..or wont create a new one, or it isn't updated on all other syncManagers or..or..or
I dont get it  Huh

And if after this reuse of a clientID a new client connects on an unused ID, he doesn't get any Information about objects in the scene. He is invisible to all others, and all others are invisible to him...
« Last Edit: March 18, 2008, 05:17:24 AM by .:emp...Imm0|82:. » Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #5 on: March 18, 2008, 07:44:13 AM »

Hmmm...have you verified that all ties to the originally used clientId have been removed?  It could be that something isn't disconnecting properly and causing this to occur.  When a disconnect happens does the original vehicle disappear from all machines?
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #6 on: March 18, 2008, 08:34:36 AM »

no, i haven't verfied that, yet. I'll take a look onto it after this post.
It IS definately that something isn't disconnecting properly, causing this.

When a disconnect happens, the original bike disapears from all machines (after 10sec, beacuse setConnectionLinking(boolean) is not set) e.g. bike2
When a new client connects (new bike2), he sees all other bikes, but isn't seen by anyone else.
If bike(nextNewUserID) connects, everything is fine for it.

I'll report again, later...
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #7 on: March 18, 2008, 10:41:09 AM »

Feel free to modify the code to set connection linking.  After you fix this you can submit a patch. Wink

This is an odd little issue...if I get some time tonight I'll see if I can help you track it down.

Thanks for all your help.
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #8 on: March 19, 2008, 07:31:09 AM »

i just dont get it..
i'm too stupid debuging this Embarrassed
Trying more then 10h+ to get a solution, without any result.

I guess i have to remove the syncObjectManager and/or the syncSpatial from the serverSyncManager and the clientSyncManager before the client finally disconnects. Do u think this could depent on the JMEGraphicalController as well?
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #9 on: March 19, 2008, 07:39:52 AM »

Sorry, I didn't get a chance last night to look at it.  Perhaps tonight.

You shouldn't have to do that. I bet it has to do with something hanging on after disconnection. Go ahead and continue with development and ignore this if you can and I'll make sure it gets fixed. Thanks for the effort you've put into it.
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #10 on: March 19, 2008, 08:38:29 AM »

thx,
but i wanna do this, to satisfy my ambition  Wink
As i said, im new to JGN, and have to write my Thesis about porting a singlePlayerApp to a multiPlayerApp..
This is my 2nd week now in this company, and the 5th day playing around with JGN.

and i made an step forward. it seems that u have to remove,disconnect and update everything properly, before u disconnect a client...
*continuing*

*updated Code*
« Last Edit: March 19, 2008, 12:51:12 PM by .:emp...Imm0|82:. » Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #11 on: March 19, 2008, 12:57:32 PM »

Here the Code:

SimpleApp
Code:
package connectionTest;

import com.jme.app.SimpleGame;
import com.jme.input.FirstPersonHandler;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;

public class SimpleApp extends SimpleGame{

private Box box1,box2,box3,box4;

private Sphere camSphere = null;
private Node rootNode = null;
boolean appRunning = false;

public SimpleApp(){}

@Override
protected void simpleInitGame() {
appRunning = true;
rootNode = super.rootNode;

camSphere = new Sphere("camSphere", Vector3f.ZERO, 5,5,5);

Box box1 = new Box("box1", Vector3f.ZERO, 5,5,5);
Box box2 = new Box("box2", Vector3f.ZERO, 5,5,5);
Box box3 = new Box("box3", Vector3f.ZERO, 5,5,5);
Box box4 = new Box("box4", Vector3f.ZERO, 5,5,5);
Box floor = new Box("floor", Vector3f.ZERO, 50,1,50);

box1.setLocalTranslation(20, 0, 0);
box2.setLocalTranslation(-20, 0, 0);
box3.setLocalTranslation(0, 0, 20);
box4.setLocalTranslation(0, 0, -20);

MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
ms.setDiffuse(ColorRGBA.randomColor());
box1.setRenderState(ms);
box2.setRenderState(ms);
box3.setRenderState(ms);
box4.setRenderState(ms);

rootNode.attachChild(box1);
rootNode.attachChild(box2);
rootNode.attachChild(box3);
rootNode.attachChild(box4);
rootNode.attachChild(floor);

input.removeAllFromAttachedHandlers();
input = new FirstPersonHandler(cam);
input.getFromAttachedHandlers(1).setActionSpeed(400);
}



@Override
protected void simpleUpdate() {
super.simpleUpdate();
camSphere.setLocalTranslation(cam.getLocation());

}

@Override
protected void quit() {
super.quit();
appRunning = false;
System.out.println("quit");
}



@Override
public void finish() {
super.finish();
appRunning = false;
System.out.println("finish");
}


public static void main(String[] args){
SimpleApp app = new SimpleApp();
app.start();
}


public Sphere getCamSphere() {
return camSphere;
}


public void setCamSphere(Sphere camSphere) {
this.camSphere = camSphere;
}

public boolean isAppRunning() {
return appRunning;
}

public void setAppRunning(boolean appRunning) {
this.appRunning = appRunning;
}

public Node getScene() {
// TODO Auto-generated method stub
return rootNode;
}
}


TestServer
Code:
package MessageTest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import MessageTest.messages.TestPlayerConnectMessage;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.TCPMessageServer;
import com.captiveimagination.jgn.UDPMessageServer;
import com.captiveimagination.jgn.clientserver.JGNConnection;
import com.captiveimagination.jgn.clientserver.JGNConnectionListener;
import com.captiveimagination.jgn.clientserver.JGNServer;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;

import connectionTest.SimpleApp;
import connectionTest.TestSyncObjectManager;

public class TestServer2 {

private JGNServer server;
private Thread serverThread;
private SimpleApp gameApp;
private Thread gameAppThread;

private InetSocketAddress reliableServerAddress;
private InetSocketAddress fastSercerAddress;

private TCPMessageServer tcpMessageServer;
private UDPMessageServer udpMessageServer;

private JGNConnectionListener clientConnectionListener;
private MessageListener messageListener;

private JMEGraphicalController jmeGraphController;
private SynchronizationManager syncManager;
private Thread syncManagerThread;
private TestSyncObjectManager syncObjManager;

private boolean gameAppIsAlive = false;
private boolean appIsAlive = false;
private boolean serverThredIsAlive = false;
private boolean SyncManagerThreadIsAlive = false;
private boolean aClientHasDisconnected = false;


public TestServer2(){

gameApp = new SimpleApp();

gameAppThread = new Thread(){
@Override
public void run()
{
gameApp.start();
}
};
gameAppThread.start();

while(!gameApp.isAppRunning()){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
gameAppIsAlive = gameApp.isAppRunning();
System.out.println("SERVER: GAMEAPP STARTED" + gameAppIsAlive);

setUpCommunication();
setUpSynchronization();

appIsAlive = true;
System.out.println("SERVER: CONNECTION AND SYNCHRONIZATION SUCCESSFULLY INITIATED - " + appIsAlive);

while(appIsAlive){

if(aClientHasDisconnected){
JGNConnection[] cons = server.getConnections();
System.out.println("SERVER: overall connected Clients: "+cons.length);
for(int i=0; i<cons.length; i++){
JGNConnection con = cons[i];
if(!con.isConnected()){
System.out.println("SERVER: ID - "+con.getPlayerId());
System.out.println("is still connected: "+con.isConnected());
}
}
aClientHasDisconnected = false;
}
try {
// System.out.println("SERVER: running");
Thread.sleep(25);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

System.out.println("SERVER: Ende");
}


private void setUpSynchronization() {

jmeGraphController = new JMEGraphicalController(){};
syncManager = new SynchronizationManager(server,jmeGraphController);
syncObjManager = new TestSyncObjectManager();
syncManager.addSyncObjectManager(syncObjManager);
syncManagerThread = JGN.createThread(syncManager);
syncManagerThread.start();
if(syncManager.isAlive()){
SyncManagerThreadIsAlive = true;
System.out.println("SERVER: SyncManagerThreadIsAlive - "+SyncManagerThreadIsAlive);
}

syncObjManager.setScene(gameApp.getScene());

try {
syncManager.register(gameApp.getCamSphere(), new SynchronizeCreateMessage(), 50);
System.out.println("SERVER: SyncManager - Sphere successfully registered");
syncManager.update();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void setUpCommunication() {

try {
reliableServerAddress = new InetSocketAddress(InetAddress.getLocalHost(),1000);
fastSercerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2000);
tcpMessageServer = new TCPMessageServer(reliableServerAddress);
udpMessageServer = new UDPMessageServer(fastSercerAddress);
}catch (IOException e) {
// TODO: handle exception
}

server = new JGNServer(tcpMessageServer, udpMessageServer);
server.setConnectionLinking(true);

clientConnectionListener = new JGNConnectionListener(){

@Override
public void connected(JGNConnection connection) {
System.out.println("\n*****");
System.out.println("SERVER: clConListener - connected");
System.out.println("SERVER: Client has ID: "+connection.getPlayerId());
System.out.println("*****\n");
}

@Override
public void disconnected(JGNConnection connection) {
System.out.println("\n*****");
System.out.println("SERVER: clConListener - disconnected");
aClientHasDisconnected = true;
System.out.println("Server: Client ID was: "+connection.getPlayerId());
System.out.println("*****\n");
}
};

messageListener = new MessageListener(){
@Override
public void messageCertified(Message message) {
// System.out.println("\n*****");
// System.out.println("SERVER: msgListener - certified");
// System.out.println("*****\n");
}

@Override
public void messageFailed(Message message) {
// System.out.println("\n*****");
// System.out.println("SERVER: msgListener - failed");
// System.out.println("*****\n");
}

@Override
public void messageReceived(Message message) {
// System.out.println("\n*****");
// System.out.println("SERVER: msgListener - recievced");
// if(message instanceof PlayerStatusMessage)
// System.out.println("Server: STATUS MESSAGE RECIEVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// System.out.println("*****\n");
}

@Override
public void messageSent(Message message) {
// System.out.println("\n*****");
// System.out.println("SERVER: msgListener - send");
// System.out.println("*****\n");
}
};

server.addClientConnectionListener(clientConnectionListener);
server.addMessageListener(messageListener);

serverThread = JGN.createThread(server);
serverThread.start();
if(server.isAlive()){
serverThredIsAlive = true;
System.out.println("SERVER: serverThredIsAlive - "+serverThredIsAlive);
}
}

/**
* @param args
*/
public static void main(String[] args) {
TestServer2 app = new TestServer2();
}

}
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #12 on: March 19, 2008, 12:58:10 PM »

TestClient
Code:
package MessageTest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.TCPMessageServer;
import com.captiveimagination.jgn.UDPMessageServer;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.clientserver.JGNConnection;
import com.captiveimagination.jgn.clientserver.JGNConnectionListener;
import com.captiveimagination.jgn.convert.ConversionException;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jgn.synchronization.message.SynchronizeRemoveMessage;
import com.captiveimagination.jmenet.JMEGraphicalController;
import connectionTest.SimpleApp;
import connectionTest.TestSyncObjectManager;


public class TestClient2 {

final Thread gameAppThread;
final SimpleApp gameApp;

private JGNClient client;
private Thread clientThread;
private InetSocketAddress reliableClientAddress;
private InetSocketAddress fastClientAddress;

private InetSocketAddress reliableServerAddress;
private InetSocketAddress fastServerAddress;
private TCPMessageServer tcpMessageServer;
private UDPMessageServer udpMessageServer;

private JGNConnectionListener serverConnectionListener;
private MessageListener messageListener;

private JMEGraphicalController jmeGraphController;
private SynchronizationManager syncManager;
private Thread syncManagerThread;
private TestSyncObjectManager syncObjManager;

private short clientID;

private boolean gameAppIsAlive = false;
private boolean appIsAlive = false;
private boolean clientThredIsAlive = false;
private boolean syncManagerThreadIsAlive = false;

public TestClient2(){

gameApp = new SimpleApp();
gameAppThread = new Thread(){
@Override
public void run()
{
gameApp.start();
}
};

gameAppThread.start();

while(!gameApp.isAppRunning()){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

gameAppIsAlive = gameApp.isAppRunning();
System.out.println("CLIENT: GAMEAPP STARTED" + gameAppIsAlive);

setUpCommunication();
setUpSynchronization();
connectAndSynchronize();

appIsAlive = true;
System.out.println("CLIENT: CONNECTION AND SYNCHRONIZATION SUCCESSFULLY INITIATED - " + appIsAlive);

while(gameApp.isAppRunning()){
// System.out.println("CLIENT: GameAPP - running");
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// syncManager.unregister(gameApp.getCamSphere());
// syncObjManager.remove(new SynchronizeRemoveMessage(), gameApp.getCamSphere());
syncManager.removeSyncObjectManager(syncObjManager);
// try {
// syncManager.update();
// } catch (Exception e) {
// // TODO: handle exception
// }
syncObjManager = null;

try{
if ((syncManagerThreadIsAlive=syncManager.isAlive())) {
syncManager.shutdown();
System.out.println("CLIENT: SyncManager successfully shutDown");
}
if((clientThredIsAlive=client.isAlive())){
client.close();
System.out.println("CLIENT: Client successfully closed");

}
}
catch(NullPointerException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


System.out.println("CLIENT: Here reached Ende??");
}


private void connectAndSynchronize() {

try {
// syncObjManager.setMySphere(gameApp.getCamSphere());
client.connectAndWait(reliableServerAddress, fastServerAddress, 3000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
syncManager.register(gameApp.getCamSphere(), new SynchronizeCreateMessage(), 50);
System.out.println("CLIENT: SyncManager - Sphere successfully registered");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void setUpSynchronization() {

jmeGraphController = new JMEGraphicalController(){};
syncManager = new SynchronizationManager(client,jmeGraphController);
syncObjManager = new TestSyncObjectManager();
syncManager.addSyncObjectManager(syncObjManager);
syncManagerThread = JGN.createThread(syncManager);
syncManagerThread.start();
if(syncManager.isAlive()){
syncManagerThreadIsAlive = true;
System.out.println("CLIENT: SyncManagerThreadIsAlive - "+syncManagerThreadIsAlive);
}

syncObjManager.setScene(gameApp.getScene());

}

private void setUpCommunication() {

try {
reliableClientAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
fastClientAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);

reliableServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1000);
fastServerAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2000);

tcpMessageServer = new TCPMessageServer(reliableClientAddress);
udpMessageServer = new UDPMessageServer(fastClientAddress);

client = new JGNClient(tcpMessageServer, udpMessageServer);

} catch (IOException e) {
// TODO: handle exception
}

serverConnectionListener = new JGNConnectionListener(){
@Override
public void connected(JGNConnection connection) {
System.out.println("\n*****");
clientID = client.getPlayerId();
System.out.println("CLIENT: srvConListener - connected as ID: "+clientID);
System.out.println("*****\n");
}

@Override
public void disconnected(JGNConnection connection) {
System.out.println("\n*****");
System.out.println("CLIENT: srvConListener - disconnected ID: "+client.getPlayerId());
System.out.println("*****\n");
if(gameApp.isAppRunning()){
System.out.println("CLIENT: SERVER shot me down / SERVER quit");
}
}
};

messageListener = new MessageListener(){

@Override
public void messageCertified(Message message) {
// System.out.println("\n*****");
// System.out.println("CLIENT: msgListener - certified");
// System.out.println("*****\n");
}

@Override
public void messageFailed(Message message) {
// System.out.println("\n*****");
// System.out.println("CLIENT: msgListener - failed");
// System.out.println("*****\n");
}

@Override
public void messageReceived(Message message) {
// System.out.println("\n*****");
// System.out.println("CLIENT: msgListener - recievced");
if(message instanceof SynchronizeRemoveMessage){
SynchronizeRemoveMessage srm = (SynchronizeRemoveMessage)message;
System.out.println("CLIENT: RECIEVED SynchronizeRemoveMessage from "+message.getPlayerId()+" to remove SyncObject "+srm.getSyncObjectId());
}
// System.out.println("*****\n");
}

@Override
public void messageSent(Message message) {
// System.out.println("\n*****");
// System.out.println("CLIENT: msgListener - send");
// System.out.println("*****\n");
}
};

client.addServerConnectionListener(serverConnectionListener);
client.addMessageListener(messageListener);

clientThread = JGN.createThread(client);
clientThread.start();

if(client.isAlive()){
clientThredIsAlive = true;
System.out.println("CLIENT: clientThredIsAlive - "+clientThredIsAlive);
}
}


/**
* @param args
*/
public static void main(String[] args) {

TestClient2 app = new TestClient2();

}

}


TestSyncObjManager
Code:
package connectionTest;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import com.captiveimagination.jgn.MessageClient;
import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jgn.synchronization.message.SynchronizeRemoveMessage;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;

public class TestSyncObjectManager implements SyncObjectManager {

private Node scene;
private Sphere mySphere;
private MaterialState ms;

public TestSyncObjectManager(){
scene = null;
mySphere = null;
ms = null;
}

@Override
public Object create(SynchronizeCreateMessage scm) {

System.out.println("TEST-SYNC-OBJECT-MANAGER: create() was Called by ID: "+scm.getPlayerId());
System.out.println("TEST-SYNC-OBJECT-MANAGER: SyncObject to create: "+scm.getSyncObjectId());


mySphere = new Sphere("myCamSphere", Vector3f.ZERO, 5,5,5);
scene.attachChild(mySphere);
        scene.updateGeometricState(0, true);
        mySphere.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        scene.updateRenderState();

        return mySphere;

}

@Override
public boolean remove(SynchronizeRemoveMessage srm, Object obj) {

System.out.println("TEST-SYNC-OBJECT-MANAGER: remove() was Called by ID: "+srm.getPlayerId());
System.out.println("TEST-SYNC-OBJECT-MANAGER: SyncObject to remove: "+srm.getSyncObjectId());

// mySphere.removeFromParent();
return ((Spatial)obj).removeFromParent();
}

public Node getScene() {
return scene;
}

public void setScene(Node scene) {
this.scene = scene;
}

public Sphere getMySphere() {
return mySphere;
}

public void setMySphere(Sphere mySphere) {
this.mySphere = mySphere;
}

// public Sphere create(SynchronizeCreateMessage synchronizeCreateMessage,
// short playerId) {
// ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
// switch (playerId) {
// case 0:{
// ms.setAmbient(ColorRGBA.red);
// ms.setDiffuse(ColorRGBA.red);
// ms.setSpecular(ColorRGBA.red);
// ms.setEnabled(true);
// break;
// }
// case 1:{
// ms.setAmbient(ColorRGBA.blue);
// ms.setDiffuse(ColorRGBA.blue);
// ms.setSpecular(ColorRGBA.blue);
// ms.setEnabled(true);
// break;
// }
// case 2:{
// ms.setAmbient(ColorRGBA.green);
// ms.setDiffuse(ColorRGBA.green);
// ms.setSpecular(ColorRGBA.green);
// ms.setEnabled(true);
// break;
// }
//
// default:
// break;
// }
//
//
// Future<Sphere> future = GameTaskQueueManager.getManager().update(new Callable<Sphere> () {
//
// @Override
// public Sphere call() throws Exception {
// mySphere = new Sphere("myCamSphere", Vector3f.ZERO, 5,5,5);
// mySphere.setRenderState(ms);
// scene.attachChild(mySphere);
//                 scene.updateGeometricState(0, true);
//                 mySphere.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
//                 scene.updateRenderState();
// return mySphere;
// }
//
// });
//
//
// try {
//            return future.get();
//        } catch(Exception exc) {
//            exc.printStackTrace();
//        }
//       
// return null;
// }

}


*updated 19.March.2008


For me now it seems that its an issue of the syncronizationManager, and its communication betewen the server's SyncManager and the client's SyncManagers.. *...will take a closer look tomorrow again*
« Last Edit: March 19, 2008, 01:03:58 PM by .:emp...Imm0|82:. » Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #13 on: March 19, 2008, 02:45:45 PM »

You might try working directly with the FlagRush networking example so I can be more involved in the process instead of having to look directly at your new code and try to understand it.  Further, it reduces the complexity of the issue for me and when we get it resolved I can update the FlagRush example. Smiley
Logged
.:emp...Imm0|82:.
Jr. Member
**
Offline Offline

Posts: 61


View Profile
« Reply #14 on: March 20, 2008, 03:47:49 AM »

aight
but to get an understanding of how it works, i like to reproduce everything Wink
but if it is solved, i will try to addapt it to the flagrushTut.
Logged
Pages: [1] 2  All   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.358 seconds with 20 queries.