@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { ChannelBuffer buf; PaxosMessage msg; buf = (ChannelBuffer) e.getMessage(); // Read in the message string. msg = new PaxosMessage(); msg.unSerialize(buf.array()); // Send the message upstream to the server handler. Channels.fireMessageReceived(ctx, msg); }
/** * Send a specific message to the supplied role. * * @param destination Destination role * @param msg Message to send */ public void send(PaxosRole destination, PaxosMessage msg) { StaticNetwork.Route route = network.getRoute(destination.getID()); if (route != null) { msg.setDestination(destination.getType()); pipeline.startClient(route.ip, route.port, msg); } }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) { PaxosMessage msg; ChannelBuffer sendBuf; byte[] data; msg = (PaxosMessage) e.getMessage(); // The original message. // Serialize the message. data = msg.serialize(); sendBuf = ChannelBuffers.buffer(data.length); sendBuf.writeBytes(data); // Write the actual msg. // Send the message upstream. Channels.write(ctx, e.getFuture(), sendBuf); }
/** * Multi-cast a message to all nodes with a specific role. * * @param role Destination role * @param msg Message to send * @param expected Number of nodes to multicast * @return Success code */ public int multicast(PaxosRole.Role role, PaxosMessage msg, int expected) { // Get all the nodes in the network. Map<String, StaticNetwork.Route> routes = network.getAllRoutes(); int i = 0; for (String id : routes.keySet()) { if (!id.equals(home)) { // Make sure that we are not sending to a client. if (role != PaxosRole.Role.CLIENT && !network.isClientConnection(id)) { StaticNetwork.Route route = routes.get(id); msg.setDestination(role); pipeline.startClient(route.ip, route.port, msg); // Increment the number of messages sent. ++i; } } // Only send the minimum necessary. if (i == expected) { break; } } return PaxosCommunicator.SUCCESS; }
/** * Send a message to a non-specific node with the specified role. Usually this means sending to a * local role on the same node, unless the sending role is already on the node. */ public void send(PaxosRole sender, PaxosRole.Role role, PaxosMessage msg) { // Ask the network for the "desired" target. Depending on the // network implementation, this could be the "leader", etc. StaticNetwork.Route route = network.getDesirable(home); if (route != null) { msg.setDestination(role); pipeline.startClient(route.ip, route.port, msg); } }
/** The server has received a message. */ @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { PaxosMessage msg; // Get our consensus message and hand it back to the parent. msg = (PaxosMessage) e.getMessage(); if (msg != null) { // Try to get remote connection information. // This is used if we need to update the network // information later. Channel channel = ctx.getChannel(); InetSocketAddress remote = (InetSocketAddress) channel.getRemoteAddress(); // Update the message with the remote address. msg.setRemoteAddress(remote.getAddress().getHostAddress()); // Update the client. updateCallback(msg); // Close the channel so that we don't leak descriptors. channel.close(); } }