/** * Dispatches a message to the appropriate receiver. * * <p>It will buffer the message under the following conditions: 1) The MessageReceiver is not yet * registered. 2) The MessageReceiver is a PastryAppl, and localNode.isReady() == false * * @param msg the message. * @return true if message could be dispatched, false otherwise. */ public boolean dispatchMessage(Message msg) { if (msg.getDestination() == 0) { Logger logger = localNode.getEnvironment().getLogManager().getLogger(MessageDispatch.class, null); if (logger.level <= Logger.WARNING) logger.logException( "Message " + msg + "," + msg.getClass().getName() + " has no destination.", new Exception("Stack Trace")); return false; } // NOTE: There is no safety issue with calling localNode.isReady() because this is on the // PastryThread, and the only way to set a node ready is also on the ready thread. PastryAppl mr = (PastryAppl) addressBook.get(Integer.valueOf(msg.getDestination())); if (mr == null) { if ((logger.level <= Logger.FINE) || (localNode.isReady() && (logger.level <= Logger.INFO))) { logger.log( "Dropping message " + msg + " because the application address " + msg.getDestination() + " is unknown."); } return false; } else { mr.receiveMessage(msg); return true; } }
/** * Get a handle to a bootstrap node. This is only a simulation, so we pick the most recently * created node. * * @return handle to bootstrap node, or null. */ private NodeHandle getBootstrap() { NodeHandle bootstrap = null; try { PastryNode lastnode = (PastryNode) pastryNodes.lastElement(); bootstrap = lastnode.getLocalHandle(); } catch (NoSuchElementException e) { } return bootstrap; }
/** * Create a Pastry node and add it to pastryNodes. Also create a client application for this node. */ public void makePastryNode(int num) { PastryNode pn = factory.newNode(getBootstrap()); pastryNodes.addElement(pn); HelloWorldApp app = new HelloWorldApp(pn); helloClients.addElement(app); synchronized (pn) { while (!pn.isReady()) { try { pn.wait(300); } catch (InterruptedException ie) { } } } System.out.println("created " + num + " " + pn); }
/** Print leafsets of all nodes in pastryNodes. */ private void printLeafSets() { for (int i = 0; i < pastryNodes.size(); i++) { PastryNode pn = (PastryNode) pastryNodes.get(i); System.out.println(pn.getLeafSet().toString()); } }
/** Constructor. */ public MessageDispatch(PastryNode pn, Deserializer deserializer) { this.deserializer = deserializer; addressBook = new HashMap<Integer, PastryAppl>(); this.localNode = pn; this.logger = pn.getEnvironment().getLogManager().getLogger(getClass(), null); }