2008-10-28 13:52:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-19 12:17:37 +01:00
|
|
|
package org.contikios.cooja;
|
2008-10-28 13:52:24 +01:00
|
|
|
|
|
|
|
/**
|
2009-03-03 14:42:45 +01:00
|
|
|
* @author Joakim Eriksson (ported to COOJA by Fredrik Osterlind)
|
2008-10-28 13:52:24 +01:00
|
|
|
*/
|
|
|
|
public class EventQueue {
|
|
|
|
|
|
|
|
private TimeEvent first;
|
|
|
|
private int eventCount = 0;
|
|
|
|
|
2009-01-08 16:42:25 +01:00
|
|
|
/**
|
|
|
|
* Should only be called from simulation thread!
|
|
|
|
*
|
|
|
|
* @param event Event
|
|
|
|
* @param time Time
|
|
|
|
*/
|
2008-12-04 15:03:41 +01:00
|
|
|
public void addEvent(TimeEvent event, long time) {
|
2008-10-28 13:52:24 +01:00
|
|
|
event.time = time;
|
|
|
|
addEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-10-27 11:06:59 +01:00
|
|
|
private void addEvent(TimeEvent event) {
|
|
|
|
if (event.queue != null) {
|
2010-10-04 12:52:26 +02:00
|
|
|
if (event.isScheduled) {
|
|
|
|
throw new IllegalStateException("Event is already scheduled: " + event);
|
2010-10-04 12:11:55 +02:00
|
|
|
}
|
2010-10-04 12:52:26 +02:00
|
|
|
removeFromQueue(event);
|
2009-10-27 11:06:59 +01:00
|
|
|
}
|
2009-01-08 16:42:25 +01:00
|
|
|
|
2008-10-28 13:52:24 +01:00
|
|
|
if (first == null) {
|
|
|
|
first = event;
|
|
|
|
} else {
|
|
|
|
TimeEvent pos = first;
|
|
|
|
TimeEvent lastPos = first;
|
|
|
|
while (pos != null && pos.time <= event.time) {
|
|
|
|
lastPos = pos;
|
|
|
|
pos = pos.nextEvent;
|
|
|
|
}
|
|
|
|
// Here pos will be the first TE after event
|
|
|
|
// and lastPos the first before
|
|
|
|
if (pos == first) {
|
|
|
|
// Before all other
|
|
|
|
event.nextEvent = pos;
|
|
|
|
first = event;
|
|
|
|
} else {
|
|
|
|
event.nextEvent = pos;
|
|
|
|
lastPos.nextEvent = event;
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 16:42:25 +01:00
|
|
|
event.queue = this;
|
2010-10-04 12:11:55 +02:00
|
|
|
event.isScheduled = true;
|
2008-10-28 13:52:24 +01:00
|
|
|
eventCount++;
|
|
|
|
}
|
|
|
|
|
2009-01-08 16:42:25 +01:00
|
|
|
/**
|
|
|
|
* Should only be called from simulation thread!
|
|
|
|
*
|
|
|
|
* @param event Event
|
|
|
|
* @return True if event was removed
|
|
|
|
*/
|
|
|
|
private boolean removeFromQueue(TimeEvent event) {
|
2008-10-28 13:52:24 +01:00
|
|
|
TimeEvent pos = first;
|
|
|
|
TimeEvent lastPos = first;
|
2009-01-08 16:42:25 +01:00
|
|
|
|
2008-10-28 13:52:24 +01:00
|
|
|
while (pos != null && pos != event) {
|
|
|
|
lastPos = pos;
|
|
|
|
pos = pos.nextEvent;
|
|
|
|
}
|
|
|
|
if (pos == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// pos == event!
|
|
|
|
if (pos == first) {
|
|
|
|
// remove it from first pos.
|
|
|
|
first = pos.nextEvent;
|
|
|
|
} else {
|
|
|
|
// else link prev to next...
|
|
|
|
lastPos.nextEvent = pos.nextEvent;
|
|
|
|
}
|
|
|
|
// unlink
|
|
|
|
pos.nextEvent = null;
|
|
|
|
|
2009-01-08 16:42:25 +01:00
|
|
|
event.queue = null;
|
2010-10-04 12:11:55 +02:00
|
|
|
event.isScheduled = false;
|
2008-10-28 13:52:24 +01:00
|
|
|
eventCount--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-08 16:42:25 +01:00
|
|
|
public void removeAll() {
|
|
|
|
TimeEvent event = popFirst();
|
|
|
|
while (event != null) {
|
|
|
|
event = popFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should only be called from simulation thread!
|
|
|
|
*
|
|
|
|
* @return Event
|
|
|
|
*/
|
2008-10-28 13:52:24 +01:00
|
|
|
public TimeEvent popFirst() {
|
|
|
|
TimeEvent tmp = first;
|
|
|
|
if (tmp == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = tmp.nextEvent;
|
|
|
|
// Unlink.
|
|
|
|
tmp.nextEvent = null;
|
|
|
|
|
|
|
|
// No longer scheduled!
|
2009-01-08 16:42:25 +01:00
|
|
|
tmp.queue = null;
|
2008-10-28 13:52:24 +01:00
|
|
|
eventCount--;
|
|
|
|
|
2010-10-04 12:11:55 +02:00
|
|
|
if (!tmp.isScheduled) {
|
2009-01-08 16:42:25 +01:00
|
|
|
/* pop and return another event instead */
|
|
|
|
return popFirst();
|
2008-10-28 13:52:24 +01:00
|
|
|
}
|
2010-10-04 12:11:55 +02:00
|
|
|
tmp.isScheduled = false;
|
2009-01-08 16:42:25 +01:00
|
|
|
return tmp;
|
2008-10-28 13:52:24 +01:00
|
|
|
}
|
|
|
|
|
2009-05-26 15:36:36 +02:00
|
|
|
public TimeEvent peekFirst() {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2008-10-28 13:52:24 +01:00
|
|
|
public String toString() {
|
2009-10-27 11:06:59 +01:00
|
|
|
return "EventQueue with " + eventCount + " events";
|
2008-10-28 13:52:24 +01:00
|
|
|
}
|
2009-01-08 16:42:25 +01:00
|
|
|
}
|