/** Send the message over the channel specified by the message. */ protected void write(Channel channel, PaxosMessage msg) { ChannelFuture future; // Send out a command. future = channel.write(msg); // Write the contents to the channel. future.addListener(this); }
/** Starts the initiation process for synching. */ public void startClient(final String host, final int port, final PaxosMessage msg) { ChannelFuture future; ClientBootstrap client; // Bootstrap a new client. Not sure if the // clientFactory is thread-safe or not, so play it safe. synchronized (this) { client = new ClientBootstrap(clientFactory); } // Set some TCP options. client.setOption("tcpNoDelay", true); client.setOption("keepAlive", true); // Create a new connection & state manager. final CommManager manager = new CommManager(); client.setPipelineFactory( new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { return Channels.pipeline( new PaxosMessageDecoder(), new PaxosMessageEncoder(), manager); } }); // Include the data we want to send. manager.start(msg); // Try to connect to the host. future = client.connect(new InetSocketAddress(host, port)); future.addListener( new ChannelFutureListener() { public void operationComplete(ChannelFuture f) { if (f.isDone() && !f.isSuccess()) { // This is a connection failure. This is not necessarily // the end of the world. Chances are that a client has // shut down and its routing information hasn't been updated yet. if (!network.updateDisconnected(host, port)) {} } } }); }
/** The write operation has completed. */ @Override public void operationComplete(ChannelFuture future) throws Exception { // Try closing the channel so that we don't leak descriptors. future.getChannel().close(); }