package csx55.overlay.wireformats; import java.io.*; /** * This is a wireformat used by the registry to tell all messaging nodes to send their stats collected when * passing messages to peers. It follows the same format as all of the other wireformats and is built using * the provided example in the slides. The 2nd constructor takes in the byte stream and unmarshalls the information * sent by the node. It also implemnts the getBytes() method from the Event interface, which marshalls * the variables for this wireformat. The first constructor is used when creating an instance of the class * and the second is used by the event factory to create a new instance and return a class that the bytes * unmarshalled. The getBytes() is used before the data is sent using the sender thread. */ public class TaskSummaryRequest implements Event { private int type; public TaskSummaryRequest() { this.type = Protocol.PULL_TRAFFIC_SUMMARY; } public TaskSummaryRequest(byte[] marshalledByte) throws IOException { ByteArrayInputStream baInputStream = new ByteArrayInputStream(marshalledByte); DataInputStream din = new DataInputStream(new BufferedInputStream(baInputStream)); this.type = din.readInt(); baInputStream.close(); din.close(); } @Override public int getType() { return this.type; } @Override public byte[] getBytes() throws IOException { byte[] marshalledBytes = null; ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(baOutputStream)); dout.writeInt(type); dout.flush(); marshalledBytes = baOutputStream.toByteArray(); baOutputStream.close(); dout.close(); return marshalledBytes; } }