📄 pastryappl.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.pastry.client;import rice.environment.logging.Logger;import rice.p2p.commonapi.appsocket.*;import rice.p2p.commonapi.rawserialization.MessageDeserializer;import rice.pastry.*;import rice.pastry.messaging.*;import rice.pastry.standard.*;import rice.pastry.routing.*;import rice.pastry.leafset.*;import java.io.IOException;import java.util.*;/** * A PastryAppl is an abstract class that every Pastry application extends. This * is the external Pastry API. * * @version $Id: PastryAppl.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Peter Druschel */public abstract class PastryAppl implements Observer { /** * DESCRIBE THE FIELD */ protected MessageDeserializer deserializer; // private block /** * DESCRIBE THE FIELD */ protected String instance; /** * DESCRIBE THE FIELD */ protected PastryNode thePastryNode; /** * DESCRIBE THE FIELD */ protected int address; /** * DESCRIBE THE FIELD */ protected Logger logger; /** * Buffered while node is not ready to prevent inconsistent routing. */ LinkedList undeliveredMessages = new LinkedList(); /** * holds the receiverSocket */ protected AppSocketReceiver receiver; // constructor /** * Constructor. * * @param pn the pastry node that client will attach to. */ public PastryAppl(PastryNode pn) { this(pn, null); } /** * Constructor. This constructor will perform the same tasks as the above * constructor, but will also create a Pastry address for this application, * which is dependent upon the given instance name and the class name. * * @param pn the pastry node that client will attach to. * @param instance The instance name of this appl. */ public PastryAppl(PastryNode pn, String instance) { this(pn, instance, 0, null); register(); } /** * Constructor for PastryAppl. * * @param pn DESCRIBE THE PARAMETER * @param instance DESCRIBE THE PARAMETER * @param address DESCRIBE THE PARAMETER * @param md DESCRIBE THE PARAMETER */ public PastryAppl(PastryNode pn, String instance, int address, MessageDeserializer md) { this.address = address; if (instance != null) { this.instance = instance; if (address == 0) { this.address = StandardAddress.getAddress(this.getClass(), instance, pn.getEnvironment()); } } thePastryNode = pn; logger = pn.getEnvironment().getLogManager().getLogger(getClass(), instance); deserializer = md; if (deserializer == null) { deserializer = new JavaSerializedDeserializer(pn); } } /** * Constructor. This constructor will perform the same tasks as the above * constructor, but will also create a Pastry address for this application, * using the specified port. Need to call register on this. * * @param pn the pastry node that client will attach to. * @param port DESCRIBE THE PARAMETER */ public PastryAppl(PastryNode pn, int port) { this(pn, null, port, null); } /** * Returns the address of this application. * * @return the address. */ public int getAddress() { return address; } // useful API methods /** * Gets the node id associated with this client. * * @return the node id. */ public final Id getNodeId() { return thePastryNode.getNodeId(); } /** * Gets the handle of the Pastry node associated with this client * * @return the node handle */ public NodeHandle getNodeHandle() { return thePastryNode.getLocalHandle(); } /** * Called by a layered Pastry application to obtain a copy of the leaf set. * The leaf set contains the nodeId to IP address binding of the l/2 nodes * with numerically closest counterclockwise and the l/2 nodes with * numerically closest clockwise nodeIds, relatively to the local node's id. * * @return the local node's leaf set */ public LeafSet getLeafSet() { return thePastryNode.getLeafSet().copy(); } /** * Called by a layered Pastry application to obtain a copy of the routing * table. The routing table contains the nodeId to IP address bindings of R * nodes that share the local node's id in the first n digits, and differ in * the n+1th digit, for 0 <= n <= ceiling(log_2^b N), where N is the total * number of currently live nodes in the Pastry network. The routing table may * be incomplete, may contain nodes that cannot be reached from the local node * or have failed, and the table may change at any time. * * @return The RoutingTable value */ public RoutingTable getRoutingTable() { return thePastryNode.getRoutingTable(); } /** * Called by the layered Pastry application to check if the local pastry node * is the one that is currently closest to the object key id. * * @param key the object key id * @return true if the local node is currently the closest to the key. */ public boolean isClosest(Id key) { return thePastryNode.isClosest(key); } /** * Sets the Deserializer attribute of the PastryAppl object * * @param deserializer The new Deserializer value */ protected void setDeserializer(MessageDeserializer deserializer) { this.deserializer = deserializer; } /** * DESCRIBE THE METHOD */ public void register() { thePastryNode.registerReceiver(getAddress(), this); thePastryNode.addLeafSetListener(new LeafSetObserver()); thePastryNode.addRouteSetListener(new RouteSetObserver()); thePastryNode.registerApp(this); // just adds it to a list thePastryNode.addObserver(this); } // internal methods /** * DESCRIBE THE METHOD * * @param msg DESCRIBE THE PARAMETER */ public void receiveMessageInternal(RawMessageDelivery msg) { Message m; try { m = msg.deserialize(deserializer); } catch (IOException ioe) { if (logger.level <= Logger.SEVERE) { logger.logException("Error deserializing " + msg + " in " + this + ". Message will be dropped.", ioe); } return; } catch (RuntimeException re) { if (logger.level <= Logger.SEVERE) { logger.logException("Error deserializing " + msg + " in " + this + ". Message will be dropped.", re); } throw re; } receiveMessage(m); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -