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

📄 eventsender.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.PriorityQueue;public abstract class EventSender implements EventReceiver{	protected Network network = null;	private PriorityQueue<Event> localEventQueue = new PriorityQueue<Event>(10, new Event.EventComparator());	private Event scheduledEvent = null;	private Event nextEvent = null;	private boolean useEventQueue = false;	private boolean noGlobalScheduling = false;	public EventSender(Network net)	{		network = net;	}	public EventSender(EventSender org)	{		network = org.network;		useEventQueue = org.useEventQueue;	}	public final void useLocalEventQueue(boolean b)	{		useEventQueue = b;	}	public final boolean useLocalEventQueue()	{		return useEventQueue;	}	public final void setNetwork(Network n)	{		network = n;	}	public final Network getNetwork()	{		return network;	}	protected abstract void receiveLocalEvent(Event event);	public final void receiveEvent(Event event)	{		if (!useEventQueue)		{			receiveLocalEvent(event);			return;		}		if (event == scheduledEvent)		{			scheduledEvent = null;			noGlobalScheduling = true;		}		receiveLocalEvent(event);		noGlobalScheduling = false;		if (scheduledEvent == null)		{			scheduleNextEvent();		}	}	public void cancelEvent(Event ev)	{		if (scheduledEvent == ev)		{			network.cancelGlobalEvent(ev);			scheduledEvent = null;			scheduleNextEvent();			return;		}		if (nextEvent == ev)		{			// One of theese:			// nextEvent = null;			nextEvent = localEventQueue.poll();			return;		}		// The event was in the local queue!		if (localEventQueue.remove(ev))		{			return;		}		network.cancelGlobalEvent(ev);	}	private void scheduleNextEvent()	{		assert scheduledEvent == null;		if (nextEvent != null)		{			scheduledEvent = nextEvent;			// One of theese:			// nextEvent = null;			nextEvent = localEventQueue.poll();		}		else		{			// Take the next event from local queue to schedule			// it...			scheduledEvent = localEventQueue.poll();		}		if (scheduledEvent != null)		{			network.scheduleGlobalEvent(scheduledEvent);		}	}	public void scheduleEvent(Event ev)	{		assert ev != null;		assert ev.getReceiver() == this;		// We are not using local event queue		// - just use global queue.		if (!useEventQueue)		{			network.scheduleGlobalEvent(ev);			return;		}		// There is something already scheduled in the main queue,		// and new event should be executed BEFORE		// the event already scheduled. We can either remove that		// previously scheduled event from the main queue,		// or just add new one without changing that event.		// The second approach is simpler, and this should not happen		// often (at all?).		if (scheduledEvent != null && ev.compareWith(scheduledEvent) < 0)		{			network.scheduleGlobalEvent(ev);			// But we leave scheduled event as it was!			return;		}		// We don't want to schedule anything. Either because there is		// already something scheduled, or we don't want to do global scheduling		if (scheduledEvent != null || noGlobalScheduling)		{			// There is next Event set (to be scheduled in a moment?)			if (nextEvent != null)			{				// New event schould be fired before nextEvent				// - we add next event to our local queue, and set				// the new one as a next event.				if (ev.compareWith(nextEvent) < 0)				{					localEventQueue.add(nextEvent);					nextEvent = ev;				}				// There is next event set, but the new one should				// be fired after that. Just add it to local queue.				else				{					localEventQueue.add(ev);				}			}			// There is no nextEvent set			else			{				// If the local queue is empty, we can just set next event.				if (localEventQueue.size() < 1)				{					nextEvent = ev;				}				else				{					// Otherwise we can only add the event to a queue					// - we have no idea, if there are any events in a queue,					// which should be fired before new event - so					// we can't set nextEvent!					localEventQueue.add(ev);				}			}			return;		}		// We want to schedule something. scheduledEvent is null,		// and we do want to have global scheduling!		// There is no nextEvent set		if (nextEvent == null)		{			// If the local queue is empty, we can just schedule new event..			if (localEventQueue.size() < 1)			{				scheduledEvent = ev;				network.scheduleGlobalEvent(scheduledEvent);			}			else			{				// Otherwise we have to add the event to a queue				// - we have no idea, if the events already in the queue				// should be fired before or after new event.				localEventQueue.add(ev);				scheduleNextEvent();			}			return;		}		// There is next Event set - to be scheduled in a moment		// New event should be fired before nextEvent		if (ev.compareWith(nextEvent) < 0)		{			// We just schedule new event and leave nextEvent as it was.			// This way we don't have to touch local queue.			scheduledEvent = ev;			network.scheduleGlobalEvent(scheduledEvent);		}		else		{			localEventQueue.add(ev);			scheduleNextEvent();		}	}}

⌨️ 快捷键说明

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