📄 routeset.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.routing;import rice.p2p.commonapi.rawserialization.*;import rice.pastry.*;import java.util.*;import java.io.*;/** * A set of nodes typically stored in the routing table. The set contains a * bounded number of the closest node handles. Since proximity value can change * unpredictably, we don't keep the set in sorted order. * * @version $Id: RouteSet.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Andrew Ladd * @author Peter Druschel */public class RouteSet implements NodeSetI, Serializable, Observer { private NodeHandle[] nodes; private int theSize; private int closest; transient RoutingTable observer; /** * DESCRIBE THE FIELD */ public final static short TYPE = 2; private final static long serialVersionUID = 8156336294555109590L; /** * Constructor. * * @param maxSize the maximum number of nodes that fit in this set. */ public RouteSet(int maxSize) { nodes = new NodeHandle[maxSize]; theSize = 0; closest = -1; } /** * Constructor for RouteSet. * * @param buf DESCRIBE THE PARAMETER * @param nhf DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public RouteSet(InputBuffer buf, NodeHandleFactory nhf) throws IOException { byte maxSize = buf.readByte(); theSize = buf.readByte(); closest = buf.readByte(); nodes = new NodeHandle[maxSize]; for (int i = 0; i < theSize; i++) { nodes[i] = nhf.readNodeHandle(buf); } } /** * Returns the node in the ith position in the set. * * @param i DESCRIBE THE PARAMETER * @return the ith node. */ public NodeHandle get(int i) { if (i < 0 || i >= theSize) { throw new NoSuchElementException(); } return nodes[i]; } /** * Returns the node handle with the matching node id or null if none exists. * * @param nid the node id. * @return the node handle. */ public NodeHandle get(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getNodeId().equals(nid)) { return nodes[i]; } } return null; } /** * Get the index of the node id. * * @param nid DESCRIBE THE PARAMETER * @return the node. */ public int getIndex(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getNodeId().equals(nid)) { return i; } } return -1; } /** * Get the index of the node id. * * @param nh DESCRIBE THE PARAMETER * @return the node. */ public int getIndex(NodeHandle nh) { for (int i = 0; i < theSize; i++) { if (nodes[i].equals(nh)) { return i; } } return -1; } /** * Finds the NodeHandle associated with the Id. * * @param id a node id. * @return the handle associated with that id or null if no such handle is * found. */ public rice.p2p.commonapi.NodeHandle getHandle(rice.p2p.commonapi.Id id) { return getHandle((Id) id); } /** * Gets the ith element in the set. * * @param i an index. * @return the handle associated with that id or null if no such handle is * found. */ public rice.p2p.commonapi.NodeHandle getHandle(int i) { return get(i); } /** * Gets the index of the element with the given node id. * * @param id the id. * @return the index or throws a NoSuchElementException. * @exception NoSuchElementException DESCRIBE THE EXCEPTION */ public int getIndexHandle(rice.p2p.commonapi.Id id) throws NoSuchElementException { return getIndex((Id) id); } /** * Gets the Type attribute of the RouteSet object * * @return The Type value */ public short getType() { return TYPE; } /** * Sets the RoutingTable attribute of the RouteSet object * * @param rt The new RoutingTable value */ public void setRoutingTable(RoutingTable rt) { observer = rt; } /** * DESCRIBE THE METHOD * * @return DESCRIBE THE RETURN VALUE */ public String toString() { String s = "RS: "; for (int i = 0; i < nodes.length; i++) { s += nodes[i] + ","; } return s; } /** * Puts a node into the set. The insertion succeeds either if the set is below * is maximal size or if the handle is closer than the most distant member in * the set. * * @param handle the handle to put. * @return true if the put succeeded, false otherwise. */ public boolean put(NodeHandle handle) { int worstIndex = -1; int worstProximity = Integer.MIN_VALUE; // scan entries for (int i = 0; i < theSize; i++) { // if handle is already in the set, abort if (nodes[i].equals(handle)) { return false; } // find entry with worst proximity int p = nodes[i].proximity(); if (p >= worstProximity) { worstProximity = p; worstIndex = i; } } if (theSize < nodes.length) { nodes[theSize++] = handle;// setChanged(); notifyTable(handle, true); // ping handles while the set is not full handle.ping(); handle.addObserver(this); return true; } else { if (handle.proximity() == Integer.MAX_VALUE) { // wait until the proximity value is available handle.ping(); // XXX - eventually, should only ping handles pinged from // the deserializer handle.addObserver(this); return false; } else if (handle.proximity() < worstProximity) { // remove handle with worst proximity// setChanged(); notifyTable(nodes[worstIndex], false); // in case we observe this handle, stop doing so nodes[worstIndex].deleteObserver(this); // insert new handle nodes[worstIndex] = handle;// setChanged(); notifyTable(handle, true); handle.addObserver(this); return true; } else { return false; } } } /** * Is called by the Observer pattern whenever the liveness or proximity of a * registered node handle is changed. * * @param o The node handle * @param arg the event type (PROXIMITY_CHANGE, DECLARED_LIVE, DECLARED_DEAD) */ public void update(Observable o, Object arg) { // if the proximity is initialized for the time, insert the handle if (((Integer) arg) == NodeHandle.PROXIMITY_CHANGED) { put((NodeHandle) o); } else if (((Integer) arg) == NodeHandle.DECLARED_DEAD) { // changed to remove dead handles - AM remove((NodeHandle) o); } } /** * Removes a node from a set. * * @param nid the node id to remove. * @return the removed handle or null. */ public NodeHandle remove(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getNodeId().equals(nid)) { NodeHandle handle = nodes[i]; nodes[i] = nodes[--theSize]; notifyTable(handle, false); // in case we observe this handle, stop doing so handle.deleteObserver(this); return handle; } } return null; } /** * Removes a node from a set. * * @param nh DESCRIBE THE PARAMETER * @return the removed handle or null. */ public NodeHandle remove(NodeHandle nh) { for (int i = 0; i < theSize; i++) { if (nodes[i].equals(nh)) { NodeHandle handle = nodes[i]; nodes[i] = nodes[--theSize]; notifyTable(handle, false); // in case we observe this handle, stop doing so handle.deleteObserver(this); return handle; } } return null; } /** * DESCRIBE THE METHOD * * @param handle DESCRIBE THE PARAMETER * @param added DESCRIBE THE PARAMETER */ private void notifyTable(NodeHandle handle, boolean added) { if (observer != null) { observer.nodeSetUpdate(null, handle, added); } } /** * Membership test. * * @param nh DESCRIBE THE PARAMETER * @return true if it is a member, false otherwise. */ public boolean member(NodeHandle nh) { for (int i = 0; i < theSize; i++) { if (nodes[i].equals(nh)) { return true; } } return false; } /** * Membership test. * * @param nid the node id to membership of. * @return true if it is a member, false otherwise. */ public boolean member(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getNodeId().equals(nid)) { return true; } } return false; } /** * Return the current size of the set. * * @return the size. */ public int size() { return theSize; } /** * Pings all new nodes in the RouteSet. No longer- Called from * RouteMaintenance. */ public void pingAllNew() { for (int i = 0; i < theSize; i++) { if (nodes[i].proximity() == Integer.MAX_VALUE) { nodes[i].ping(); } } } /** * Return the closest live node in the set. * * @return the closest node, or null if no live node exists in the set. */ public NodeHandle closestNode() { return closestNode(NodeHandle.LIVENESS_SUSPECTED); } /** * Return the closest live node in the set. * * @param minLiveness DESCRIBE THE PARAMETER * @return the closest node, or null if no live node exists in the set. */ public NodeHandle closestNode(int minLiveness) { int bestProximity = Integer.MAX_VALUE; NodeHandle bestNode = null; for (int i = 0; i < theSize; i++) { if (nodes[i].getLiveness() > minLiveness) { continue; } int p = nodes[i].proximity(); if (p <= bestProximity) { bestProximity = p; bestNode = nodes[i]; closest = i; } } // If a backup node handle bubbles up to the top, ping it. if (bestNode != null && bestProximity == Integer.MAX_VALUE) { bestNode.ping(); } return bestNode; } /** * deserialize the routeSet pings the handle the was the closests on the * sending node * * @param in DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION * @exception ClassNotFoundException DESCRIBE THE EXCEPTION */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { nodes = (NodeHandle[]) in.readObject(); theSize = in.readInt(); closest = in.readInt(); if (closest != -1) { nodes[closest].ping(); } closest = -1; } /** * serialize the RouteSet records the closest node * * @param out DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION * @exception ClassNotFoundException DESCRIBE THE EXCEPTION */ private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { if (closest == -1) { closestNode(); } // here, we don't want to advetise nodes which are dead, so we filter // our list based on only live nodes NodeHandle[] tmp = new NodeHandle[nodes.length]; int j = 0; for (int i = 0; i < tmp.length; i++) { if ((nodes[i] != null) && (nodes[i].isAlive())) { tmp[j] = nodes[i]; j++; } } out.writeObject(tmp); out.writeInt(j); int closest = -1; for (int i = 0; i < j; i++) { if ((closest == -1) || (tmp[i].proximity() < tmp[closest].proximity())) { closest = i; } } out.writeInt(closest); } // Common API Support /** * Puts a NodeHandle into the set. * * @param handle the handle to put. * @return true if the put succeeded, false otherwise. */ public boolean putHandle(rice.p2p.commonapi.NodeHandle handle) { return put((NodeHandle) handle); } /** * Verifies if the set contains this particular id. * * @param id a node id. * @return true if that node id is in the set, false otherwise. */ public boolean memberHandle(rice.p2p.commonapi.Id id) { return member((Id) id); } /** * Removes a node id and its handle from the set. * * @param id DESCRIBE THE PARAMETER * @return the node handle removed or null if nothing. */ public rice.p2p.commonapi.NodeHandle removeHandle(rice.p2p.commonapi.Id id) { return remove((Id) id); } /** * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + maxSize + theSize + * closest + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * + NodeHandle 1st + ... + + + + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * NodeHandle theSize-th + ... + + + + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * @param buf DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public void serialize(OutputBuffer buf) throws IOException { buf.writeByte((byte) nodes.length); buf.writeByte((byte) theSize); buf.writeByte((byte) closest); for (int i = 0; i < theSize; i++) { nodes[i].serialize(buf); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -