/** @param network The network contains routing information. */ public NettyCommunicator(StaticNetwork network) { this.network = network; // Initialize the communication pipeline. pipeline = new NettyPipeline(); pipeline.initServer(); pipeline.initClient(); }
/** * 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); } }
/** * 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); } }
/** * Start listening for network communication. * * @param id ID identifying the listening node. */ public void startListening(String id) { home = id; StaticNetwork.Route route = network.getRoute(id); if (route != null) { if (pipeline.startServer(route.port)) { logger.log(Level.INFO, String.format("%s: listening port %d", id, route.port)); } } }