📄 symphonynode.java
字号:
StringBuffer sb = new StringBuffer();
sb.append("________________________________________________");
sb.append("\nId [" + id + "] >> \n\n");
sb.append("\nRole> " + (role?BehavioursPatternImpl.ROLE_GOOD:BehavioursPatternImpl.ROLE_BAD));
sb.append("\nN> " + n);
Iterator it;
sb.append("\nOutSet> ");
it = outcommingSet.iterator();
while (it.hasNext()) {
NodeHandle current = (NodeHandle) it.next();
sb.append(current.toString() + ",");
}
sb.append("\nInSet> ");
it = incommingSet.iterator();
while (it.hasNext()) {
NodeHandle current = (NodeHandle) it.next();
sb.append(current.toString() + ",");
}
sb.append("\nNeighbourSet> ");
it = neighbourSet.iterator();
while (it.hasNext()) {
NodeHandle current = (NodeHandle) it.next();
sb.append(current.toString() + ",");
}
sb.append("\noutcomming> "+this.outMessages());
sb.append("\nincomming > "+this.inMessages());
sb.append("\n________________________________________________");
System.out.println(sb.toString());
}
/**
* Shows a brief description of the internal routing state.
* @see planet.commonapi.Node#prettyPrintNode()
*/
public void prettyPrintNode() {
StringBuffer sb = new StringBuffer();
sb.append("________________________________________________");
sb.append("\nId [" + id + "] >> \n\n");
sb.append("\nRole> " + (role?BehavioursPatternImpl.ROLE_GOOD:BehavioursPatternImpl.ROLE_BAD));
sb.append("\nN> " + n);
sb.append("\n________________________________________________");
System.out.println(sb.toString());
}
/**
* Shows if the node is already alive.
* @return true if the node is alive. false in other case.
* @see planet.commonapi.Node#isAlive()
*/
public boolean isAlive() {
return alive;
}
/**
* Having a node destination <b>to</b>, calculates the shortest path to it,
* passing by the <b>a</b> node or <b>b</b> node.
* @param to Destination node.
* @param a The first candidate to arrive to the destination node.
* @param b The second candidate to arrive to the destination node.
* @return The <b>a</b> candidate if has the shortest path to <b>to</b> node,
* or <b> in other case.
*/
private NodeHandle bestOf(NodeHandle to, NodeHandle a, NodeHandle b) {
if (a == null)
return b;
if (b == null)
return a;
double lena = Math.abs(((SymphonyId) to.getId()).getDoubleValue() - ((SymphonyId)a.getId()).getDoubleValue());
double lenb = Math.abs(((SymphonyId) to.getId()).getDoubleValue() - ((SymphonyId)b.getId()).getDoubleValue());
if (lena <= lenb)
return a;
else
return b;
}
/**
* Having a node destination <b>to</b>, calculates the shortest path to it,
* passing by the <b>a</b> node or <b>b</b> node.
* @param to Destination node.
* @param a The first candidate to arrive to the destination node.
* @param it NodeHandle iterator with another candidates to arrive to the
* destination node.
* @return The NodeHandle with the shortest path to destination node.
*/
private NodeHandle bestOf(NodeHandle to, NodeHandle a, Vector values) {
NodeHandle candidate = a;
for (int i = 0; i < values.size(); i++)
{
candidate = bestOf(to, candidate, (NodeHandle) values.get(i));
}
return candidate;
}
/**
* Returns the NodeHandle with the shortest path to the destination node,
* using all internal routing information.
* @param to Destination node.
* @return The best node to route any RouteMessage to the node <b>to</b>.
*/
public NodeHandle route(NodeHandle to) {
if (to.getId().between(id, getSucc().getId())) {
return getSucc();
}
NodeHandle candidate = null;
candidate = bestOf(to, candidate, (Vector)neighbourSet.getSortedSet());
candidate = bestOf(to, candidate, (Vector)outcommingSet);
candidate = bestOf(to, candidate, (Vector)incommingSet);
return candidate;
}
/**
* Sends a RouteMessage with Application level data.
* @param message RouteMessage to be sent with Application level data.
*/
private void sendData(RouteMessage message) {
//planet.results.LinkStateResults.newMessage(message);
//planet.results.LinkStateResults.updateOutcoming(this.id);
sendMessage(message);
Results.incTraffic();
}
/**
* Shows a String representation of this node, showing only its Id.
* @return A String representation of this node.
* @see java.lang.Object#toString()
*/
public String toString() {
return "SymphonyNode(" + id + ")";
}
/**
* Informs if this node is stabilized.
* @return true if the node is stabilized. false in other case.
*/
public boolean isStabilized() {
return fixedNeighbours && statisticStabilized;
}
/**
* Sends to any neighbour the local neighbours. It permits update any new
* acquisition as neighbour. This method is invoked periodically to update
* this information.
*/
private void stabilize() {
if (!modifiedNeighbours) return;
stabilizeVector = (Vector)neighbourSet.getNeighbourSet();
int length = stabilizeVector.size();
for (int i=0; i < length; i++)
{
stabilizeNH = (NodeHandle) stabilizeVector.get(i);
RouteMessage msg = buildMessage(nodeHandle, stabilizeNH, stabilizeNH, SymphonyNode.SET_INFO,
SymphonyNode.REFRESH, NeighbourMessagePool.getMessage(neighbourSet.getSortedSet()));
//LinkStateResults.newMessage(msg);
//LinkStateResults.updateOutcoming(id);
sendMessage(msg);
}
modifiedNeighbours = false;
}
/**
* Simple TimerTask that invoke stabilize() Node method.
*
* @author <a href="mailto: marc.sanchez@estudiants.urv.es">Marc Sanchez</a>
* Date: 07/05/2004
*/
public class StabilizeTask extends TimerTaskImpl {
/**
* Initialize this StabilizeTask.
*/
public StabilizeTask() {
super(true);
}
/**
* Invoke stabilize() method of Node.
*
* @see java.util.TimerTask#run()
*/
public void run() {
super.run();
if (!isFinished()) {
stabilize();
}
}
}
/**
* This method throws a NoSuchMethodError, because the broadcast algorithm
* is not implemented yet.
* @see planet.commonapi.Node#broadcast(java.lang.String,
* planet.commonapi.NodeHandle, planet.commonapi.NodeHandle,
* planet.commonapi.Message)
* @param appId Application identification.
* @param to Source of the message.
* @param nextHop NextHop of the message.
* @param msg Message to be send as broadcast.
*/
public void broadcast(String appId, NodeHandle to, NodeHandle nextHop,
Message msg) {
throw new NoSuchMethodError("Broadcast is not implemented yet.");
}
/**
* The leave protocol is the same as in leave case.
* @see planet.commonapi.Node#fail()
*
*/
public void fail() {
leave();
}
/**
* Returns up to <b>max</b> successors of the actual node.
* @see planet.commonapi.Node#getSuccList(int)
* @param max Maximum number of successors to return.
* @return Up to <b>max</b> successors.
*/
public Vector getSuccList(int max) {
return neighbourSet.getSuccList(max);
}
/**
* Returns a list of nodes that can be used as next hops on a route
* towards key. If is safe, the expected fraction of faulty nodes in the
* list is guaranteed to be no higher than the fraction of
* faulty nodes in the overlay.
* @param key Target key
* @param max Number of next hops.
* @param safe If true, the expected fraction of faulty nodes are the same
* of the nodes in the overlay.
* @return Until a maximum of max nodes to use as next hop.
* @see planet.commonapi.Node#localLookup(planet.commonapi.Id, int, boolean)
*/
public Vector localLookup(Id key, int max, boolean safe) {
Vector toReturn = new Vector();
TreeSet contacts = new TreeSet(new IdComparer(nodeHandle));
contacts.addAll(neighbourSet.getNeighbourSet());
contacts.addAll(incommingSet);
contacts.addAll(outcommingSet);
try {
Set selected = contacts.headSet(GenericFactory.buildNodeHandle(key,true));
if (selected.size()<=max)
toReturn.addAll(selected);
else
{
Iterator it = selected.iterator();
for (int i=0; i< max; i++)
{
toReturn.add(it.next());
}
}
} catch (Exception e) {}
return toReturn;
}
/**
* Obtains an unordered list of up to max nodes that are neighbors
* of the local node.
* @param max Maximum of nodes to return.
* @return Neighbor nodes.
* @see planet.commonapi.Node#neighborSet(int)
*/
public Vector neighborSet(int max) {
int max2 = max/2;
Vector toReturn = neighbourSet.getPredList(max2);
toReturn.addAll(neighbourSet.getSuccList(max2));
return toReturn;
}
/**
* This methods ALWAYS return false. It is not implemented yet.
* <br><br>
* This operation provides information about ranges of keys for
* which the <b>node</b> is currently a root. Returns false if
* the range could not be determined, true otherwise.
* <br><br>
* It is an error to query the range of a node not present in
* the neighbor set.
* <br><br>
* The [leftKey,rightKey] denotes the inclusive range of key values.
* @param node Node that is currently a root of some range of keys.
* @param rank Number of keys that is root the node (rank=rightKey-leftKey).
* @param leftKey The value that appears in the invokation is the candidate left
* key of the range. It may be modified to reflect the correct left margin
* once invokation has finished.
* @param rightKey Shows once the invokation has finished the left margin of
* the range.
* @return true if the range chold be determined; false otherwise, including
* the case of node is not present in the neighbor set returned by neighborSet().
* @see planet.commonapi.Node#range(planet.commonapi.NodeHandle,
* planet.commonapi.Id, planet.commonapi.Id, planet.commonapi.Id)
*/
public boolean range(NodeHandle node, Id rank, Id leftKey, Id rightKey) {
return false;
}
/**
* Returns an ordered set of nodes on which replicas of the object with
* this key can be stored. The maximum number of nodes is maxRank. If
* the implementation's maximum replica set size is lower, then its
* maximum replica set is returned.
* @param key Id for which we find the replica set.
* @param maxRank Maximum number of members in replica set.
* @return An array of nodes with the replica set.
* @see planet.commonapi.Node#replicaSet(planet.commonapi.Id, int)
*/
public Vector replicaSet(Id key, int maxRank) {
return neighbourSet.getSuccList(maxRank);
}
/**
* Returns the NodeHandle closest to <b>id</b>.
* @param id Id to find.
* @return The NodeHandle closest to <b>id</b>.
*/
public NodeHandle getClosestNodeHandle(Id id)
{
try {
return route(GenericFactory.buildNodeHandle(id,true));
} catch (Exception e)
{
return (NodeHandle)neighbourSet.getFirstSucc();
}
}
/**
* Gets all node connections to other nodes as its NodeHandles. The order of
* NodeHandles is unexpected.
*
* @return A set with all connections as NodeHandles.
*/
public Set getAllLinks() {
HashSet conns = new HashSet();
conns.addAll(outcommingSet);
conns.addAll(incommingSet);
conns.addAll(neighbourSet.getNeighbourSet());
//remove the null element if exists
conns.remove(null);
return conns;
}
/* BEGIN ************************ RESULTS ****************************/
/**
* This method is a callback method used to collect all of the edges of a
* graph according to current <b>resultName</b> format.
* @param resultName Result type name to use.
* @param edgeCollection Edge collection where to add new edges.
* @param constraint ResultsConstraint for edge selection
*/
public void buildEdges(String resultName, Collection edgeCollection,
ResultsConstraint constraint) {
if (edgeCollection == null || constraint == null) return;
//neighbours (successors and predecessors)
Iterator it2 = this.neighbourSet.iterator();
addEdges(resultName,it2, constraint, edgeCollection, "#0000FF");
//long distance links
Iterator[] it = new Iterator[2];
it[0] = this.incommingSet.iterator();
it[1] = this.outcommingSet.iterator();
for (int i = 0; i < 2; i++) {
addEdges(resultName, it[i],constraint,edgeCollection,"#FF0000");
}
}
/* END ************************ RESULTS ****************************/
/** Sets the new Id.
* @param newId New Id.
* @return This instance after it has been updated.
* @throws InitializationException if any error has occurred.
* @see planet.commonapi.Node#setValues(planet.commonapi.Id)
*/
public Node setValues(Id newId) throws InitializationException {
super.setValues(newId);
//F is the number of successors ==> F predecessors and F successors
neighbourSet = new SortedKList(new IdComparer(this.nodeHandle), this.nodeHandle, SymphonyNode.getSuccessorsNumber()*2);
return this;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -