application cycle verwijderd, structuur input listener aangepast en event manager netter gemaakt.

This commit is contained in:
Bram Veenboer
2011-04-27 20:03:08 +00:00
parent 12467a043e
commit a268f91023
7 changed files with 58 additions and 66 deletions

View File

@@ -71,7 +71,7 @@ public abstract class Device extends EventListener {
Button button = hold.getButton(); Button button = hold.getButton();
add(new Press(button), new Release(button), continuous); add(new Press(button), new Release(button), continuous);
} }
/* Register interruptibles * /* Register interruptibles *
protected void add(Interruptible interruptible) { protected void add(Interruptible interruptible) {
interruptListener.add(interruptible); interruptListener.add(interruptible);

View File

@@ -5,7 +5,6 @@ import java.util.ArrayList;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import pm.application.ApplicationCycle;
import pm.application.cmd.windows.gomplayer.GomPlayerApplication; import pm.application.cmd.windows.gomplayer.GomPlayerApplication;
import pm.application.cmd.windows.wmp.WMPApplication; import pm.application.cmd.windows.wmp.WMPApplication;
import pm.application.example.ExampleApplication; import pm.application.example.ExampleApplication;
@@ -29,13 +28,14 @@ import pm.exception.application.ApplicationInitialiseException;
import pm.exception.device.DeviceExitException; import pm.exception.device.DeviceExitException;
import pm.exception.device.DeviceInitialiseException; import pm.exception.device.DeviceInitialiseException;
import pm.macro.Active; import pm.macro.Active;
import pm.util.ArrayCycle;
import pm.value.Action; import pm.value.Action;
public class Main extends EventListener { public class Main extends EventListener {
protected Log log = LogFactory.getLog(Main.class); protected Log log = LogFactory.getLog(Main.class);
//protected String[] deviceClassArray; //protected String[] deviceClassArray;
protected ApplicationCycle applicationCycle; protected ArrayCycle<Application> applicationCycle;
protected ArrayList<Device> deviceList; protected ArrayList<Device> deviceList;
public Main() { public Main() {
@@ -45,7 +45,7 @@ public class Main extends EventListener {
"pm.device.javainput.rumblepad.RumblepadDevice", "pm.device.javainput.rumblepad.RumblepadDevice",
"pm.device.javainput.extreme3d.Extreme3DDevice", "pm.device.javainput.extreme3d.Extreme3DDevice",
"pm.device.wiimote.WiimoteDevice"};*/ "pm.device.wiimote.WiimoteDevice"};*/
applicationCycle = new ApplicationCycle(); applicationCycle = new ArrayCycle<Application>();
deviceList = new ArrayList<Device>(); deviceList = new ArrayList<Device>();
EventManager.initialise(applicationCycle); EventManager.initialise(applicationCycle);
EventManager.add(this); EventManager.add(this);

View File

@@ -1,8 +0,0 @@
package pm.application;
import pm.Application;
import pm.util.ArrayCycle;
public class ApplicationCycle extends ArrayCycle<Application> {
protected static final long serialVersionUID = 1L;
}

View File

