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

📄 simevent.java

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


package gps.event;
import java.util.ArrayList;

import gps.util.LogFormatter;

/**
 * the base class of all events in simulator. events 
 * includes simulator internal events, protocol events, 
 * and user action events.
 *
 * @author  Weishuai Yang
 * @version 1.2, 6/20/2005
 */
public class SimEvent implements Comparable {
	
	/**
	 * start simulation event, don't have to be explicitely triggered
	 */
	public static final int SIM_START=0;
	/**
	 * end simulation event
	 */
	public static final int SIM_END=1;
	/**
	 * activate a node 
	 */
	public static final int SIM_NODE_ACTIVE=2;
	/**
	 * deactivate a node
	 */
	public static final int SIM_NODE_DEACTIVE=3;

	/**
	 * activate an agent 
	 */
	public static final int SIM_AGENT_ACTIVE=4;
	/**
	 * deactivate an agent
	 */
	public static final int SIM_AGENT_DEACTIVE=5;
	/**
	 * next available event id
	 */
	private static long NEXTID=1;
	/**
	 * time stamp for this event to be triggered
	 */
	protected double mTimeStamp = 0;

	/**
	 * a list of handlers	
	 */
	protected ArrayList mHandlerList = null;
	/**
	 * event type
	 */
	protected int mType=0;
	
	/**
	 * major parameter for that event
	 */
	protected Object mParam = null;
	/**
	 * Additional param if one  not enough
	 */
	protected Object mAddParam=null;
	/**
	 * event id
	 */
	protected long mEventID=0;
	
	/**
	 * don't do anything, just for complete
	 */
	public SimEvent() { }
	
	/**
	 * constructs a new event with one handler
	 *
	 * @param timeStamp  the time this event to be triggered
	 * @param type  the event type
	 * @param obj  the paramater for this event
	 * @param handler  the handler for this event
	 */
	public SimEvent(double timeStamp, int type, SimEventHandler handler, Object obj) {
		mTimeStamp=timeStamp;
		mType=type;
		mHandlerList= new ArrayList(1);
		if(handler!=null)
			mHandlerList.add(handler);
		mParam=obj;
	}

	/**
	 * constructs a new event with a list of handler
	 *
	 * @param timeStamp  the time this event to be triggered
	 * @param type  the event type
	 * @param obj  the paramater for this event
	 * @param handlerList  the handler for this event
	 */
	public SimEvent(double timeStamp, int type, ArrayList handlerList, Object obj) {
		mTimeStamp=timeStamp;
		mType=type;
		mHandlerList=handlerList;
		mParam=obj;
	}

	/**
	 * gets the unique id for this event
	 * @return id of the event
	 */
	public long getEventID(){
	    return mEventID;
	}
	
	
	/**
	 * sets next available unique id for this event
	 */
	public void setEventID(){
		  mEventID=NEXTID++;
	}


	/**
	 * dispatchs the event to it's handler
	 */
	public void dispatch() {
		
		for (int index = 0; index < mHandlerList.size(); index++) {
		
			((SimEventHandler)(mHandlerList.get(index))).handle(this);
		}
	}
	
	/**
	 * gets the timestamp of the event
	 * @return timestamp
	 */
	public double getTimeStamp(){
		return mTimeStamp;	
	}

	/**
	 * sets timestamp
	 * @param t new timestamp
	 * 
	 */
	public void setTimeStamp(double t){
		mTimeStamp=t;
	}


	/**
	 * gets first param for the event
	 * @return the first param object of the event
	 * 
	 */
	public Object getParam(){
		return mParam;	
	}



	/**
	 * sets the second param for the event
	 * @param obj the object to be included int the event as the second parameter
	 * 
	 */
	
	public void setAddParam(Object obj){
		mAddParam=obj;
	}
	
	
	/**
	 * gets the second param for the event
	 * @return the second param object of the event
	 * 
	 */
	public Object getAddParam(){
		return mAddParam;
	}
	

	/**
	 * compare the order of two events
	 * @param event the events to be compared to
	 */
	public int compareTo(Object event){
		//only if they are exactly the same object, return 0;
	    if(this==event) return 0;
		//since timeStamp  double rather than int, can't simply return the difference.
	    double diff=mTimeStamp-((SimEvent)event).getTimeStamp();
		if(diff<0) return -1;
		else if(diff==0) return (int)(mEventID-((SimEvent)event).getEventID());
		else return 1;
	}
	
	/**
	 * gets event type
	 * @return event type
	 */
	public int getType(){
		return mType;
	}
	
	/**
	 * sets event type
	 * @param type  the new event type
	 */
	public void setType(int type){
		mType=type;
	}
	
	/**
	 * generates a String description of the type
	 * @return event type in string format 
	 */
	public String typeString(int type){
		
		switch(type){
			// SIM_START don't have to be explicitely triggered
			case SIM_START: return "SIM_START";
			case SIM_END: return "SIM_END";
			case SIM_NODE_ACTIVE: return "SIM_NODE_ACTIVE";
			case SIM_NODE_DEACTIVE: return "SIM_NODE_DEACTIVE";
			case SIM_AGENT_ACTIVE: return "SIM_AGENT_ACTIVE";
			case SIM_AGENT_DEACTIVE: return "SIM_AGENT_DEACTIVE";
		}
		return "UNKNOWN";
	}


	/**
	 * generates a string representation of the event
	 *
	 * @return a string representation of the event
	 */
	public String toString(){
		int index =0;
		StringBuffer sb=new StringBuffer();
		
		sb.append(mTimeStamp+" "+typeString(mType)+": Handlers:(");
		
		for (index = 0; index < mHandlerList.size(); index++) {
			if((SimEventHandler)mHandlerList.get(index)!=null){
			    sb.append((SimEventHandler)mHandlerList.get(index));
			    sb.append(";");
			}
		}
		/*
		if((SimEventHandler)mHandlerList.get(mHandlerList.size()-1)!=null)
		    sb.append((SimEventHandler)mHandlerList.get(mHandlerList.size()-1)+") ");
		else
		    */
		    sb.append(") ");
		sb.append("Source: "+mParam);
		
		return sb.toString();
	}
	
	/**
	 * generate a formated string with basic event information and 
	 * additional information provided by parameter
	 *
	 * @param ss the additional information to be included in the formated string
	 * @return formated String
	 */

    public String format(String ss){
        StringBuffer s=new StringBuffer();
        SimEventScheduler scheduler=SimEventScheduler.getInstance();
        
        s.append(LogFormatter.sprintf("%.9f", scheduler.getCurrent()));
        s.append(" "+ss+" ");
        s.append(LogFormatter.sprintf("%-7d", mEventID));

        s.append(LogFormatter.sprintf("%-35s", typeString(mType)));
        s.append(" ");

        StringBuffer sb=new StringBuffer(":");

		for (int index = 0; index < mHandlerList.size(); index++) {
			sb.append((SimEventHandler)mHandlerList.get(index));
			sb.append(":");
		}
        
		s.append(LogFormatter.sprintf("%-35s", sb.toString()));
        
        
        if(mParam!=null)
            s.append(LogFormatter.sprintf("%-25s", mParam.toString()));
        
        //s.append(" - ");
        //if(mAddParam!=null)
        //    s.append(mAddParam);

        return s.toString();
    }
	
	

}

⌨️ 快捷键说明

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