⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trivialnode.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package planet.trivialp2p;

import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

import planet.commonapi.Id;
import planet.commonapi.Message;
import planet.commonapi.Node;
import planet.commonapi.NodeHandle;
import planet.commonapi.RouteMessage;
import planet.commonapi.behaviours.BehavioursPool;
import planet.commonapi.exception.InitializationException;
import planet.commonapi.results.ResultsConstraint;
import planet.commonapi.results.ResultsEdge;
import planet.generic.commonapi.NodeImpl;
import planet.generic.commonapi.factory.GenericFactory;
import planet.simulate.Results;
import planet.util.Properties;

/**
 * It is a trivial implementation of a P2P overlay network. Each node only
 * has two links: the predecessor and successor in a ring topology. This
 * overlay not contains stabilization protocol and is assigned under 
 * network building.
 * @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
 * 03-jun-2005
 */
public class TrivialNode extends NodeImpl {

    /* ******************  CONSTANTS FOR MODE OF ROUTEMESSAGE *******/
    /**
     * Mode value: Defines a message's mode that requires a routing task
     * in a oneway communication.
     */
    public final static int REQUEST                 = 0;        
    /**
     * Mode value: Defines a message's mode to be delivered directly in a
     * oneway communication.
     */
    public final static int REFRESH                 = 1;        
    
    /* END ******************  CONSTANTS FOR MODE OF ROUTEMESSAGE *******/
    
    /* ******************  CONSTANTS FOR TYPE OF ROUTEMESSAGE *******/
     /**
      * Type value: It identifies that this message contains an application 
      * level Message. 
      */
     public final static int DATA                     = 0;    

    /* END ******************  CONSTANTS FOR TYPE OF ROUTEMESSAGE *******/
    
    /* ******************  CONSTANTS FOR TYPE/MODE OF ROUTEMESSAGE *******/
    /**
     * This String contains a string representation of each type
     * value of the RouteMessage.
     */
    public final static String[] TYPES = { "DATA" };
    /**
     * This String contains a string representation of each mode
     * value of the RouteMessage.
     */
    public final static String[] MODES = { "REQUEST", "REFRESH" };
    
    /* END **************  CONSTANTS FOR TYPE/MODE OF ROUTEMESSAGE *******/
    
    // Routing table:
    /** The successor of the actual node. */
    private NodeHandle successor;
    /** The predecessor of the actual node. */
    private NodeHandle predecessor;
    /** Contains ALL links of the actual node. */
    private Set links;
    /** Contains the unique node successor. */
    private Vector successors;
    /** If true, the node is already alive. */
    private boolean alive;
    /** The behaviours pool to be used. */
    private BehavioursPool behPool;
    
    
    /* ******************** STARTING IMPLEMENTATION **************************/
    
    /**
     * Initialize the internal structure.
     */
    public TrivialNode() throws InitializationException {
        super();
        alive = true;
        successor = null;
        predecessor = null;
        links = new HashSet(2); 
        successors = new Vector(1);
        if (Properties.overlayWithBehaviours)
            behPool = GenericFactory.getDefaultBehavioursPool();
    }

    /**
     * Nothing does. This implementation don't contain a stabilization protocol.
     * @param bootstrap Bootstrap node.
     * @see planet.commonapi.Node#join(planet.commonapi.NodeHandle)
     */
    public void join(NodeHandle bootstrap) {
    }

    /**
     * Nothing does. Only sets the alive flag to false.
     * @see planet.commonapi.Node#leave()
     */
    public void leave() {
        alive = false;
    }

    /**
     * Gets the internal routing information in a hashtable.
     * The key informs the concept of the related value.
     * @return A hashtable with the internal routing information.
     * @see planet.generic.commonapi.NodeImpl#getInfo()
     */
    public Hashtable getInfo() {
        Hashtable info = new Hashtable();
        info.put("successor",successor);
        info.put("predecessor",predecessor);
        return info;
    }

    /**
     * Returns the own nodehandle or its successor nodehandle, in a
     * clockwise proximity.
     * @param id The id to be find.
     * @return The nearest nodehandle in a clockwise manner.
     * @see planet.commonapi.Node#getClosestNodeHandle(planet.commonapi.Id)
     */
    public NodeHandle getClosestNodeHandle(Id id) {
        return (predecessor.getId().betweenE(predecessor.getId(),this.id)) ?
                    this.nodeHandle :
                    successor;
    }

    /**
     * Routes an application level message to the destination node.
     * @param appId Application name.
     * @param to Destination node (or key).
     * @param nextHop May be null. The next hop into the route.
     * @param msg Application level message to be sent.
     * @see planet.commonapi.Node#routeData(java.lang.String, planet.commonapi.NodeHandle, planet.commonapi.NodeHandle, planet.commonapi.Message)
     */
    public void routeData(String appId, NodeHandle to, NodeHandle nextHop, Message msg) {
        RouteMessage data = buildMessage(GenericFactory.generateKey(),nodeHandle,to,nextHop,DATA,REQUEST,appId,msg);
        if (data!=null)
        {
            Results.incTraffic();
            this.dispatchDataMessage(data,REQUEST,REFRESH);
        }
    }

    /**
     * Do nothing. Only sets to false the alive flag.
     * @see planet.commonapi.Node#fail()
     */
    public void fail() {
        alive = false;
    }

    /**
     * Prints out the routing information of this node.
     * @see planet.commonapi.Node#printNode()
     */
    public void printNode() {
        System.out.println("<Node id=\""+id+"\">");
        System.out.println("   <Successor   id=\""+successor.getId()+"\">");
        System.out.println("   <Predecessor id=\""+predecessor.getId()+"\">");
        System.out.println("</Node>");
    }

    /**
     * Prints out the local node information.
     * @see planet.commonapi.Node#prettyPrintNode()
     */
    public void prettyPrintNode() {
        System.out.println("<Node id=\""+id+"\"/>");
    }

    /**
     * This routing method is not implemented and always throws a 
     * NoSuchMethodError.
     * @param appId Application id that requires to send a broadcast message.
     * @param to Source node.
     * @param nextHop Next hop in the route.
     * @param msg Application level message to be delivered in the broadcast.
     * @see planet.commonapi.Node#broadcast(java.lang.String, planet.commonapi.NodeHandle, planet.commonapi.NodeHandle, planet.commonapi.Message)
     * @throws NoSuchMethodError always this method is invoked.
     */
    public void broadcast(String appId, NodeHandle to, NodeHandle nextHop,
            Message msg) {
        throw new NoSuchMethodError("Method not implemented yet.");
    }

    /**
     * Gets the predecessor nodehandle.
     * @return The predecessor nodeHandle.
     * @see planet.commonapi.Node#getPred()
     */
    public NodeHandle getPred() {
        return predecessor;
    }

    /**
     * Gets the successor nodehandle.
     * @return The successor nodehandle.
     * @see planet.commonapi.Node#getSucc()
     */
    public NodeHandle getSucc() {
        return successor;
    }

    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -