Omslachtigheid van actions en type in event omgeschreven naar action enum met parameter voor type. Type hernoemd naar target, dat dekt de lading beter. Mogelijkheid om ook device als target te gebruiken? En op die manier feedback versturen? Het event is nu bedoeld om events van services naar de devices te versturen.

This commit is contained in:
2011-02-04 21:23:01 +00:00
parent df91eab859
commit 1c61ac62fd
11 changed files with 77 additions and 60 deletions

View File

@@ -5,29 +5,29 @@ import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import pm.action.Action;
import pm.applicatie.voorbeeld.Voorbeeld;
import pm.application.Application;
import pm.application.voorbeeld.Voorbeeld;
import pm.device.Device;
import pm.device.example.Example;
import pm.event.Event;
import pm.exception.ActionException;
import pm.exception.ActionNotImplementedException;
import pm.exception.EventException;
public class Main extends Application {
public class Main extends Target {
protected static final int SLEEP = 100;
ArrayList<Application> applicationList;
ArrayList<Device> deviceList;
Queue<Event> eventQueue;
Queue<Action> actionQueue;
boolean run;
Application currentApplication;
public Main() {
applicationList = new ArrayList<Application>();
applicationList.iterator();
deviceList = new ArrayList<Device>();
eventQueue = new ConcurrentLinkedQueue<Event>();
actionQueue = new ConcurrentLinkedQueue<Action>();
}
public void addApplication(Application application) {
@@ -47,7 +47,9 @@ public class Main extends Application {
}
public void start() throws Exception {
addDevice(new Example(eventQueue));
Device device = new Example(actionQueue);
addDevice(device);
device.initialise();
Application application = new Voorbeeld();
addApplication(application);
currentApplication = application;
@@ -57,27 +59,29 @@ public class Main extends Application {
public void run() throws ActionException, EventException {
run = true;
while (run) {
if (eventQueue.isEmpty()) {
if (actionQueue.isEmpty()) {
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e) {}
} else {
Event event = eventQueue.poll();
Action action = event.getAction();
switch (event.getType()) {
Action action = actionQueue.poll();
Target target;
switch (action.getTarget()) {
case MAIN:
target = this;
invoke(action);
break;
case APPLICATION:
try {
currentApplication.invoke(action);
} catch (ActionNotImplementedException e) {
// Todo: log.write(...)
}
target = currentApplication;
break;
default:
throw new EventException("Unknown event type");
}
try {
target.invoke(action);
} catch (ActionNotImplementedException e) {
// Todo: log.write(...)
}
}
}
}

20
java/src/pm/Target.java Normal file
View File

@@ -0,0 +1,20 @@
package pm;
import java.lang.reflect.InvocationTargetException;
import pm.action.Action;
import pm.exception.ActionException;
import pm.exception.ActionInvokeException;
public abstract class Target {
public void invoke(Action action) throws ActionException {
try {
action.getMethod(this).invoke(this);
return;
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {}
throw new ActionInvokeException();
// Todo: informatie doorgeven over wat er precies is foutgegaan
}
}

View File

@@ -2,6 +2,7 @@ package pm.action;
import java.lang.reflect.Method;
import pm.event.Target;
import pm.exception.ActionNotImplementedException;
public enum Action {
@@ -10,11 +11,20 @@ public enum Action {
EXIT ("exit");
protected String action;
protected Target target;
Action(String action) {
this.action = action;
}
public void setTarget(Target target) {
this.target = target;
}
public Target getTarget() {
return target;
}
public Method getMethod(Object object) throws ActionNotImplementedException {
try {
return object.getClass().getMethod(action);

View File

@@ -1,20 +1,5 @@
package pm.application;
import java.lang.reflect.InvocationTargetException;
import pm.Target;
import pm.action.Action;
import pm.exception.ActionException;
import pm.exception.ActionInvokeException;
public abstract class Application {
public void invoke(Action action) throws ActionException {
try {
action.getMethod(this).invoke(this);
return;
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {}
throw new ActionInvokeException();
// Todo: informatie doorgeven over wat er precies is foutgegaan
}
}
public abstract class Application extends Target {}

View File

@@ -1,4 +1,4 @@
package pm.applicatie.voorbeeld;
package pm.application.voorbeeld;
import pm.application.Application;

View File

@@ -2,16 +2,21 @@ package pm.device;
import java.util.Queue;
import pm.event.Event;
import pm.action.Action;
import pm.event.Target;
public abstract class Device {
protected Queue<Event> eventQueue;
protected Queue<Action> actionQueue;
protected Device(Queue<Event> eventQueue) {
this.eventQueue = eventQueue;
initialise();
public Device(Queue<Action> actionQueue) {
this.actionQueue = actionQueue;
}
public void initialise() {}
public void addAction(Action action, Target target) {
action.setTarget(target);
actionQueue.add(action);
}
public abstract void initialise();
}

View File

@@ -4,17 +4,16 @@ import java.util.Queue;
import pm.action.Action;
import pm.device.Device;
import pm.event.Event;
import pm.event.Type;
import pm.event.Target;
public class Example extends Device {
public Example(Queue<Event> eventQueue) {
super(eventQueue);
public Example(Queue<Action> actionQueue) {
super(actionQueue);
}
public void initialise() {
eventQueue.add(new Event(Type.APPLICATION, Action.START));
eventQueue.add(new Event(Type.APPLICATION, Action.TEST));
eventQueue.add(new Event(Type.MAIN, Action.EXIT));
addAction(Action.START, Target.APPLICATION);
addAction(Action.TEST, Target.APPLICATION);
addAction(Action.EXIT, Target.MAIN);
}
}

View File

@@ -3,15 +3,15 @@ package pm.event;
import pm.action.Action;
public class Event {
protected Type type;
protected Target type;
protected Action action;
public Event(Type type, Action action) {
public Event(Target type, Action action) {
this.type = type;
this.action = action;
}
public Type getType() {
public Target getType() {
return type;
}

View File

@@ -0,0 +1,5 @@
package pm.event;
public enum Target {
MAIN, DEVICE, APPLICATION;
}

View File

@@ -1,5 +0,0 @@
package pm.event;
public enum Type {
MAIN, APPLICATION;
}