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

📄 basiccontact.java

📁 DTNSim2 is a simulator for Delay-Tolerant Networks (DTNs) written in Java. It is based on Sushant Ja
💻 JAVA
字号:
package simulator;import java.util.ArrayList;import util.CommandStatus;import util.Verbose;public class BasicContact extends Contact{	protected double latency = 1;	protected double bandwidth = 1;	protected boolean abortTransfers = false;	protected boolean isUp = false;	protected boolean isIdle = false;	protected Message messageToSend = null;	protected BasicEvent sendEvent = null;	public BasicContact()	{	}	public BasicContact(BasicContact org)	{		super(org);		latency = org.latency;		bandwidth = org.bandwidth;	}	public void setBandwidth(double b)	{		bandwidth = b;	}	public void setLatency(double l)	{		latency = l;	}	public double getBandwidth()	{		return bandwidth;	}	public double getLatency()	{		return latency;	}	public boolean isUp()	{		return isUp;	}	public boolean isIdle()	{		return isIdle;	}	public boolean sendMessage(Message msg)	{		if (!isUp || !isIdle)			return false;		assert messageToSend == null;		assert sendEvent == null;		isIdle = false;		BasicEvent e = new BasicEvent(timeToSend(msg), this, CONTACT_MSG_SENT, msg);		messageToSend = msg;		if (network.vShouldLog(Verbose.DEBUG3))			network.vprint("C_MSG_SENDING: " + msg + " by " + this + " Size: " + msg.getLength() + " time: "					+ getTransferTime(msg));		sendEvent = e;		scheduleEvent(e);		return true;	}	public double getTransferTime(Message msg)	{		return msg.getLength() / bandwidth;	}	private double timeToSend(Message msg)	{		double ret = network.getCurrentTime();		ret += getTransferTime(msg);		return ret;	}	protected double timeToDeliver(Message msg)	{		return network.getCurrentTime() + latency;	}	protected void receiveLocalEvent(Event event)	{		BasicEvent s = (BasicEvent) event;		processEvent(s.getType(), s);	}	public boolean abortSending()	{		if (!abortTransfers || !isUp || isIdle || messageToSend == null)			return false;		isIdle = true;		if (sendEvent != null)		{			cancelEvent(sendEvent);			sendEvent = null;		}		assert messageToSend != null;		if (network.vShouldLog(Verbose.DEBUG3))			network.vprint("C_ABORT: " + messageToSend + " in " + this);		srcNode.messageSent(messageToSend, this, 0);		messageToSend = null;		return true;	}	protected void processEvent(int evType, BasicEvent event)	{		assert srcNode != null && dstNode != null;		Message msg = null;		if (event != null)			msg = event.getMessage();		switch (evType)		{			case CONTACT_UP:				if (isUp)					return;				isUp = true;				if (network.vShouldLog(Verbose.NOTIFY))					network.vprint("C_UP: " + this);				// TODO - Do we have to inform dstNode about contact-up?				dstNode.contactUp(this);				srcNode.contactUp(this);				isIdle = true;				srcNode.contactIdle(this);				break;			case CONTACT_DOWN:				if (!isUp)					return;				isUp = false;				isIdle = false;				if (network.vShouldLog(Verbose.NOTIFY))					network.vprint("C_DOWN: " + this);				if (sendEvent != null)				{					if (network.vShouldLog(Verbose.DEBUG4))						network.vprint("C_DOWN_CANCEL_SEND_EVENT: " + sendEvent + " in " + this);					cancelEvent(sendEvent);					sendEvent = null;				}				if (messageToSend != null)				{					if (network.vShouldLog(Verbose.DEBUG3))						network.vprint("C_DOWN_SEND_ABORT: " + messageToSend + " in " + this);					srcNode.messageSent(messageToSend, this, 0);					messageToSend = null;				}				// TODO - Do we have to inform dstNode about contact-down?				dstNode.contactDown(this);				srcNode.contactDown(this);				break;			case CONTACT_MSG_SENT:				assert msg != null;				assert isUp;				assert messageToSend != null;				isIdle = true;				assert messageToSend == msg;				assert sendEvent == event;				sendEvent = null;				messageToSend = null;				BasicEvent e = new BasicEvent(timeToDeliver(msg), this, CONTACT_MSG_DELIVERED, msg);				scheduleEvent(e);				if (network.vShouldLog(Verbose.DEBUG3))					network.vprint("C_MSG_SENT: " + msg + " by " + this);				statCntEntry.setup(network.getCurrentTime(), Stats.CNT_MSG_SENT, this, msg);				network.stats().notify(statCntEntry);				// SENT should be BEFORE RECEIVED & IDLE				srcNode.messageSent(msg, this, msg.getLength());				srcNode.contactIdle(this);				break;			case CONTACT_MSG_DELIVERED:				assert msg != null;				if (network.vShouldLog(Verbose.DEBUG3))					network.vprint("C_MSG_DELIVERED: " + msg + " by " + this);				dstNode.messageReceived(msg, this, msg.getLength());				srcNode.messageDelivered(msg, this, msg.getLength());				break;		}	}	public boolean contactUp()	{		if (isUp)			return false;		processEvent(CONTACT_UP, null);		return true;	}	public boolean contactDown()	{		if (!isUp)			return false;		processEvent(CONTACT_DOWN, null);		return true;	}	public CommandStatus parseCommandPart(ArrayList<String> part, String path)	{		String param = part.get(0);		CommandStatus ok = new CommandStatus(CommandStatus.COMMAND_OK);		// Parameters without arguments:		if (param.equals("up"))		{			contactUp();			return ok;		}		else if (param.equals("down"))		{			contactDown();			return ok;		}		// Parameters with one argument (any type):		if (part.size() != 2)			return null;		try		{			if (param.equals("bandwidth"))			{				bandwidth = Double.parseDouble(part.get(1));				return ok;			}			else if (param.equals("latency"))			{				latency = Double.parseDouble(part.get(1));				return ok;			}		}		catch (NumberFormatException e)		{			return new CommandStatus("Error parsing value of parameter '" + param + "': " + e);		}		return null;	}}

⌨️ 快捷键说明

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