public void serialize(ReusableBuffer rb) { rb.put(CURRENT_VERSION); rb.putString(uuid); rb.putLong(getVersion()); for (KeyValuePair kvp : configurationParameter) { rb.putString(kvp.getKey()); rb.putString(kvp.getValue()); } }
public ConfigurationRecord(ReusableBuffer rb) throws IOException { byte recVersion = rb.get(); if (recVersion == 1) { this.uuid = rb.getString(); this.version = rb.getLong(); this.configurationParameter = new Vector<KeyValuePair>(); while (rb.remaining() != 0) { configurationParameter.add( KeyValuePair.newBuilder().setKey(rb.getString()).setValue(rb.getString()).build()); } } else { throw new IOException("don't know how to handle version " + recVersion); } }
/** @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Logging.start(Logging.LEVEL_DEBUG); TimeSync ts = TimeSync.initializeLocal(50); try { ts.waitForStartup(); TCPClient com = new TCPClient( 3334, null, new NIOServer() { public void onAccept(NIOConnection connection) { onConnect(connection); } public void onConnect(NIOConnection connection) { System.out.println("connected to " + connection.getEndpoint()); connection.read(BufferPool.allocate(1024)); connection.setContext(new AtomicBoolean(false)); } public void onRead(NIOConnection connection, ReusableBuffer buffer) { System.out.println("read from " + connection); buffer.flip(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); String contents = new String(data); BufferPool.free(buffer); connection.read(BufferPool.allocate(1024)); System.out.println(">> " + contents); } public void onClose(NIOConnection connection) { System.out.println("connection from " + connection.getEndpoint() + " closed "); } public void onWriteFailed(IOException exception, Object context) { System.out.println("could not write, context: " + context); } public void onConnectFailed( InetSocketAddress endpoint, IOException exception, Object context) { System.out.println("could not connect to: " + endpoint + ", context: " + context); } }); com.start(); com.waitForStartup(); ReusableBuffer data = ReusableBuffer.wrap("Hello world!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, "Yagg"); Thread.sleep(100); data = ReusableBuffer.wrap("Hello world!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, "Yagga"); Thread.sleep(30000); data = ReusableBuffer.wrap("YaggaYagga!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, null); Thread.sleep(2000); com.shutdown(); ts.close(); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } }