📄 multiringendpoint.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.p2p.multiring;import rice.*;import java.io.IOException;import java.util.*;import rice.environment.Environment;import rice.p2p.commonapi.*;import rice.p2p.commonapi.appsocket.AppSocketReceiver;import rice.p2p.commonapi.rawserialization.*;import rice.p2p.multiring.messaging.RingMessage;import rice.p2p.util.JavaSerializedMessage;/** * @(#) MultiringEndpoint.java This class wraps an endpoint and allows for * applications to transparently use multiring functionality. * * @version $Id: MultiringEndpoint.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Alan Mislove */public class MultiringEndpoint implements Endpoint { /** * The multiring node supporting this endpoint */ protected MultiringNode node; /** * The application this endpoint is for */ protected Application application; /** * The node which this mulitring node is wrapping */ protected Endpoint endpoint;// protected MessageDeserializer myDeserializer; /** * Constructor * * @param node The node to base this node off of * @param endpoint DESCRIBE THE PARAMETER * @param application DESCRIBE THE PARAMETER */ protected MultiringEndpoint(MultiringNode node, Endpoint endpoint, Application application) { this.node = node; this.endpoint = endpoint; this.application = application;// myDeserializer = endpoint.getDeserializer();// endpoint.setDeserializer(new MessageDeserializer() {//// public Message deserialize(InputBuffer buf, short type, byte priority,// NodeHandle sender) throws IOException {// switch (type) {// case RingMessage.TYPE:// return new RingMessage(buf,MultiringEndpoint.this.endpoint,myDeserializer,sender,priority);// }// throw new IllegalArgumentException("Invalid type:"+type);// }//// }); } /** * Returns this node's id, which is its identifier in the namespace. * * @return The local node's id */ public Id getId() { return RingId.build(node.getRingId(), endpoint.getId()); } /** * Returns a handle to the local node below this endpoint. This node handle is * serializable, and can therefore be sent to other nodes in the network and * still be valid. * * @return A NodeHandle referring to the local node. */ public NodeHandle getLocalNodeHandle() { return new MultiringNodeHandle(node.getRingId(), endpoint.getLocalNodeHandle()); } /** * Returns a unique instance name of this endpoint, sort of a mailbox name for * this application. * * @return The unique instance name of this application */ public String getInstance() { return "multiring" + endpoint.getInstance(); } /* * (non-Javadoc) * @see rice.p2p.commonapi.Endpoint#getEnvironment() */ /** * Gets the Environment attribute of the MultiringEndpoint object * * @return The Environment value */ public Environment getEnvironment() { return node.getEnvironment(); } /** * Gets the Deserializer attribute of the MultiringEndpoint object * * @return The Deserializer value */ public MessageDeserializer getDeserializer() { return endpoint.getDeserializer(); } /** * Sets the Deserializer attribute of the MultiringEndpoint object * * @param md The new Deserializer value */ public void setDeserializer(MessageDeserializer md) { endpoint.setDeserializer(md); } /** * This method makes an attempt to route the message to the root of the given * id. The hint handle will be the first hop in the route. If the id field is * null, then the message is routed directly to the given node, and delivers * the message there. If the hint field is null, then this method makes an * attempt to route the message to the root of the given id. Note that one of * the id and hint fields can be null, but not both. * * @param id The destination Id of the message. * @param message The message to deliver * @param hint The first node to send this message to, optional */ public void route(Id id, Message message, NodeHandle hint) { if (message instanceof RawMessage) { route(id, (RawMessage) message, hint); } else { route(id, new JavaSerializedMessage(message), hint); } } /** * DESCRIBE THE METHOD * * @param id DESCRIBE THE PARAMETER * @param message DESCRIBE THE PARAMETER * @param hint DESCRIBE THE PARAMETER */ public void route(Id id, RawMessage message, NodeHandle hint) { RingId mId = (RingId) id; MultiringNodeHandle mHint = (MultiringNodeHandle) hint; if (mId == null) { if (mHint.getRingId().equals(node.getRingId())) { endpoint.route(null, message, mHint.getHandle()); } else { route(mHint.getId(), message, null); } } else { if (mId.getRingId().equals(node.getRingId())) { if ((mHint != null) && (mHint.getRingId().equals(node.getRingId()))) { endpoint.route(mId.getId(), message, mHint.getHandle()); } else { endpoint.route(mId.getId(), message, null); } } else { node.getCollection().route(mId, message, getInstance()); } } } /** * This call produces a list of nodes that can be used as next hops on a route * towards the given id, such that the resulting route satisfies the overlay * protocol's bounds on the number of hops taken. If the safe flag is * specified, then the fraction of faulty nodes returned is no higher than the * fraction of faulty nodes in the overlay. * * @param id The destination id. * @param num The number of nodes to return. * @param safe Whether or not to return safe nodes. * @return DESCRIBE THE RETURN VALUE */ public NodeHandleSet localLookup(Id id, int num, boolean safe) { return new MultiringNodeHandleSet(node.getRingId(), endpoint.localLookup(((RingId) id).getId(), num, safe)); } /** * This methods returns an unordered set of nodehandles on which are neighbors * of the local node in the id space. Up to num handles are returned. * * @param num The number of desired handle to return. * @return DESCRIBE THE RETURN VALUE */ public NodeHandleSet neighborSet(int num) { return new MultiringNodeHandleSet(node.getRingId(), endpoint.neighborSet(num)); } /** * This methods returns an ordered set of nodehandles on which replicas of an * object with a given id can be stored. The call returns nodes up to and * including a node with maxRank. * * @param id The object's id. * @param maxRank The number of desired replicas. * @return DESCRIBE THE RETURN VALUE */ public NodeHandleSet replicaSet(Id id, int maxRank) { if (((RingId) id).getRingId().equals(node.getRingId())) { return new MultiringNodeHandleSet(node.getRingId(), endpoint.replicaSet(((RingId) id).getId(), maxRank)); } else { return new MultiringNodeHandleSet(((RingId) id).getRingId(), node.getNode().getIdFactory().buildNodeHandleSet()); } } /** * This methods returns an ordered set of nodehandles on which replicas of an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -