Some questions about Spider Monkey

Hi everyone

I am trying to master Spider Monkey

after reading testchatclient/server code,a bit of monkeyzone, this and this

I understood somewhat and managed to make the connection + send some text/signal

I am now trying to make a test (making 2 cubes and move one of them according to signal from client)

I pressed left and the cubed moved on both machine, at second time the server side crash and I get this error

(it might crash at first time or may be at fifth time, the errors are almost the same)

Sep 13, 2011 10:19:13 AM com.jme3.network.serializing.Serializer registerClass

INFO: Registered class[-51]:class network.messages.

Sep 13, 2011 10:19:13 AM com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_VertexColor is not declared in shader.

Sep 13, 2011 10:19:14 AM com.jme3.network.base.DefaultServer registerClient

INFO: Client registered:Connection[ id=0, reliable=NioEndpoint[1, java.nio.channels.SocketChannel[connected local=/192.168.1.66:5001 remote=/192.168.1.67:35134]], fast=UdpEndpoint[1, /192.168.1.67:47600] ].

Sep 13, 2011 10:19:14 AM com.jme3.network.base.KernelAdapter reportError

SEVERE: Unhandled error, endpoint:NioEndpoint[1, java.nio.channels.SocketChannel[connected local=/192.168.1.66:5001 remote=/192.168.1.67:35134]], context:network.messages@198a455

java.lang.NullPointerException

at network.ServerListener.messageReceived(ServerListener.java:19)

at network.ServerListener.messageReceived(ServerListener.java:1)

at com.jme3.network.base.MessageListenerRegistry.messageReceived(MessageListenerRegistry.java:72)

at com.jme3.network.base.DefaultServer.dispatch(DefaultServer.java:221)

at com.jme3.network.base.DefaultServer$Redispatch.messageReceived(DefaultServer.java:472)

at com.jme3.network.base.DefaultServer$Redispatch.messageReceived(DefaultServer.java:468)

at com.jme3.network.base.KernelAdapter.dispatch(KernelAdapter.java:163)

at com.jme3.network.base.KernelAdapter.createAndDispatch(KernelAdapter.java:217)

at com.jme3.network.base.KernelAdapter.run(KernelAdapter.java:260)

null

(0.0, 0.0, 0.0)

client left pressed

(-1.0, 0.0, 0.0) <


works until this point
client left pressed <---- I think it crash here
(-1.0, 0.0, 0.0)
Sep 13, 2011 10:19:19 AM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.IllegalStateException: Scene graph is not properly updated for rendering.
Make sure scene graph state was not changed after
rootNode.updateGeometricState() call.
Problem spatial name: Root Node
at com.jme3.scene.Spatial.checkCulling(Spatial.java:241)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:775)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1116)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1162)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:264)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:218)
at java.lang.Thread.run(Thread.java:662)

I dont understand the error of the bold part. how can I solve it ?
and here are my codes
MyServer
ServerListener
MyClient
ClientListener
MyMessage
MyServer
[java]package network;

import java.io.IOException;

import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.ConnectionListener;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.network.serializing.Serializer;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class MyServer extends SimpleApplication{
Server server;
Client client;
ConnectionListener listener ;
protected static Geometry serverblue;
protected static Geometry serverred;
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
serverblue= new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
serverblue.setMaterial(mat);
rootNode.attachChild(serverblue);


serverred= new Geometry("Box", b);
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
serverred.setMaterial(mat2);
rootNode.attachChild(serverred);

serverblue.setLocalTranslation(-5, 0, 0);
serverred.setLocalTranslation(5, 0, 0);

//network code
try {
server = Network.createServer(5001,5001);
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//probe code
initKey();

Serializer.registerClass(MyMessages.class);
server.addMessageListener(new ServerListener(), MyMessages.class);

}
private void initKey() {
inputManager.addMapping("L", new KeyTrigger(keyInput.KEY_LEFT));
inputManager.addMapping("R", new KeyTrigger(keyInput.KEY_RIGHT));

inputManager.addListener(new ActionListener() {
@Override
public void onAction(String name, boolean keyPressed, float fpf) {
if (name.equals("L")&&keyPressed) {

}
if (name.equals("R")&&keyPressed) {

}
}
}, new String []{"L","R"});
}
@Override
public void simpleUpdate(float tpf) {
// System.out.println(server.getConnections());
}
public static void main(String[] args) { MyServer app=new MyServer(); app.setShowSettings(false); app.start();}

}//end of class

[/java]
ServerListener
[java]package network;


import com.jme3.network.HostedConnection;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;

