/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there * is no guarantee the peer will have received it. Throws NotYetConnectedException if we are not * yet connected to the remote peer. TODO: Maybe use something other than the unchecked * NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }