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

📄 protocol.java

📁 一个p2p仿真软件
💻 JAVA
字号:
/*
 * @(#)Protocol.java	ver 1.2  6/20/2005
 *
 * Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu). 
 * All rights reserved.
 * 
 */

package gps.protocol;

import gps.event.SimEventScheduler;
import gps.network.Topology;

import java.util.Properties;
import java.util.logging.Logger;

/**
 * Protocol base class, all specific protocols inherit from it. 
 *
 *
 * @author  Weishuai Yang
 * @version 1.2,  6/20/2005
 */
public class Protocol {
	/**
	 * singleton reference to topology
	 */
	protected static Topology mTopology=Topology.getInstance();
	/**
	 * singleton reference to debug log
	 */
	protected static Logger mDebugLog = Logger.getLogger("Debug");
	/**
	 * singleton reference to trace log
	 */
	protected static Logger mTraceLog = Logger.getLogger("Trace");
	/**
	 * singleton reference to event scheduler
	 */
	protected static SimEventScheduler mScheduler=SimEventScheduler.getInstance();
	
	
	/**
	 * protocol configuration path
	 */
	protected String mProtocolConf=null;
	
	/**
	 * simulator properties
	 */
	protected Properties mSimulatorProperties=null;
	
	/**
	 * protocol properties
	 */
	protected Properties mProtocolProperties=new Properties();
	/**
	 * agent list under this protocol
	 */
	protected Agent[] mAgent=null;
	
	/**
	 * agent number
	 */
	protected int mTotalAgentNum=0;	
	
	/**
	 * peer number
	 */
	protected int mPAgentNum=0;
	
	/**
	 * server number
	 */
	protected int mSAgentNum=0;
	
	/**
	 * dummy constructor
	 */
	public Protocol(){
		/* set default config file path in specific class implementation*/
		//mProtocolConf="config/myprotocol.cfg";
	}
	
	/**
	 * resets status
	 */
	public void reset(){
		//mProtocolProperties=new Properties();
	    
	    if(mAgent!=null)
            for(int i=0;i <mAgent.length;i++){ 
                if(mAgent[i]!=null)
                    mAgent[i].reset();
            }

		mAgent=null;
		mTotalAgentNum=0;	
		mPAgentNum=0;
		mSAgentNum=0;
	}
	/**
	 * loads protocol properties, to be overriden
	 * @param path path to the configuration file
	 */
	public void loadProtocolProperties(String path){
		//to be implemented in protocol file
		//for example, BT:
    	//mProtocolConf="config/bt.cfg";
	    //mProtocolProperties.clear();
		//Config.loadConfFromFile(path, mProtocolProperties);
    	//int nodeNum=Integer.parseInt(mProtocolProperties.getProperty("BTTrackers"))+Integer.parseInt(mProtocolProperties.getProperty("BTPeers"));
		//mProtocolProperties.setProperty("TotalNodeNum", ""+nodeNum);
	}
    
	/**
	 * gets simulator properties
	 * @return simulator properties
	 */
    public Properties getSimulatorProperties(){
	    return mSimulatorProperties;
	}
	/**
	 * sets simulator properties
	 * @param p simulator properties
	 */
	public void setSimulatorProperties(Properties p){
	    mSimulatorProperties = p;
	}

	/**
	 * gets protocol properties
	 * @return protocol properties
	 */
    public Properties getProtocolProperties(){
	    return mProtocolProperties;
	}
	/**
	 * sets protocol properties
	 * @param p protocol properties
	 */
	public Properties setProtocolProperties(Properties p){
	    return mProtocolProperties = p;
	}
	
	/**
	 * gets delay between two agents
	 *
	 * @param agent1 source agent id
	 * @param agent2 destination agent id
	 */
	public double getDelayAgent(int agent1, int agent2){
		if(agent1>mTotalAgentNum||agent2>mTotalAgentNum){
			mDebugLog.severe("Agent "+agent1+ " or Agent "+agent2+"exceeds total agent number!");
			return 0;
		}
		return mTopology.getDelay(mAgent[agent1].getNode(), mAgent[agent2].getNode());
	}

	/**
	 * gets delay between two agents
	 *
	 * @param agent1 source agent
	 * @param agent2 destination agent
	 */
	public double getDelayAgent(Agent agent1, Agent agent2){
		return getDelayAgent(agent1.getID(), agent2.getID());
	}

	/**
	 * gets the agent instance from it's id
	 *
	 * @param id agent id
	 * 
	 */
	public Agent getAgent(int id){
		if(id>mTotalAgentNum){
			mDebugLog.severe("Node "+id+ "exceeds total node number!");
			return null;
		}	
		if(mAgent[id].getID()!=id)
			mDebugLog.severe("ID retrieved from "+mAgent[id]+"doesn't match"+id);
		
		return mAgent[id];
	}
	
	/**
	 * gets agent object list
	 *
	 * @return array of agents
	 * 
	 */
	public Agent[] getAgentList(){
		return mAgent;
	}
	/**
	 * sets agent number
	 *
	 * @param num new agent number
	 * 
	 */
	public void setAgentNum(int num){
		mTotalAgentNum=num;
		mAgent=new Agent[mTotalAgentNum+1];
	}
	/**
	 * sets an agent object at the specified locaiton
	 *
	 * @param index the index of the agent
	 * @param a agent object
	 * 
	 */
	public void setAgent(int index, Agent a){
		mAgent[index]=a;
	}
	
	
	/**
	 * sets the agent number in this topology, this method is used for p2p networks with servers and peers
	 *
	 * @param snum is number of peer agents
	 * @param pnum is number of server agents
	 *
	 */
	public void setAgentNum(int snum, int pnum){
		mPAgentNum=pnum;
		mSAgentNum=snum;
		mTotalAgentNum=pnum+snum;
		mAgent=new Agent[mTotalAgentNum+1];
		//the initialization of node is done outside of topology
		//mNode[0] is null
	}
	/**
	 * sets an sagent object at the specified locaiton
	 *
	 * @param index the index of the sagent
	 * @param s sagent object
	 * 
	 */
	public void setSAgent(int index, Server s){
		mAgent[index]=s;
	}
	/**
	 * sets an pagent object at the specified locaiton
	 *
	 * @param index the index of the pagent
	 * @param p pagent object
	 * 
	 */
	public void setPAgent(int index, Peer p){
		mAgent[index]=p;
	}
	/**
	 * configure documents for this protocol, to be overriden
	 * 
	 */
	public void confDocuments(){}
	
	/**
	 * configure events for this protocol, to be overriden
	 * 
	 */
	public void confEvents(){}
	/**
	 * configure agents for this protocol, to be overriden
	 * 
	 */
	public void confAgents(){}

}

⌨️ 快捷键说明

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