public class ServerListener extends MyServer implements MessageListener<HostedConnection>{

@Override
public void messageReceived(HostedConnection source, Message m) {
// messages helloMessage = (messages)m;
// System.out.println(helloMessage.hello);
// helloMessage.hello = "Hi!";
// source.send(helloMessage);
MyMessages helloMessage = (MyMessages)m;

System.out.println(helloMessage.hello);
System.out.println(helloMessage.moveVector);
if (helloMessage.hello.equals("client left pressed")) {
helloMessage.hello="client will be moved to LEFT, done";
messagehandler();
// source.send(helloMessage);
}
if (helloMessage.hello.equals("client right pressed")) {
helloMessage.hello="client will be moved to RIGHT, done";
// red.setLocalTranslation(red.getLocalTranslation().add(helloMessage.moveVector));
// source.send(helloMessage);
}

}
public void messagehandler(){
serverred.setLocalTranslation(serverred.getLocalTranslation().add(-1,0,0));
}
}//end of class

[/java]
MyClient
[java]
package network;

import java.io.IOException;

import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.Network;
import com.jme3.network.serializing.Serializer;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class MyClient extends SimpleApplication{
Client client;
protected static Geometry clientred;
protected static Geometry clientblue;
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
clientred= new Geometry("Box", b);
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
clientred.setMaterial(mat2);
rootNode.attachChild(clientred);

clientblue= new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
clientblue.setMaterial(mat);
rootNode.attachChild(clientblue);

clientblue.setLocalTranslation(-5, 0, 0);
clientred.setLocalTranslation(5, 0, 0);

//network code
try {
client = Network.connectToServer("192.168.1.66", 5001);
client.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//probe code
initKey();

Serializer.registerClass(MyMessages.class);
client.addMessageListener(new ClientListener(),MyMessages.class);
client.send(new MyMessages());




}//end of init
private void initKey() {
inputManager.addMapping("L", new KeyTrigger(keyInput.KEY_LEFT));
inputManager.addMapping("R", new KeyTrigger(keyInput.KEY_RIGHT));

inputManager.addListener(new ActionListener() {
@Override
public void onAction(String name, boolean keyPressed, float fpf) {
if (name.equals("L")&&keyPressed) {
MyMessages m= new MyMessages();
m.hello="client left pressed";
m.moveVector=new Vector3f(-1, 0, 0);
client.send(m);
clientred.setLocalTranslation(clientred.getLocalTranslation().add(m.moveVector));
}
if (name.equals("R")&&keyPressed) {
MyMessages m= new MyMessages();
m.hello="client right pressed";
m.moveVector=new Vector3f(1, 0, 0);
client.send(m);
clientred.setLocalTranslation(clientred.getLocalTranslation().add(m.moveVector));
}

}
}, new String []{"L","R"});
}
public static void main(String[] args) { MyClient app=new MyClient(); app.setShowSettings(false); app.start(); }
}//end of class

[/java]
ClientListener
[java]
package network;

import com.jme3.network.Client;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;

class ClientListener implements MessageListener<Client> {

@Override
public void messageReceived(Client source, Message m) {
MyMessages helloMessage = (MyMessages)m;
System.out.println(helloMessage.hello);

}

}

[/java]

MyMessage
[java]
package network;

import com.jme3.math.Vector3f;
import com.jme3.network.AbstractMessage;
import com.jme3.network.serializing.Serializable;

@Serializable
public class MyMessages extends AbstractMessage{
public String hello;
public Vector3f moveVector= new Vector3f(0,0,0);
public MyMessages() {

}
}

[/java]
here is the whole code from above
http://download391.mediafire.com/plh52q4shjmg/p6nz2r9902jlf6l/mynetworkcode.jar

You modify the Scenegraph in the NetworkThread, this is not allowed. You have o enque the modfications in the OpenglThread. There is a enqueue method in the application, that you can apss a callable, that callable then does the modifaication.





Alos at all due to recent often having this threading caused problem, maybee the exeption message should mention that modify scenegraph outside opengl thread is not allowed.

Also, a friendly word of advice… you might want to stick to Java coding conventions of capitalizing the first letter of class names. I found this code nearly unskimmable. I know it may sound picky but it really does make a difference to readability… and that means more people might spot the actual problems instead of stumbling around trying to figure the code out.

1 Like

@pspeed

thanks for your nice advice, I will try to improve myself. I already edited the above code a bit, hope it is a little bit better ^^ (sry for the ugly code before, actually I learned java by myself )



@EmpirePhoenix

thx for your help, I found app.enqueue(callable)

still dont know what to do with it (yet), like I said above I taught myself java, and I have no idea about callable,runable and thread (yet)

I will try to find a way, if I fail I will revive this thread and ask for your help again.