This commit is contained in:
47
java/src/pm/event/EventListener.java
Normal file
47
java/src/pm/event/EventListener.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package pm.event;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import pm.Listener;
|
||||
import pm.event.task.Continuous;
|
||||
import pm.value.Action;
|
||||
|
||||
public abstract class EventListener extends Listener implements Runnable {
|
||||
protected Queue<Task> taskQueue;
|
||||
|
||||
public EventListener() {
|
||||
taskQueue = new ConcurrentLinkedQueue<Task>();
|
||||
}
|
||||
|
||||
public final void run() {
|
||||
while (run) {
|
||||
if (taskQueue.isEmpty()) {
|
||||
sleep();
|
||||
} else {
|
||||
task(taskQueue.poll());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void add(Task task) {
|
||||
taskQueue.add(task);
|
||||
}
|
||||
|
||||
protected void task(Task task) {
|
||||
Action action = task.getAction();
|
||||
if (task instanceof Continuous) {
|
||||
Continuous continuous = (Continuous) task;
|
||||
do {
|
||||
action(action);
|
||||
continuous.nextIteration();
|
||||
sleep(continuous.getSleep());
|
||||
} while (run && !continuous.getStop());
|
||||
continuous.reset();
|
||||
} else {
|
||||
action(action);
|
||||
}
|
||||
}
|
||||
|
||||
protected void action(Action action) {}
|
||||
}
|
||||
75
java/src/pm/event/EventManager.java
Normal file
75
java/src/pm/event/EventManager.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package pm.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import pm.Application;
|
||||
import pm.Device;
|
||||
import pm.Main;
|
||||
import pm.application.ApplicationCycle;
|
||||
import pm.event.task.Stopper;
|
||||
import pm.value.Target;
|
||||
|
||||
public class EventManager {
|
||||
protected static ArrayList<EventListener> taskListenerList;
|
||||
protected static ApplicationCycle applicationCycle;
|
||||
|
||||
public static void initialise(ApplicationCycle applicationCycle) {
|
||||
taskListenerList = new ArrayList<EventListener>();
|
||||
EventManager.applicationCycle = applicationCycle;
|
||||
}
|
||||
|
||||
public static void add(EventListener eventListener) {
|
||||
taskListenerList.add(eventListener);
|
||||
}
|
||||
|
||||
public static void add(EventListener self, Task task) {
|
||||
if (task instanceof Stopper) {
|
||||
Stopper stopper = (Stopper) task;
|
||||
stopper.stop();
|
||||
} else {
|
||||
Target target = task.getTarget();
|
||||
switch (target) {
|
||||
case SELF:
|
||||
self.add(task);
|
||||
break;
|
||||
case APPLICATION:
|
||||
applicationCycle.current().add(task);
|
||||
break;
|
||||
default:
|
||||
for (EventListener eventListener : taskListenerList) {
|
||||
switch (target) {
|
||||
case ALL:
|
||||
eventListener.add(task);
|
||||
break;
|
||||
case MAIN:
|
||||
if (eventListener instanceof Main) {
|
||||
eventListener.add(task);
|
||||
}
|
||||
break;
|
||||
case DEVICES:
|
||||
if (eventListener instanceof Device) {
|
||||
eventListener.add(task);
|
||||
}
|
||||
break;
|
||||
case APPLICATIONS:
|
||||
if (eventListener instanceof Application) {
|
||||
eventListener.add(task);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void add(Task task) {
|
||||
if (!task.getTarget().equals(Target.SELF)) {
|
||||
add(null, task);
|
||||
}
|
||||
}
|
||||
|
||||
public static void remove(EventListener eventListener) {
|
||||
taskListenerList.remove(eventListener);
|
||||
}
|
||||
}
|
||||
44
java/src/pm/event/task/Continuous.java
Normal file
44
java/src/pm/event/task/Continuous.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package pm.event.task;
|
||||
|
||||
import pm.event.Task;
|
||||
import pm.value.Action;
|
||||
import pm.value.Target;
|
||||
|
||||
public class Continuous extends Task {
|
||||
protected static final int SLEEP = 0;
|
||||
|
||||
protected int sleep;
|
||||
protected int iteration;
|
||||
protected boolean stop;
|
||||
|
||||
public Continuous(Action action, Target target, int sleep) {
|
||||
super(action, target);
|
||||
this.sleep = sleep;
|
||||
reset();
|
||||
}
|
||||
|
||||
public Continuous(Action action, Target target) {
|
||||
this(action, target, SLEEP);
|
||||
}
|
||||
|
||||
public void nextIteration() {
|
||||
++iteration;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stop = true;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
iteration = 0;
|
||||
stop = false;
|
||||
}
|
||||
|
||||
public int getSleep() {
|
||||
return sleep;
|
||||
}
|
||||
|
||||
public boolean getStop() {
|
||||
return stop;
|
||||
}
|
||||
}
|
||||
24
java/src/pm/event/task/Dynamic.java
Normal file
24
java/src/pm/event/task/Dynamic.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package pm.event.task;
|
||||
|
||||
import pm.value.Action;
|
||||
import pm.value.Target;
|
||||
|
||||
public class Dynamic extends Continuous {
|
||||
protected static final int RATE = 10;
|
||||
|
||||
protected int rate;
|
||||
|
||||
public Dynamic(Action action, Target target, int sleep, int rate) {
|
||||
super(action, target, sleep);
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
public Dynamic(Action action, Target target, int sleep) {
|
||||
super(action, target, sleep);
|
||||
this.rate = RATE;
|
||||
}
|
||||
|
||||
public int getSleep() {
|
||||
return sleep + rate * iteration;
|
||||
}
|
||||
}
|
||||
16
java/src/pm/event/task/Stopper.java
Normal file
16
java/src/pm/event/task/Stopper.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package pm.event.task;
|
||||
|
||||
import pm.event.Task;
|
||||
|
||||
public class Stopper extends Task {
|
||||
protected Continuous continuous;
|
||||
|
||||
public Stopper(Continuous continuous) {
|
||||
super(null, null);
|
||||
this.continuous = continuous;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
continuous.stop(); // Todo: check if the task is really started?
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user