@@ -1,41 +0,0 @@
package pm.device.text;
import java.io.InputStream;
import java.util.Scanner;
import pm.Listener;
import pm.event.Task;
import pm.event.EventManager;
import pm.exception.task.TaskNotSupportedException;
import pm.value.Action;
import pm.value.Target;
public class InputListener extends Listener {
protected Scanner input;
public InputListener(InputStream inputStream) {
input = new Scanner(inputStream);
}
public void run() {
run = true;
while (running()) {
String string = input.next().toUpperCase();
if(string != null) {
try {
EventManager.add(
new Task(Action.valueOf(string), Target.APPLICATION));
} catch(IllegalArgumentException e) {}
}
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected boolean running() {
return run && input.hasNext();
}
}

View File

@@ -1,6 +1,14 @@
package pm.device.text; package pm.device.text;
import java.io.InputStream;
import java.util.Scanner;
import pm.Device; import pm.Device;
import pm.Listener;
import pm.event.EventManager;
import pm.event.Task;
import pm.value.Action;
import pm.value.Target;
public class TextDevice extends Device { public class TextDevice extends Device {
InputListener inputListener; InputListener inputListener;
@@ -16,4 +24,33 @@ public class TextDevice extends Device {
public void exit() { public void exit() {
inputListener.stop(); inputListener.stop();
} }
}
public void add(String string) {
EventManager.add(new Task(Action.valueOf(string), Target.APPLICATION));
}
public class InputListener extends Listener {
protected Scanner input;
public InputListener(InputStream inputStream) {
input = new Scanner(inputStream);
}
public void run() {
run = true;
while (run && input.hasNext()) {
String string = input.next().toUpperCase();
if(string != null) {
try {
add(string);
} catch(IllegalArgumentException e) {}
}
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

View File

@@ -7,15 +7,15 @@ import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import pm.Listener; import pm.Listener;
import pm.device.text.InputListener; import pm.device.text.TextDevice;
public class SocketListener extends Listener { public class SocketListener extends Listener {
protected ServerSocket server; protected ServerSocket server;
protected ArrayList<InputListener> inputListenerList; protected ArrayList<TextDevice.InputListener> inputListenerList;
public SocketListener(ServerSocket server) { public SocketListener(ServerSocket server) {
this.server = server; this.server = server;
inputListenerList = new ArrayList<InputListener>(); inputListenerList = new ArrayList<TextDevice.InputListener>();
} }
public void run() { public void run() {
@@ -23,7 +23,7 @@ public class SocketListener extends Listener {
try { try {
Socket socket = server.accept(); Socket socket = server.accept();
InputStream inputStream = socket.getInputStream(); InputStream inputStream = socket.getInputStream();
InputListener inputListener = new InputListener(inputStream); TextDevice.InputListener inputListener = new TextDevice().new InputListener(inputStream);
inputListenerList.add(inputListener); inputListenerList.add(inputListener);
inputListener.start(); inputListener.start();
} catch (IOException e) { } catch (IOException e) {
@@ -34,7 +34,7 @@ public class SocketListener extends Listener {
public void stop() { public void stop() {
run = false; run = false;
for (InputListener inputListener : inputListenerList) { for (TextDevice.InputListener inputListener : inputListenerList) {
inputListener.stop(); inputListener.stop();
} }
} }

View File

@@ -5,15 +5,15 @@ import java.util.ArrayList;
import pm.Application; import pm.Application;
import pm.Device; import pm.Device;
import pm.Main; import pm.Main;
import pm.application.ApplicationCycle;
import pm.event.task.Stopper; import pm.event.task.Stopper;
import pm.util.ArrayCycle;
import pm.value.Target; import pm.value.Target;
public class EventManager { public class EventManager {
protected static ArrayList<EventListener> eventListenerList; protected static ArrayList<EventListener> eventListenerList;
protected static ApplicationCycle applicationCycle; protected static ArrayCycle<Application> applicationCycle;
public static void initialise(ApplicationCycle applicationCycle) { public static void initialise(ArrayCycle<Application> applicationCycle) {
eventListenerList = new ArrayList<EventListener>(); eventListenerList = new ArrayList<EventListener>();
EventManager.applicationCycle = applicationCycle; EventManager.applicationCycle = applicationCycle;
} }
@@ -30,8 +30,7 @@ public class EventManager {
public static void add(EventListener self, Task task) { public static void add(EventListener self, Task task) {
if (task instanceof Stopper) { if (task instanceof Stopper) {
Stopper stopper = (Stopper) task; ((Stopper) task).stop();
stopper.stop();
} else { } else {
Target target = task.getTarget(); Target target = task.getTarget();
switch (target) { switch (target) {
@@ -43,7 +42,7 @@ public class EventManager {
applicationCycle.current().add(task); applicationCycle.current().add(task);
} }
break; break;
default: default: {
for (EventListener eventListener : eventListenerList) { for (EventListener eventListener : eventListenerList) {
switch (target) { switch (target) {
case ALL: case ALL:
@@ -66,11 +65,16 @@ public class EventManager {
break; break;
} }
} }
}
} }
} }
} }
public static void add(Task task) { public static void add(Task task) {
add(null, task);
}
private static void add(Task task) {
if (!task.getTarget().equals(Target.SELF)) { if (!task.getTarget().equals(Target.SELF)) {
add(null, task); add(null, task);
} }