I've written a runnable network class that listens on a socket and unmarshalls the input. It also can write to the socket with a marshalled object. An issue arises because the socket remains open (in order to allow later communication between client and host) - this causes the unmarshalling of the input stream to hang. I've tried writing XMLStreamConstants.END_DOCUMENT from the sender side but this causes an error unmarshalling instead of hanging. Here's some of the code for the network class:
@Override
public void update(Observable o, Object arg) {
try {
if(!this.updatedByNetwork){
OutputStream os = socket.getOutputStream();
mh.marshal(this.gm.getBoard(), os);
os.flush();
}
}catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
if (this.ss != null){
this.socket = this.ss.accept();
this.update(this.gm, null);
}
while (true){
try {
InputStream is = socket.getInputStream();
Board b = mh.unmarshal(is);
this.updatedByNetwork = true;
this.gm.updateBoard(b);
} catch (SocketTimeoutException e){
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Here's the code for my marshall handler:
public Board unmarshal(InputStream in) throws JAXBException{
Unmarshaller um = this.jc.createUnmarshaller();
Board b = (Board) um.unmarshal(in);
return b;
}
public void marshal(Board b, OutputStream os) throws JAXBException {
Marshaller m = this.jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(b, os);
}
So, is there a way to signify the end of file for the unmarshaller? Or, is there a better way to do this?
Even if there is a way to signal "end of file" to the unmarshaller, there is still a chance that the unmarshaller will read into the next message when two or more messages are send directly after each other. To prevent this from happening, a network protocol layer needs to be in place that logically separates the bytes send/received into separate messages. In the example below this 'protocol' is implemented in the writeMsg
and readMsg
methods. Note that this is a simple example that assumes all messages can be processed entirely in memory.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class NetworkMarshall {
private static final int NumberOfMsgs = 2;
public static void main(String[] args) {
Socket s = null;
try {
JAXBContext jc = JAXBContext.newInstance(NetworkMarshall.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Unmarshaller unmarshaller = jc.createUnmarshaller();
new Thread(new Receiver(unmarshaller)).start();
// Wait for socket server to start
Thread.sleep(500);
s = new Socket(InetAddress.getLocalHost(), 54321);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
for (int i = 0; i < NumberOfMsgs; i++) {
NetworkMarshall msg = new NetworkMarshall();
msg.setName("vanOekel" + i);
writeMsg(msg, marshaller, dos);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { s.close(); } catch (Exception ignored) {}
}
}
private static void writeMsg(NetworkMarshall msg, Marshaller marshaller, DataOutputStream dos) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
marshaller.marshal(msg, bout);
byte[] msgBytes = bout.toByteArray();
System.out.println("Sending msg: " + new String(msgBytes));
dos.writeInt(msgBytes.length);
dos.write(msgBytes);
dos.flush();
}
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return this.getClass().getName() + ": " + getName();
}
static class Receiver implements Runnable {
final Unmarshaller unmarshaller;
public Receiver(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void run() {
ServerSocket ss = null;
Socket s = null;
try {
s = (ss = new ServerSocket(54321)).accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
for (int i = 0; i < NumberOfMsgs; i++) {
Object o = unmarshaller.unmarshal(readMsg(dis));
System.out.println("Received message " + i + ": " + o);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { ss.close(); } catch (Exception ignored) {}
try { s.close(); } catch (Exception ignored) {}
}
}
private ByteArrayInputStream readMsg(DataInputStream dis) throws Exception {
int size = dis.readInt();
byte[] ba = new byte[size];
dis.readFully(ba);
return new ByteArrayInputStream(ba);
}
}
}