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

📄 node.java

📁 DTNSim2 is a simulator for Delay-Tolerant Networks (DTNs) written in Java. It is based on Sushant Ja
💻 JAVA
字号:
/** * simulator/Node.java */package simulator;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import util.CommandStatus;import util.Verbose;/** * Represents a node in the DTN. */public abstract class Node extends CommandParser implements Comparable{	protected static int lastNodeID = 0;	protected int nodeID = 0;	protected String name = null;	protected HashSet<Contact> contacts = new HashSet<Contact>();	protected HashMap<Integer, ArrayList<Message>> receivedMessages = new HashMap<Integer, ArrayList<Message>>();	protected Stats.StatMsgEntry statMsgEntry = new Stats.StatMsgEntry();	protected boolean useMsgHistory = false;	public Node()	{		super((Network) null);	}	public Node(Node org)	{		super(org);		name = org.name;		useMsgHistory = org.useMsgHistory;		nodeID = ++lastNodeID;	}	public void initNode()	{	}	public Node selfNode()	{		return this;	}	public int compareTo(Object ob)	{		int h0 = this.nodeID;		int h1 = ((Node) ob).nodeID;		if (h0 < h1)			return -1;		else if (h0 > h1)			return 1;		return 0;	}	public String toString()	{		return "NODE_'" + name + "'";	}	public void setName(String nName)	{		name = nName;	}	public String getName()	{		return name;	}	public int compareTo(Node b)	{		return selfNode().getName().compareTo(b.getName());	}	public boolean addContact(Contact contact)	{		return contacts.add(contact);	}	public boolean removeContact(Contact contact)	{		return contacts.remove(contact);	}	public Iterator<Contact> getContacts()	{		return contacts.iterator();	}	public Iterator<Contact> getContactsToNode(Node dst)	{		ArrayList<Contact> ret = new ArrayList<Contact>();		Iterator<Contact> it = getContacts();		while (it.hasNext())		{			Contact c = it.next();			if (c.getDest() == dst)				ret.add(c);		}		return ret.iterator();	}	public boolean receivedMessage(Message msg)	{		return receivedMessage(msg.getId());	}	public boolean receivedMessage(int id)	{		return receivedMessages.containsKey(new Integer(id));	}	/**	 * Called when the contact c connects. This is called if this node is the	 * source or the destination of the contact. If this node is the source, it	 * is permitted to use this contact to send a message. TODO: Can we	 * rearrange it so that it is only possible for the source to send messages	 * on c?	 */	public void contactUp(Contact contact)	{		assert contact.isUp();		if (contact.getSource() != selfNode())			return;		if (network.vShouldLog(Verbose.DEBUG3))			network.vprint("C_UP: " + contact + " in " + selfNode());	}	/**	 * Called when the contact c becomes idle and this node is the source of	 * this contact. This is called once when the contact first comes up, if	 * there are no messages for it to send. It is also called after each	 * message has been sent.	 */	public void contactIdle(Contact contact)	{		assert contact.isUp();		assert contact.isIdle();		assert contact.getSource() == selfNode();		if (network.vShouldLog(Verbose.DEBUG3))			network.vprint("C_IDLE: " + contact + " in " + selfNode());	}	/**	 * Called when the contact c disconnected. This is called if this node is	 * the source or destination of the contact.	 */	public void contactDown(Contact contact)	{		assert !contact.isUp();		assert !contact.isIdle();		if (contact.getSource() != selfNode())			return;		if (network.vShouldLog(Verbose.DEBUG3))			network.vprint("C_DOWN: " + contact + " in " + selfNode());	}	/** Called when the message m arrives via contact c. */	public void messageReceived(Message msg, Contact contact, int size)	{		assert contact.getDest() == selfNode();		if (size < msg.getLength())			return;		if (network.vShouldLog(Verbose.DEBUG4))			network.vprint("RECEIVED " + msg + " by " + contact + " to " + selfNode());		if (useMsgHistory && !msg.isRoutingMessage())		{			msg.addHistory(network.getCurrentTime(), contact);		}		if (!msg.isRoutingMessage() && msg.getDestNode() == selfNode())		{			Integer i = new Integer(msg.getId());			ArrayList<Message> a = receivedMessages.get(i);			if (a == null)			{				a = new ArrayList<Message>();				a.add(msg);				receivedMessages.put(i, a);				if (network.vShouldLog(Verbose.INFO))					network.vprint("DELIVERED_FIRST " + msg + " in " + selfNode() + "; " + contact);				statMsgEntry.setup(msg, network.getCurrentTime(), Stats.MSG_DELIVERED);				network.stats().notify(statMsgEntry);			}			else			{				a.add(msg);				if (network.vShouldLog(Verbose.INFO))					network.vprint("DELIVERED_NEXT " + msg + " in " + selfNode() + "; " + contact);				statMsgEntry.setup(msg, network.getCurrentTime(), Stats.MSG_DELIVERED);				network.stats().notify(statMsgEntry);			}			messageReachedFinalDest(msg, contact, size);			return;		}		if (acceptMessage(msg, contact, size))		{			if (!msg.isRoutingMessage())			{				if (network.vShouldLog(Verbose.DEBUG1))					network.vprint("ACCEPTED: " + msg + " in " + selfNode() + "; " + contact);			}		}		else		{			if (network.vShouldLog(Verbose.DEBUG1))				network.vprint("REFUSED: " + msg + " in " + selfNode() + "; " + contact);		}	}	public CommandStatus parseCommandPart(ArrayList<String> part, String path)	{		String param = part.get(0);		CommandStatus ok = new CommandStatus(CommandStatus.COMMAND_OK);		if (part.size() != 2)			return null;		if (param.equals("msg_history"))		{			useMsgHistory = Boolean.parseBoolean(part.get(1));			return ok;		}		return null;	}	/**	 * Called when the message m is successfully sent via contact c. This should	 * only be used to perform queue maintenance tasks. Sending additional	 * messages should only be done in response to contactIdle.	 */	public void messageSent(Message msg, Contact contact, int size)	{	}	public void messageDelivered(Message msg, Contact contact, int size)	{	}	public void messageReachedFinalDest(Message msg, Contact contact, int size)	{	}	public void defaultLoad()	{	}	public void defaultUnload()	{	}	abstract protected boolean acceptMessage(Message msg, Contact contact, int size);	abstract public boolean sendNewMessage(int dataLength, Node destNode);}

⌨️ 快捷键说明

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