Skip to content

Commit

Permalink
The WallTimeTimer reprogrammed. Now it takes into account the time of
Browse files Browse the repository at this point in the history
code execution and the sleep. Added support for insertion of an event
that needs to be executed as soon as possible.
Added support for Bee Click device.
Added support for turtlebots navigation using "move base" topic.
  • Loading branch information
iridin committed Jul 14, 2015
1 parent 5e213ce commit 25434a8
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public void notifyAt(long time, TimerEventListener listener, DEECoContainer cont
}
}

@Override
public void interruptionEvent(TimerEventListener listener, DEECoContainer container){
throw new UnsupportedOperationException();
// If you are about to implement this method remember to use synchronization
}

boolean tryToTerminate() {
if (eventTimes.isEmpty()) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/timer/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface Timer extends CurrentTimeProvider {
* Node to schedule the even at
*/
void notifyAt(long time, TimerEventListener listener, DEECoContainer node);

void interruptionEvent(TimerEventListener listener, DEECoContainer node);
}
127 changes: 108 additions & 19 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/timer/WallTimeTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,131 @@

import cz.cuni.mff.d3s.deeco.runtime.DEECoContainer;


/**
* {@link WallTimeTimer} holds a queue of events waiting to be executed and the
* current time of the DEECo node. The time starts at 0 and is being increased
* according to the {@link System} time.
*
* @author Ilias Gerostathopoulos <iliasg@d3s.mff.cuni.cz>
* @author Dominik Skoda <skoda@d3s.mff.cuni.cz>
*/
public class WallTimeTimer implements RunnerTimer {

Queue<EventTime> eventTimes;
long currentTime;

protected final Queue<EventTime> eventTimes;
protected long startTime;
protected long currentTime;

public WallTimeTimer() {
startTime = 0;
currentTime = 0;
eventTimes = new PriorityQueue<>();
}


/**
* Returns the number of milliseconds since the {@link #start()} method was
* invoked. If the {@link #start()} method was not called before the call to
* this method then the return value is 0.
*
* @return The number of milliseconds since the {@link #start()} method was
* invoked.
*/
@Override
public long getCurrentMilliseconds() {
if(startTime == 0){
// Before the start() method is called always return 0
return 0;
}

// Adjust the time to return precise value
adjustCurrentTime();

return currentTime;
}

public void start() {

// Save the current time as the start time of the timer
startTime = System.currentTimeMillis();
while (true) {
EventTime eventTime = eventTimes.remove();
long nextEventTime = eventTime.getTimePoint();
try {
Thread.sleep(nextEventTime-currentTime);
currentTime = nextEventTime;
} catch (InterruptedException e) {
e.printStackTrace();
// The next event to be processed. Assigned when its execution time
// arises
EventTime eventToProcess = null;

synchronized (eventTimes) {
final EventTime nextEvent = eventTimes.peek();
if (nextEvent == null) {
// TODO: log the state and exit
throw new IllegalStateException(
"No event found in the queue in the WallTimeTimer.");
}

// Get the time of the next event
final long nextTime = nextEvent.getTimePoint();
try {
// Wait till the time of the next event
if (nextTime > currentTime) {
eventTimes.wait(nextTime - currentTime);
} // TODO: else log planned time miss
// Take the next event to be executed. Interruption events
// notify the wait before the timeout expires
eventToProcess = eventTimes.poll();
} catch (InterruptedException e) {
// Ignore interruptions
adjustCurrentTime();
continue;
}
}

// eventToProcess should never be null since the nextEvent wasn't
// null either. The at() method gets its planned time of execution.
eventToProcess.getListener().at(eventToProcess.getTimePoint());

// Adjust the current time after the event execution
adjustCurrentTime();
}
}

@Override
public void notifyAt(long time, TimerEventListener listener,
DEECoContainer container) {
synchronized (eventTimes) {
EventTime eventTime = new EventTime(time, listener, false);
if (!eventTimes.contains(eventTime)) {
eventTimes.add(eventTime);
}
eventTime.getListener().at(currentTime);
}
}

/**
* Add new event at the beginning of the event queue. Notify the timer about
* the new event.
*
* @param listener
* The event listener to be invoked at its specific time.
* @param node
* The DEECoContainer is not used in this method.
*/
@Override
public void notifyAt(long time, TimerEventListener listener, DEECoContainer container) {
EventTime eventTime = new EventTime(time, listener, false);
if (!eventTimes.contains(eventTime)) {
eventTimes.add(eventTime);
public void interruptionEvent(TimerEventListener listener,
DEECoContainer node) {
synchronized (eventTimes) {
EventTime event = new EventTime(currentTime, listener, false);
eventTimes.add(event);
eventTimes.notify();
}
}

/**
* Synchronize the {@link #currentTime} by the difference of
* {@link System#currentTimeMillis()} and lastSystemTime.
*
* @param lastSystemTime
* The last measured {@link System#currentTimeMillis()} before a
* time-consuming operation.
*/
protected void adjustCurrentTime() {
final long currentSystemTime = System.currentTimeMillis();

// Compute the amount of elapsed time since the timer was started.
currentTime = currentSystemTime - startTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ public long getCurrentMilliseconds() {

@Override
public void notifyAt(long time, TimerEventListener listener, DEECoContainer node) {
throw new UnsupportedOperationException("Fakt timer dows not support notifications");
throw new UnsupportedOperationException("Fake timer does not support notifications");
}

@Override
public void interruptionEvent(TimerEventListener listener,
DEECoContainer node) {
throw new UnsupportedOperationException("Fake timer does not support interruptionEvent");
}
});
return scheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public void notifyAt(long time, TimerEventListener listener, DEECoContainer node
callAt(time, node.getId());
MATSimSimulation.this.getHost(node.getId()).listener = listener;
}

@Override
public void interruptionEvent(TimerEventListener listener,
DEECoContainer node) {
throw new UnsupportedOperationException("The Timer for MATSim simulation does not support interruptionEvent");
}

@Override
public synchronized void callAt(long absoluteTime, int hostId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public long getCurrentMilliseconds() {
public void notifyAt(long time, TimerEventListener listener, DEECoContainer node) {
throw new UnsupportedOperationException();
}

@Override
public void interruptionEvent(TimerEventListener listener,
DEECoContainer node) {
throw new UnsupportedOperationException("Fake timer does not support interruptionEvent");
}
};
}

Expand Down
5 changes: 5 additions & 0 deletions jdeeco-ros/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
<artifactId>sensor_msgs</artifactId>
<version>1.11.7</version>
</dependency>
<dependency>
<groupId>beeclickarmROS</groupId>
<artifactId>beeclickarmROS</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Loading

0 comments on commit 25434a8

Please sign in to comment.