Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization refactoring efforts by Michiel Haisma & Rowan Bottema #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*/bin/*
*.*~
.classpath
.settings
.project
/.gradle/
/.nb-gradle/private/
/.nb-gradle/profiles/private/
Expand Down
15 changes: 8 additions & 7 deletions jme3-core/src/main/resources/com/jme3/system/version.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# THIS IS AN AUTO-GENERATED FILE..
# DO NOT MODIFY!
build.date=1900-01-01
git.revision=0
git.branch=unknown
git.hash=
git.hash.short=
git.tag=
name.full=jMonkeyEngine 3.1.0-UNKNOWN
build.date=2016-03-30
git.revision=5488
git.branch=addDefaultServerTests
git.hash=d489ecc91ff05cc4b9b4bbb0c5540fc02ecd0832
git.hash.short=d489ecc
git.tag=null
name.full=jMonkeyEngine 3.1-addDefaultServerTests-5488
version.full=3.1-addDefaultServerTests-5488
version.number=3.1.0
version.tag=SNAPSHOT
22 changes: 16 additions & 6 deletions jme3-examples/src/main/java/jme3test/network/TestChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
*/
package jme3test.network;

import com.jme3.network.AbstractMessage;
import com.jme3.network.Client;
import com.jme3.network.ClientStateListener;
import com.jme3.network.ErrorListener;
import com.jme3.network.Message;
//import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Network;
import java.awt.Component;
Expand All @@ -45,6 +46,7 @@
import java.util.logging.Logger;
import javax.swing.*;
import jme3test.network.TestChatServer.ChatMessage;
import jme3test.network.TestChatServer.CommandMessage;

