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

📄 agent.java

📁 p2p仿真
💻 JAVA
字号:
/*
 * @(#)Agent.java	ver 1.2  6/20/2005
 *
 * Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu). 
 * All rights reserved.
 * 
 */
 
package gps.protocol;

import java.awt.Graphics;
import java.util.logging.Logger;

import gps.event.SimEvent;
import gps.event.SimEventHandler;
import gps.event.SimEventScheduler;
import gps.network.Topology;
import gps.network.graph.Node;



/**
 * Agent base class, all specific protocol agents inherit from it. 
 *
 *
 * @author  Weishuai Yang
 * @version 1.2,  6/20/2005
 */
public class Agent implements SimEventHandler {
	
	/**
	 * 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();
	/**
	 * provide next available id for automatically indexing, starts from 0
	 */
	protected static int mNextAvailableID = 0;
	/**
	 * active or not
	 */
	protected boolean mActivated = true;
	
	/**
	 * the node attached to
	 */
	protected Node mNode=null;
	
	/**
	 * agent id
	 */
	protected int mAgentID = 0;
	
	/**
	 * constructs a new Agent object with next available id
	 */
	public Agent(){
		this(mNextAvailableID++);
	}
	
	/**
	 * constructs a new Agent object with giving id
	 */
	public Agent(int i){
		if(i+1>mNextAvailableID)
			mNextAvailableID=i+1;
		mAgentID=i;
	}
	
	/**
	 * gets agent id, agent id is different from node id
	 * @return id
	 */
	public int getID(){
		return mAgentID;
	}
	
	/**
	 * attach this agent to a node
	 * @param n the node to be attached
	 */
	public void attachTo(Node n){
		n.attach(this);
		mNode=n;
	}
	
	/**
	 * gets the node that this agent is attached to
	 * @return the node
	 */
	public Node getNode(){
		return mNode;
	}

	/**
	 * do some agent specific drawing on the graph
	 * to be implemented in sub class
	 *
	 */
	public void agentDraw(Graphics g, int x, int y){
	}
	
	/**
	 * check activity
	 * @return true if activated
	 */
	public boolean isActive(){
		return mActivated;
	}

	/**
	 * handles agent level event
	 * @param e the event to be handled
	 * @return true if already handled
	 */
	public boolean handle(SimEvent e){
		if(e.getType()==SimEvent.SIM_AGENT_ACTIVE) {
			//make this node active
			mActivated=true;
			return true;
		}
		else if(e.getType()==SimEvent.SIM_AGENT_DEACTIVE) {
			//make this node inactive
			mActivated=false;
			return true;
		}
		else return mNode.handle(e);
	};

	/**
	 * generates string description
	 * @return string description
	 *
	 */
	public String toString(){
		return "Agent("+mAgentID+"/"+getNode().getID()+")";
	}
	
	/**
	 * dettach from the node
	 */
	public void reset(){
		mNode=null;	
	}
}

⌨️ 快捷键说明

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