buffer=Util.objectToByteBuffer(msg);
ByteArrayOutputStream bos=new ByteArrayOutputStream(100); DataOutputStream tmp=new DataOutputStream(bos); msg.writeTo(tmp); tmp.flush(); buffer=bos.toByteArray(); tmp.close();
    public void test_REGISTER_Route_To_All() throws Exception {        log.info("running test_REGISTER_Route_To_All");        int len;        byte[] buffer;        String groupName="TESTGROUP";        Message msg, msgCopy;        // Register the first member        Socket sOne = new Socket("localhost", routerPort);        DataInputStream disOne = new DataInputStream(sOne.getInputStream());        DataOutputStream dosOne = new DataOutputStream(sOne.getOutputStream());        // read the IpAddress sent by GossipRouter        len=disOne.readInt();        buffer=new byte[len];        disOne.readFully(buffer, 0, len);        IpAddress localAddrOne=(IpAddress)Util.objectFromByteBuffer(buffer);        assertEquals(localAddrOne.getIpAddress(), sOne.getLocalAddress());        assertEquals(localAddrOne.getPort(), sOne.getLocalPort());        // send REGISTER request        dosOne.writeInt(GossipRouter.REGISTER);        dosOne.writeUTF(groupName);        // send the Address back to the router        buffer=Util.objectToByteBuffer(localAddrOne);        dosOne.writeInt(buffer.length);        dosOne.write(buffer, 0, buffer.length);        dosOne.flush();        // registration of the first member is complete        // Register the second member        Socket sTwo = new Socket("localhost", routerPort);        DataInputStream disTwo = new DataInputStream(sTwo.getInputStream());        DataOutputStream dosTwo = new DataOutputStream(sTwo.getOutputStream());        // read the IpAddress sent by GossipRouter        len=disTwo.readInt();        buffer=new byte[len];        disTwo.readFully(buffer, 0, len);        IpAddress localAddrTwo=(IpAddress)Util.objectFromByteBuffer(buffer);        assertEquals(localAddrTwo.getIpAddress(), sTwo.getLocalAddress());        assertEquals(localAddrTwo.getPort(), sTwo.getLocalPort());        // send REGISTER request        dosTwo.writeInt(GossipRouter.REGISTER);        dosTwo.writeUTF(groupName);        // send the Address back to the router        buffer=Util.objectToByteBuffer(localAddrTwo);        dosTwo.writeInt(buffer.length);        dosTwo.write(buffer, 0, buffer.length);        dosTwo.flush();        // registration of the second member is complete        // make sure both clients registered        Thread.sleep(1000);        String payload="THIS IS A MESSAGE PAYLOAD " + random.nextLong();        // the first member sends a simple routing request to all members (null dest address)        msg=new Message(null, localAddrOne, payload);        buffer=Util.objectToByteBuffer(msg);        dosOne.writeUTF(groupName);        dosOne.write(0); // a 0 byte means a null address        dosOne.writeInt(buffer.length);        dosOne.write(buffer, 0, buffer.length);        dosOne.flush();        // only the second member should receive the routing request, the router won't send a        // message to the originator        // the second member reads the message        len=disTwo.readInt();        buffer=new byte[len];        disTwo.readFully(buffer, 0, len);        msgCopy=(Message)Util.objectFromByteBuffer(buffer);        assertEquals(msg.getSrc(), msgCopy.getSrc());        assertNull(msgCopy.getDest());        assertEquals(msg.getObject(), msgCopy.getObject());        // close the routing connection        disOne.close();        dosOne.close();        sOne.close();        disTwo.close();        dosTwo.close();        sTwo.close();    }