/**
* A simple test chat server. When SM implements a set
Expand Down Expand Up @@ -159,7 +161,7 @@ public void run() {
private class ChatHandler implements MessageListener<Client> {

@Override
public void messageReceived(Client source, Message m) {
public void messageReceived(Client source, AbstractMessage m) {
ChatMessage chat = (ChatMessage) m;

System.out.println("Received:" + chat);
Expand Down Expand Up @@ -227,10 +229,18 @@ public void actionPerformed(ActionEvent evt) {
String name = nameField.getText();
String message = messageField.getText();

ChatMessage chat = new ChatMessage(name, message);
chat.setReliable(reliable);
System.out.println("Sending:" + chat);
client.send(chat);
if(message.startsWith("/")) {
CommandMessage command = new CommandMessage(name, message);
command.setReliable(reliable);
System.out.println("Sending:" + command);
client.send(command);
}
else {
ChatMessage chat = new ChatMessage(name, message);
chat.setReliable(reliable);
System.out.println("Sending:" + chat);
client.send(chat);
}
}
}
}
70 changes: 60 additions & 10 deletions jme3-examples/src/main/java/jme3test/network/TestChatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.logging.Logger;

import com.jme3.network.*;
import com.jme3.network.serializing.Serializable;
import com.jme3.network.serializing.Serializer;
import java.io.IOException;

Expand Down Expand Up @@ -70,6 +69,7 @@ public TestChatServer() throws IOException {

ChatHandler handler = new ChatHandler();
server.addMessageListener(handler, ChatMessage.class);
server.addMessageListener(new CommandHandler(), CommandMessage.class);

server.addConnectionListener(new ChatConnectionListener());
}
Expand Down Expand Up @@ -124,7 +124,8 @@ protected void runCommand( HostedConnection conn, String user, String command )
public static void initializeClasses() {
// Doing it here means that the client code only needs to
// call our initialize.
Serializer.registerClass(ChatMessage.class);
// Serializer.registerClass(ChatMessage.class);
// Serializer.registerClass(CommandMessage.class);
}

public static void main(String... args) throws Exception {
Expand Down Expand Up @@ -160,20 +161,14 @@ public ChatHandler() {
}

@Override
public void messageReceived(HostedConnection source, Message m) {
public void messageReceived(HostedConnection source, AbstractMessage m) {
if (m instanceof ChatMessage) {
// Keep track of the name just in case we
// want to know it for some other reason later and it's
// a good example of session data
ChatMessage cm = (ChatMessage)m;
source.setAttribute("name", cm.getName());

// Check for a / command
if( cm.message.startsWith("/") ) {
runCommand(source, cm.name, cm.message);
return;
}

System.out.println("Broadcasting:" + m + " reliable:" + m.isReliable());

// Just rebroadcast... the reliable flag will stay the
Expand All @@ -184,6 +179,27 @@ public void messageReceived(HostedConnection source, Message m) {
}
}
}

private class CommandHandler implements MessageListener<HostedConnection> {

public CommandHandler() {
}

@Override
public void messageReceived(HostedConnection source, AbstractMessage m) {
if (m instanceof CommandMessage) {
// Keep track of the name just in case we
// want to know it for some other reason later and it's
// a good example of session data
CommandMessage cm = (CommandMessage)m;
source.setAttribute("name", cm.getName());

runCommand(source, cm.name, cm.command);
} else {
System.err.println("Received odd message:" + m);
}
}
}

private class ChatConnectionListener implements ConnectionListener {

Expand All @@ -199,7 +215,6 @@ public void connectionRemoved(Server server, HostedConnection conn) {

}

@Serializable
public static class ChatMessage extends AbstractMessage {

private String name;
Expand Down Expand Up @@ -234,4 +249,39 @@ public String toString() {
return name + ":" + message;
}
}

public static class CommandMessage extends AbstractMessage {

private String name;
private String command;

public CommandMessage() {
}

public CommandMessage(String name, String message) {
setName(name);
setMessage(message);
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setMessage(String s) {
this.command = s;
}

public String getMessage() {
return command;
}

@Override
public String toString() {
return name + ":" + command;
}
}
}
8 changes: 3 additions & 5 deletions jme3-examples/src/main/java/jme3test/network/TestLatency.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
package jme3test.network;

import com.jme3.network.*;
import com.jme3.network.serializing.Serializable;
import com.jme3.network.serializing.Serializer;
import java.io.IOException;

Expand All @@ -51,7 +50,6 @@ private static long getTime(){
return System.currentTimeMillis() - startTime;
}

@Serializable
public static class TimestampMessage extends AbstractMessage {

long timeSent = 0;
Expand All @@ -70,7 +68,7 @@ public TimestampMessage(long timeSent, long timeReceived){
}

public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(TimestampMessage.class);
// Serializer.registerClass(TimestampMessage.class);

Server server = Network.createServer(5110);
server.start();
Expand All @@ -79,7 +77,7 @@ public static void main(String[] args) throws IOException, InterruptedException{
client.start();

client.addMessageListener(new MessageListener<Client>(){
public void messageReceived(Client source, Message m) {
public void messageReceived(Client source, AbstractMessage m) {
TimestampMessage timeMsg = (TimestampMessage) m;

long curTime = getTime();
Expand All @@ -103,7 +101,7 @@ public void messageReceived(Client source, Message m) {
}, TimestampMessage.class);

server.addMessageListener(new MessageListener<HostedConnection>(){
public void messageReceived(HostedConnection source, Message m) {
public void messageReceived(HostedConnection source, AbstractMessage m) {
TimestampMessage timeMsg = (TimestampMessage) m;
TimestampMessage outMsg = new TimestampMessage(timeMsg.timeSent, getTime());
source.send(outMsg);
Expand Down
11 changes: 4 additions & 7 deletions jme3-examples/src/main/java/jme3test/network/TestMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@
package jme3test.network;

import com.jme3.network.*;
import com.jme3.network.serializing.Serializable;
import com.jme3.network.serializing.Serializer;
import java.io.IOException;

public class TestMessages {

@Serializable
public static class PingMessage extends AbstractMessage {
}

@Serializable
public static class PongMessage extends AbstractMessage {
}

private static class ServerPingResponder implements MessageListener<HostedConnection> {
public void messageReceived(HostedConnection source, com.jme3.network.Message message) {
public void messageReceived(HostedConnection source, AbstractMessage message) {
if (message instanceof PingMessage){
System.out.println("Server: Received ping message!");
source.send(new PongMessage());
Expand All @@ -57,16 +54,16 @@ public void messageReceived(HostedConnection source, com.jme3.network.Message me
}

private static class ClientPingResponder implements MessageListener<Client> {
public void messageReceived(Client source, com.jme3.network.Message message) {
public void messageReceived(Client source, AbstractMessage message) {
if (message instanceof PongMessage){
System.out.println("Client: Received pong message!");
}
}
}

public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(PingMessage.class);
Serializer.registerClass(PongMessage.class);
// Serializer.registerClass(PingMessage.class);
// Serializer.registerClass(PongMessage.class);

Server server = Network.createServer(5110);
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.jme3.network.Server;
import com.jme3.network.rmi.ObjectStore;
import com.jme3.network.serializing.Serializer;
import com.jme3.network.serializing.serializers.SavableSerializer;
//import com.jme3.network.serializing.serializers.SavableSerializer;
import com.jme3.scene.Spatial;
import java.io.IOException;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -102,7 +102,7 @@ public void simpleInitApp() {
}

public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(Savable.class, new SavableSerializer());
// Serializer.registerClass(Savable.class, new SavableSerializer());

createServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
package jme3test.network;

import com.jme3.network.*;
import com.jme3.network.serializing.Serializable;
import com.jme3.network.serializing.Serializer;
import java.io.IOException;
import java.util.*;

public class TestSerialization implements MessageListener<HostedConnection> {

@Serializable
public static class SomeObject {

private int val;
Expand Down Expand Up @@ -68,7 +66,6 @@ public enum Status {
Low;
}

@Serializable
public static class TestSerializationMessage extends AbstractMessage {

boolean z;
Expand Down Expand Up @@ -121,7 +118,7 @@ public TestSerializationMessage(boolean initIt){
}
}

public void messageReceived(HostedConnection source, Message m) {
public void messageReceived(HostedConnection source, AbstractMessage m) {
TestSerializationMessage cm = (TestSerializationMessage) m;
System.out.println(cm.z);
System.out.println(cm.b);
Expand All @@ -140,8 +137,8 @@ public void messageReceived(HostedConnection source, Message m) {
}

public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(SomeObject.class);
Serializer.registerClass(TestSerializationMessage.class);
// Serializer.registerClass(SomeObject.class);
// Serializer.registerClass(TestSerializationMessage.class);

Server server = Network.createServer( 5110 );
server.start();
Expand Down
Loading