Serializing Inet4Address

Hi,



I was completing the networking tutorials, and got stuck with the serialization trail.



Since I was using the same code from the Sending and receiving messages tutorial, I think there’s quite a few line codes that are missing in the tutorial, so I wanted to help.



I will post the code.

On the client project:



Main class:



[java]import com.jme3.network.connection.Client;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;

import com.jme3.network.message.ZIPCompressedMessage;

import java.io.IOException;

import com.jme3.network.serializing.Serializer;

import java.net.Inet4Address;



public class Main implements MessageListener {



public static void main(String[] args) {

Main m = new Main();

m.begin();

}



public void begin() {

try {

Client client = new Client(“localhost”, 4040, 5050);

client.start();

Serializer.registerClass(HelloMessage.class);

Serializer.registerClass(ZIPCompressedMessage.class);

Serializer.registerClass(Inet4Address.class,new Inet4AddressSerializer());

Serializer.registerClass(AddressMessage.class);

client.addMessageListener(this, HelloMessage.class);

client.addMessageListener(this, ZIPCompressedMessage.class);

client.addMessageListener(this, AddressMessage.class);

client.send(new ZIPCompressedMessage(new HelloMessage()));

client.send(new ZIPCompressedMessage(new AddressMessage((Inet4Address)Inet4Address.getByName(“google.com”))));

} catch (IOException iOException) {

iOException.printStackTrace(System.err);

}

}



public void messageReceived(Message message) {

if (message instanceof HelloMessage) {

HelloMessage helloMessage = (HelloMessage) message;

System.out.println(helloMessage.hello);

} else {

AddressMessage addressMessage = (AddressMessage) message;

System.out.println(addressMessage.addr);

}

}



public void messageSent(Message message) {

}



public void objectReceived(Object object) {

}



public void objectSent(Object object) {

}

}

[/java]



Inet4AddressSerializer class:



[java]import com.jme3.network.serializing.Serializer;

import java.io.IOException;

import java.net.Inet4Address;

import java.nio.ByteBuffer;



public class Inet4AddressSerializer extends Serializer {



public Inet4Address readObject(ByteBuffer data, Class c) throws IOException {

byte[] address = new byte[4];

data.get(address);

return (Inet4Address)Inet4Address.getByAddress(address);

}



public void writeObject(ByteBuffer buffer, Object object) throws IOException {

Inet4Address address = (Inet4Address) object;

buffer.put(address.getAddress());

}

}[/java]



HelloMessage class:



[java]import com.jme3.network.message.Message;

import com.jme3.network.serializing.Serializable;



@Serializable(id=4)

public class HelloMessage extends Message{



public String hello = “Hello!”;



}[/java]



AddressMessage class:



[java]import com.jme3.network.message.Message;

import com.jme3.network.serializing.Serializable;

import java.net.Inet4Address;

import java.net.InetAddress;



@Serializable(id=5)

public class AddressMessage extends Message {



public InetAddress addr;



public AddressMessage(){

}



public AddressMessage(Inet4Address addr) {

this.addr = addr;

}



}[/java]

On the server project:



Main class:



[java]import com.jme3.network.connection.Server;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;

import com.jme3.network.message.ZIPCompressedMessage;

import com.jme3.network.serializing.Serializer;

import java.io.IOException;

import java.net.Inet4Address;



public class Main implements MessageListener {



public static void main(String[] args) {

Main m = new Main();

m.begin();

}



public void begin() {

try {

Server myServer = new Server(4040, 5050);

myServer.start();

Serializer.registerClass(HelloMessage.class);

Serializer.registerClass(ZIPCompressedMessage.class);

Serializer.registerClass(Inet4Address.class,new Inet4AddressSerializer());

Serializer.registerClass(AddressMessage.class);

myServer.addMessageListener(this, HelloMessage.class);

myServer.addMessageListener(this, ZIPCompressedMessage.class);

} catch (IOException iOException) {

iOException.printStackTrace(System.err);

}

}



public void messageReceived(Message message) {

try {

ZIPCompressedMessage zIPCompressedMessage=(ZIPCompressedMessage) message;

Message uncompressedMessage=zIPCompressedMessage.getMessage();

if (uncompressedMessage instanceof HelloMessage) {

HelloMessage helloMessage = (HelloMessage) uncompressedMessage;

System.out.println(helloMessage.hello);

helloMessage.hello = “Hi!”;

message.getClient().send(helloMessage);

} else if(uncompressedMessage instanceof AddressMessage){

AddressMessage addressMessage=(AddressMessage) uncompressedMessage;

System.out.println(addressMessage.addr);

}

} catch (IOException iOException) {

iOException.printStackTrace(System.err);

}

}



public void messageSent(Message message) {

}



public void objectReceived(Object object) {

}



public void objectSent(Object object) {

}

}[/java]



The Inet4AddressSerializer, HelloMessage, and AddressMessage are the same.



Anyway, so I wanted to help because I had to travel quite a road to get to this code (I’m a clumsy monkey :smiley: ) and I think that the tutorial could be improved, hope this code will do.