Zeer veel aanpassingen:

- omslachtigheden verbeterd
- netwerking gefixt
- ...
This commit is contained in:
2011-05-08 13:38:23 +00:00
parent f5a555adbc
commit e135ea5a6d
39 changed files with 661 additions and 549 deletions

3
java/client.bat Normal file
View File

@@ -0,0 +1,3 @@
set path=%path%;native
java -cp bin;cfg;resource;lib/jacob-1.15-M3.jar;lib/TableLayout.jar;lib/nativecall-0.4.1.jar;lib/nativeloader-200505172341.jar pm.Client
pause

View File

@@ -1,10 +1,10 @@
package pm;
import pm.event.EventListener;
import pm.event.EventHandler;
import pm.exception.application.ApplicationExitException;
import pm.exception.application.ApplicationInitialiseException;
public abstract class Application extends EventListener {
public abstract class Application extends EventHandler {
public void initialise() throws ApplicationInitialiseException {}
public void exit() throws ApplicationExitException {

34
java/src/pm/Client.java Normal file
View File

@@ -0,0 +1,34 @@
package pm;
import pm.event.spreader.NetworkSpreader;
import pm.exception.device.DeviceInitialiseException;
import pm.exception.event.spreader.NetworkSpreaderException;
public class Client extends Manager {
public static final String IP = "localhost";
public static final int PORT = 6789;
public Client(String ip, int port) throws NetworkSpreaderException {
super(new NetworkSpreader(ip, port));
}
public Client() throws NetworkSpreaderException {
this(IP, PORT);
}
public void start() {
log.info("LocalManager!");
try {
initialise();
} catch (DeviceInitialiseException e) {}
super.start(false);
}
public static void main(String[] args) {
try {
new Client().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -1,7 +1,7 @@
package pm;
import pm.event.Task;
import pm.event.EventListener;
import pm.event.EventHandler;
import pm.event.task.Continuous;
import pm.event.task.Stopper;
import pm.exception.device.DeviceExitException;
@@ -13,11 +13,14 @@ import pm.macro.state.Hold;
import pm.macro.state.Press;
import pm.macro.state.Release;
public abstract class Device extends EventListener {
public abstract class Device extends EventHandler {
protected SequenceListener sequenceListener;
static {
SequenceListener.initialise(eventSpreader);
}
public Device() {
super();
sequenceListener = new SequenceListener(this);
}

View File

@@ -1,3 +1,35 @@
package pm;
public interface Event {}
import java.io.Serializable;
import pm.event.EventListener;
import pm.value.Target;
public class Event implements Serializable {
protected static final long serialVersionUID = 1L;
protected Target target;
public Event(Target target) {
this.target = target;
}
public Target getTarget() {
return target;
}
public boolean compatible(EventListener eventListener) {
switch (target) {
case ALL:
return true;
case MANAGER:
return eventListener instanceof Manager;
case DEVICES:
return eventListener instanceof Device;
case APPLICATIONS:
return eventListener instanceof Application;
default:
return false;
}
}
}

View File

@@ -2,116 +2,69 @@ package pm;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pm.application.cmd.windows.gomplayer.GomPlayerApplication;
import pm.application.cmd.windows.wmp.WMPApplication;
import pm.application.example.ExampleApplication;
import pm.application.itunes.iTunesApplication;
import pm.application.mpc.MPCApplication;
import pm.application.vlc.VLCApplication;
import pm.application.windows.winamp.WinampApplication;
import pm.device.gui.GUIDevice;
import pm.device.javainput.extreme3d.Extreme3DDevice;
import pm.device.javainput.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice;
import pm.device.text.TextDevice;
import pm.device.text.lan.LanTextDevice;
import pm.device.wiimote.WiimoteDevice;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.event.spreader.LocalSpreader;
import pm.exception.application.ApplicationExitException;
import pm.exception.application.ApplicationInitialiseException;
import pm.exception.device.DeviceExitException;
import pm.exception.device.DeviceInitialiseException;
import pm.macro.Active;
import pm.network.NetworkServer;
import pm.util.ArrayCycle;
import pm.value.Action;
public class Main extends EventListener {
protected Log log = LogFactory.getLog(Main.class);
//protected String[] deviceClassArray;
public class Main extends Manager {
protected ArrayCycle<Application> applicationCycle;
protected ArrayList<Device> deviceList;
protected EventManager eventManager;
public Main() {
super();
super(new LocalSpreader());
applicationCycle = new ArrayCycle<Application>();
deviceList = new ArrayList<Device>();
eventManager = new EventManager(applicationCycle);
EventListener.initialise(eventManager);
}
protected void action(Action action) {
System.out.println("Manager: " + action);
switch (action) {
case NEXT:
eventSpreader.set(applicationCycle.next());
System.out.println(applicationCycle.current());
break;
case PREVIOUS:
eventSpreader.set(applicationCycle.previous());
System.out.println(applicationCycle.current());
break;
case EXIT:
exit();
break;
}
}
public void initialise() throws DeviceInitialiseException {
//add(new JIntellitypeDevice());
//add(new PlayerDevice());
//add(new RumblepadDevice());
//add(new WiimoteDevice());
//add(new GUIDevice());
//add(new TextDevice());
add(new PanelDevice());
//add(new LanTextDevice());
//add(new Extreme3DDevice());
//add(new NetworkServer());
startDevices();
//add(new ExampleApplication());
//add(new WMPApplication());
//add(new GomPlayerApplication());
add(new WinampApplication());
//add(new iTunesApplication());
//add(new VLCApplication());
//add(new MPCApplication());
super.initialise();
add(new iTunesApplication());
log.error("main init");
startApplications();
}
public void exit() {
exitDevices();
stop();
}
protected void startApplications() {
ArrayList<Application> removeList = new ArrayList<Application>();
for (Application application : applicationCycle) {
try {
application.initialise();
application.start();
log.debug(application);
} catch (ApplicationInitialiseException e) {
removeList.add(application);
}
}
for (Application application : removeList) {
remove(application);
}
}
eventSpreader.set(applicationCycle.current());
}
protected void startDevices() {
ArrayList<Device> removeList = new ArrayList<Device>();
for (Device device : deviceList) {
try {
device.initialise();
device.start();
log.info("Device started: " + device);
} catch (DeviceInitialiseException e) {
removeList.add(device);
}
}
for (Device device : removeList) {
remove(device);
}
}
public void exit() {
System.out.println("Exit devices...");
for (Device device : deviceList) {
try {
device.exit();
} catch (DeviceExitException e) {
e.printStackTrace();
}
}
protected void exitApplications() {
System.out.println("Exit applications...");
for (Application application : applicationCycle) {
try {
@@ -119,74 +72,25 @@ public class Main extends EventListener {
} catch (ApplicationExitException e) {}
}
System.out.println("Exit main...");
stop();
}
protected void action(Action action) {
System.out.println("NetworkClient: " + action);
switch (action) {
case NEXT:
applicationCycle.next();
System.out.println(applicationCycle.current());
break;
case PREVIOUS:
applicationCycle.previous();
System.out.println(applicationCycle.current());
break;
case EXIT:
exit();
break;
}
}
/*protected void addDevices() throws DeviceInitialiseException {
for (String deviceClass : deviceClassArray) {
try {
Object object = Class.forName(deviceClass).getConstructor((Class[]) null).newInstance();
if (object instanceof Application) {
Device device = (Device) object;
add(device);
try {
device.initialise();
} catch (DeviceNotFoundException e) {}
}
} catch (IllegalArgumentException e) {
} catch (SecurityException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
} catch (ClassNotFoundException e) {}
}
}*/
/* Add / remove methods */
protected void add(Application application) {
//EventManager.add(application);
applicationCycle.add(application);
}
protected void remove(Application application) {
//EventManager.remove(application);
applicationCycle.remove(application);
}
protected void add(Device device) {
//EventManager.add(device);
deviceList.add(device);
}
protected void remove(Device device) {
//EventManager.remove(device);
deviceList.remove(device);
public void start() {
log.info("LocalManager!");
try {
initialise();
} catch (DeviceInitialiseException e) {}
super.start(false);
}
public static void main(String[] args) {
try {
Main main = new Main();
main.initialise();
main.start(false);
} catch (Exception e) {
e.printStackTrace();
}
new Main().start();
}
}
}

79
java/src/pm/Manager.java Normal file
View File

@@ -0,0 +1,79 @@
package pm;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pm.device.network.NetworkDevice;
import pm.device.wiimote.WiimoteDevice;
import pm.event.EventHandler;
import pm.event.EventSpreader;
import pm.exception.device.DeviceExitException;
import pm.exception.device.DeviceInitialiseException;
public abstract class Manager extends EventHandler {
protected Log log = LogFactory.getLog(Main.class);
protected ArrayList<Device> deviceList;
public Manager(EventSpreader eventSpreader) {
EventHandler.initialise(eventSpreader);
deviceList = new ArrayList<Device>();
eventSpreader.start();
}
public void initialise() throws DeviceInitialiseException {
//add(new JIntellitypeDevice());
//add(new PlayerDevice());
//add(new RumblepadDevice());
add(new WiimoteDevice());
//add(new GUIDevice());
//add(new TextDevice());
//add(new PanelDevice());
//add(new LanTextDevice());
//add(new Extreme3DDevice());
//add(new NetworkDevice());
startDevices();
}
public void exit() {
exitDevices();
stop();
}
protected void startDevices() {
ArrayList<Device> removeList = new ArrayList<Device>();
for (Device device : deviceList) {
try {
device.initialise();
device.start();
System.out.println("Device started: " + device);
} catch (DeviceInitialiseException e) {
removeList.add(device);
}
}
for (Device device : removeList) {
remove(device);
}
}
protected void exitDevices() {
System.out.println("Exit devices...");
for (Device device : deviceList) {
try {
device.exit();
} catch (DeviceExitException e) {
e.printStackTrace();
}
}
}
protected void add(Device device) {
deviceList.add(device);
}
protected void remove(Device device) {
deviceList.remove(device);
}
}

View File

@@ -1,6 +1,6 @@
package pm;
public abstract class Listener implements Runnable {
public abstract class Worker implements Runnable {
protected static final boolean THREAD = true;
protected static final int SLEEP = 100;

View File

@@ -9,7 +9,6 @@ import pm.exception.MacroException;
import pm.exception.button.UnknownButtonException;
import pm.exception.button.UnknownDirectionException;
import pm.exception.device.DeviceInitialiseException;
import pm.interrupt.Interruptible;
import pm.macro.state.Hold;
import pm.macro.state.Press;
import pm.macro.state.Release;
@@ -33,7 +32,7 @@ public class Extreme3DDevice extends JavaInputDevice {
new Press(Extreme3DButton.TWO),
new Press(Extreme3DButton.ELEVEN),
new Release(Extreme3DButton.ONE)),
new Task(Action.EXIT, Target.MAIN));
new Task(Action.EXIT, Target.MANAGER));
} catch (MacroException e) {
e.printStackTrace();
}

View File

@@ -32,10 +32,10 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
jit.addIntellitypeListener(this);
add(
new Hotkey(Key.PRIOR),
new Task(Action.PREVIOUS, Target.MAIN));
new Task(Action.PREVIOUS, Target.MANAGER));
add(
new Hotkey(Key.NEXT),
new Task(Action.NEXT, Target.MAIN));
new Task(Action.NEXT, Target.MANAGER));
add(
new Press(CommandButton.VOLUME_DOWN),
new Task(Action.VOLUME_DOWN, Target.APPLICATIONS));
@@ -44,7 +44,7 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
new Task(Action.VOLUME_UP, Target.APPLICATIONS));
add(
new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'),
new Task(Action.EXIT, Target.MAIN));
new Task(Action.EXIT, Target.MANAGER));
add(
new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'),
new Task(Action.NEXT, Target.APPLICATION));

View File

@@ -0,0 +1,122 @@
package pm.device.network;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pm.Device;
import pm.Event;
import pm.Worker;
import pm.exception.device.DeviceInitialiseException;
import pm.value.Action;
public class NetworkDevice extends Device {
public static final int PORT = 6789;
protected Log log = LogFactory.getLog(NetworkDevice.class);
protected int port;
protected Server server;
protected ArrayList<Client> clientList;
public NetworkDevice(int port) {
this.port = port;
}
public NetworkDevice() {
this(PORT);
}
public void initialise() throws DeviceInitialiseException {
try {
server = new Server(port);
server.start();
} catch (IOException e) {
throw new DeviceInitialiseException();
}
}
public void exit() {
server.stop();
}
protected void add(Action action) {
}
protected class Server extends Worker {
protected ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
clientList = new ArrayList<Client>();
System.out.println("Server started");
}
public void run() {
while (run) {
System.out.println("Server is waiting for clients");
try {
Socket socket = serverSocket.accept();
Client client = new Client(socket);
client.start();
System.out.println("Client connected");
} catch (IOException e) {}
}
for (Client client : clientList) {
client.stop();
}
}
}
protected class Client extends Worker {
protected Socket socket;
protected ObjectInputStream objectInputStream;
protected OutputStream outputStream;
public Client(Socket socket) throws IOException {
this.socket = socket;
objectInputStream = new ObjectInputStream(socket.getInputStream());
//outputStream = socket.getOutputStream();
clientList.add(this);
}
public void run() {
try {
Object object;
do {
object = objectInputStream.readObject();
if (object instanceof Event) {
log.debug("event binnen!");
eventSpreader.add((Event) object);
}
log.debug("iets te lezen!");
} while (object != null);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("stoppen");
try {
disconnect();
} catch (IOException e) {
} finally {
clientList.remove(this);
}
}
public void disconnect() throws IOException {
objectInputStream.close();
outputStream.close();
socket.close();
}
}
}

View File

@@ -1,4 +1,4 @@
package pm.network;
package pm.device.network;
import java.io.IOException;
import java.io.InputStream;
@@ -7,7 +7,7 @@ import java.net.Socket;
import java.util.Scanner;
import pm.Device;
import pm.exception.device.DeviceInitialiseException;
import pm.exception.task.action.ActionDeserializeException;
import pm.value.Action;
public class NetworkServer extends Device {
@@ -43,15 +43,15 @@ public class NetworkServer extends Device {
Socket socket = server.accept();
final InputStream inputStream = socket.getInputStream();
new Thread() {
public void run(){
public void run() {
Scanner input = new Scanner(inputStream);
while (input.hasNext()) {
String string = input.next().toUpperCase();
if(string != null) {
try {
Action action = Action.deserialize(string);
Action action = Action.deserialise(string);
action(action);
} catch(IllegalArgumentException e) {}
} catch (ActionDeserializeException e) {}
}
try {
Thread.sleep(SLEEP);
@@ -68,4 +68,4 @@ public class NetworkServer extends Device {
}
}
}
}
}

View File

@@ -1,56 +0,0 @@
package pm.device.text;
import java.io.InputStream;
import java.util.Scanner;
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 {
InputListener inputListener;
public TextDevice() {
inputListener = new InputListener(System.in);
}
public void initialise() {
inputListener.start();
}
public void exit() {
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

@@ -6,10 +6,10 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import pm.Listener;
import pm.Worker;
import pm.device.text.TextDevice;
public class SocketListener extends Listener {
public class SocketListener extends Worker {
protected ServerSocket server;
protected ArrayList<TextDevice.InputListener> inputListenerList;

View File

@@ -97,7 +97,7 @@ public class WiimoteDevice extends Device implements GestureListener {
new Continuous(Action.VOLUME_UP, Target.APPLICATION, 100));
add(
new Press(WiimoteButton.HOME),
new Task(Action.NEXT, Target.MAIN));
new Task(Action.NEXT, Target.MANAGER));
try {
add(
new Macro(

View File

@@ -0,0 +1,45 @@
package pm.event;
import pm.Event;
import pm.event.task.Continuous;
import pm.exception.InitialiseException;
import pm.value.Action;
public abstract class EventHandler extends EventListener {
protected static EventSpreader eventSpreader;
public static void initialise(EventSpreader eventSpreader) {
EventHandler.eventSpreader = eventSpreader;
}
public void initialise() throws InitialiseException {
eventSpreader.add(this);
}
public void event(Event event) {
if (event instanceof Feedback) {
feedback((Feedback) event);
} else if (event instanceof Task) {
task((Task) event);
}
}
protected void feedback(Feedback feedback) {}
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) {}
}

View File

@@ -1,47 +1,123 @@
package pm.event;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import pm.Event;
import pm.Listener;
import pm.event.task.Continuous;
import pm.value.Action;
import pm.Worker;
public abstract class EventListener extends Listener implements Runnable {
protected static EventManager eventManager;
public abstract class EventListener extends Worker {
protected Queue<Event> eventQueue;
protected Object available;
public static void initialise(EventManager eventManager) {
EventListener.eventManager = eventManager;
public EventListener() {
eventQueue = new ConcurrentLinkedQueue<Event>();
available = new Object();
}
public void add(Event event) {
eventQueue.add(event);
synchronized (available) {
available.notifyAll();
}
}
/*public Event get(Target target) {
while (eventQueue.isEmpty()) {
synchronized (available) {
try {
available.await();
} catch (InterruptedException e) {}
}
}
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return eventQueue.poll();
} else {
return null;
}
} else {
return eventQueue.poll();
}
}
public Event gett(Target target) {
while (true) {
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return get();
}
}
}
}
public Event get() {
return get(Target.ALL);
}*/
public final void run() {
while (run) {
event(eventManager.get());
}
while (eventQueue.isEmpty()) {
synchronized (available) {
try {
available.wait();
} catch (InterruptedException e) {}
}
}
event(eventQueue.poll());
}
}
protected void event(Event event) {
if (event instanceof Feedback) {
event((Feedback) event);
} else if (event instanceof Task) {
event((Task) event);
public abstract void event(Event event);
/*public static void add(Feedback feedback) {
for (EventListener eventListener : eventListenerList) {
eventListener.add(feedback);
}
}
}*/
protected void event(Feedback feedback) {}
protected void event(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();
/*public static void add(EventListener self, Task task) {
if (task instanceof Stopper) {
((Stopper) task).stop();
} else {
action(action);
}
}
protected void action(Action action) {}
Target target = task.getTarget();
switch (target) {
case SELF:
self.add(task);
break;
case APPLICATION:
if (applicationCycle.size() > 0) {
applicationCycle.current().add(task);
}
break;
default: {
for (EventListener eventListener : eventListenerList) {
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;
}
}
}
}
}
}*/
}

View File

@@ -1,116 +0,0 @@
package pm.event;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import pm.Application;
import pm.Event;
import pm.util.ArrayCycle;
import pm.value.Target;
public class EventManager {
protected Queue<Event> eventQueue;
protected Lock lock;
protected Condition available;
public EventManager(ArrayCycle<Application> applicationCycle) {
eventQueue = new ConcurrentLinkedQueue<Event>();
lock = new ReentrantLock();
available = lock.newCondition();
}
public void add(Event event) {
eventQueue.add(event);
synchronized (available) {
lock.notifyAll();
}
}
public Event get(Target target) {
while (eventQueue.isEmpty()) {
synchronized (available) {
try {
available.await();
} catch (InterruptedException e) {}
}
}
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return eventQueue.poll();
} else {
return null;
}
} else {
return eventQueue.poll();
}
}
public Event gett(Target target) {
while (true) {
Event event = eventQueue.peek();
if (event instanceof Task) {
Task task = (Task) event;
if (task.getTarget() == target) {
return get();
}
}
}
}
public Event get() {
return get(Target.ALL);
}
/*public static void add(Feedback feedback) {
for (EventListener eventListener : eventListenerList) {
eventListener.add(feedback);
}
}*/
/*public static void add(EventListener self, Task task) {
if (task instanceof Stopper) {
((Stopper) task).stop();
} else {
Target target = task.getTarget();
switch (target) {
case SELF:
self.add(task);
break;
case APPLICATION:
if (applicationCycle.size() > 0) {
applicationCycle.current().add(task);
}
break;
default: {
for (EventListener eventListener : eventListenerList) {
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;
}
}
}
}
}
}*/
}

View File

@@ -0,0 +1,26 @@
package pm.event;
import java.util.ArrayList;
import pm.Application;
public abstract class EventSpreader extends EventListener {
protected ArrayList<EventListener> eventListenerList;
protected Application application;
public void set(Application application) {
this.application = application;
}
public EventSpreader() {
eventListenerList = new ArrayList<EventListener>();
}
public void add(EventListener eventListener) {
eventListenerList.add(eventListener);
}
public void remove(EventListener eventListener) {
eventListenerList.remove(eventListener);
}
}

View File

@@ -1,7 +1,10 @@
package pm.event;
import pm.Event;
import pm.value.Target;
public class Feedback implements Event {
}
public class Feedback extends Event {
public Feedback() {
super(Target.ALL);
}
}

View File

@@ -4,13 +4,12 @@ import pm.Event;
import pm.value.Action;
import pm.value.Target;
public class Task implements Event {
public class Task extends Event {
protected Action action;
protected Target target;
public Task(Action action, Target target) {
super(target);
this.action = action;
this.target = target;
}
public Task(Action action) {
@@ -20,8 +19,4 @@ public class Task implements Event {
public Action getAction() {
return action;
}
public Target getTarget() {
return target;
}
}

View File

@@ -0,0 +1,27 @@
package pm.event.spreader;
import pm.Event;
import pm.event.EventListener;
import pm.event.EventSpreader;
import pm.value.Target;
public class LocalSpreader extends EventSpreader {
public void event(Event event) {
System.out.println("localspread");
System.out.println(application);
Target target = event.getTarget();
switch (target) {
case APPLICATION:
if (application != null) {
application.add(event);
}
break;
default:
for (EventListener eventListener : eventListenerList) {
if (event.compatible(eventListener)) {
eventListener.add(event);
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
package pm.event.spreader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import pm.Event;
import pm.event.EventSpreader;
import pm.exception.event.spreader.NetworkSpreaderException;
public class NetworkSpreader extends EventSpreader {
protected Socket socket;
protected ObjectOutputStream objectOutputStream;
public NetworkSpreader(String ip, int port) throws NetworkSpreaderException {
try {
socket = new Socket(ip, port);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
return;
} catch (UnknownHostException e) {
} catch (IOException e) {}
throw new NetworkSpreaderException();
}
public void event(Event event) {
System.out.println("NetworkSpreader: event!");
//System.out.println(socket.isConnected());
try {
objectOutputStream.writeObject(event);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -1,5 +1,5 @@
package pm.exception;
public class ApplicationException extends Exception {
public class ApplicationException extends HandlerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -1,5 +1,5 @@
package pm.exception;
public class DeviceException extends Exception {
public class DeviceException extends HandlerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,5 @@
package pm.exception;
public class EventException extends Exception {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,5 @@
package pm.exception;
public class HandlerException extends Exception {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,5 @@
package pm.exception;
public class InitialiseException extends HandlerException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,5 @@
package pm.exception;
public class TaskException extends Exception {
protected static final long serialVersionUID = 1L;
}

View File

@@ -1,7 +1,7 @@
package pm.exception.application;
import pm.exception.ApplicationException;
import pm.exception.InitialiseException;
public class ApplicationInitialiseException extends ApplicationException {
public class ApplicationInitialiseException extends InitialiseException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -1,7 +1,7 @@
package pm.exception.device;
import pm.exception.DeviceException;
import pm.exception.InitialiseException;
public class DeviceInitialiseException extends DeviceException {
public class DeviceInitialiseException extends InitialiseException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.event;
import pm.exception.EventException;
public class SpreaderException extends EventException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.event.spreader;
import pm.exception.event.SpreaderException;
public class NetworkSpreaderException extends SpreaderException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.task;
import pm.exception.TaskException;
public class ActionException extends TaskException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,7 @@
package pm.exception.task.action;
import pm.exception.task.ActionException;
public class ActionDeserializeException extends ActionException {
protected static final long serialVersionUID = 1L;
}

View File

@@ -3,34 +3,35 @@ package pm.macro;
import java.util.ArrayList;
import java.util.HashMap;
import pm.Event;
import pm.event.Task;
import pm.event.EventHandler;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.value.Target;
public class SequenceListener {
protected EventListener eventListener;
protected EventHandler eventHandler;
protected ArrayList<Sequence> sequenceList;
protected HashMap<Sequence, Task> taskMap;
protected HashMap<Sequence, Event> eventMap;
protected ArrayList<Active> activeList;
protected static EventManager eventManager;
protected static EventListener eventListener;
public SequenceListener(EventListener eventListener) {
this.eventListener = eventListener;
public SequenceListener(EventHandler eventHandler) {
this.eventHandler = eventHandler;
sequenceList = new ArrayList<Sequence>();
taskMap = new HashMap<Sequence, Task>();
eventMap = new HashMap<Sequence, Event>();
activeList = new ArrayList<Active>();
}
public static void initialise(EventManager eventManager) {
SequenceListener.eventManager = eventManager;
public static void initialise(EventListener eventListener) {
SequenceListener.eventListener = eventListener;
}
public int add(Sequence sequence, Task task) {
int id = sequenceList.size();
sequenceList.add(sequence);
taskMap.put(sequence, task);
eventMap.put(sequence, task);
return id;
}
@@ -42,12 +43,11 @@ public class SequenceListener {
for (Active active : activeList) {
if (active.next(state)) {
if (active.last()) {
Task task = taskMap.get(active.getSequence());
if (task.getTarget().equals(Target.SELF)) {
//eventListener.event() protected in event listener
//.add(eventListener, task);
Event event = eventMap.get(active.getSequence());
if (event.getTarget().equals(Target.SELF)) {
eventHandler.event(event);
} else {
eventManager.add(task);
eventListener.add(event);
}
removeList.add(active);
}

View File

@@ -1,145 +0,0 @@
package pm.network;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pm.Device;
import pm.client.LanTextClient;
import pm.device.gui.GUIDevice;
import pm.device.javainput.extreme3d.Extreme3DDevice;
import pm.device.javainput.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice;
import pm.device.text.TextDevice;
import pm.device.text.lan.LanTextDevice;
import pm.device.wiimote.WiimoteDevice;
import pm.event.EventListener;
import pm.event.EventManager;
import pm.exception.device.DeviceExitException;
import pm.exception.device.DeviceInitialiseException;
import pm.macro.Active;
import pm.value.Action;
public class NetworkClient extends EventListener {
protected Log log = LogFactory.getLog(NetworkClient.class);
protected ArrayList<Device> deviceList;
protected MessageSender messageSender;
public NetworkClient() {
super();
deviceList = new ArrayList<Device>();
EventManager.initialise(null);
EventManager.add(this);
}
public void initialise() throws DeviceInitialiseException {
//add(new JIntellitypeDevice());
//add(new PlayerDevice());
//add(new RumblepadDevice());
//add(new WiimoteDevice());
//add(new GUIDevice());
//add(new TextDevice());
//add(new LanTextDevice());
//add(new Extreme3DDevice());
add(new PanelDevice());
startDevices();
messageSender = new MessageSender("192.168.1.101", 1234);
}
protected void startDevices() {
ArrayList<Device> removeList = new ArrayList<Device>();
for (Device device : deviceList) {
try {
device.initialise();
device.start();
log.info("Device started: " + device);
} catch (DeviceInitialiseException e) {
removeList.add(device);
}
}
for (Device device : removeList) {
remove(device);
}
}
public void exit() {
System.out.println("Exit devices...");
for (Device device : deviceList) {
try {
device.exit();
} catch (DeviceExitException e) {
e.printStackTrace();
}
}
System.out.println("Exit main...");
stop();
}
protected void add(Action action) {
System.out.println("NetworkClient: " + action);
String message = action.serialze();
messageSender.setMessage(message);
messageSender.notify();
}
/* Add / remove methods */
protected void add(Device device) {
EventManager.add(device);
deviceList.add(device);
}
protected void remove(Device device) {
EventManager.remove(device);
deviceList.remove(device);
}
public static void main(String[] args) {
try {
NetworkClient networkClient = new NetworkClient();
networkClient.initialise();
networkClient.start(false);
} catch (Exception e) {
e.printStackTrace();
}
}
protected class MessageSender {
protected Socket socket;
protected Scanner input;
protected PrintStream output;
protected String message;
public MessageSender(String host, int port) {
try {
socket = new Socket(host, port);
input = new Scanner(System.in);
output = new PrintStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public void setMessage(String message) {
this.message = message;
}
protected void start() {
while (true) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
output.println(message);
}
}
}
}

View File

@@ -1,5 +1,7 @@
package pm.value;
import pm.exception.task.action.ActionDeserializeException;
public enum Action {
EXIT,
FORWARD,
@@ -21,15 +23,12 @@ public enum Action {
public String serialze() {
return name();
}
public static Action deserialize(String value) {
if (value != null) {
for (Action action : values()) {
if (action.name().equals(value)) {
return action;
}
}
public static Action deserialise(String value) throws ActionDeserializeException {
try {
return Action.valueOf(value);
} catch (NullPointerException e) {
throw new ActionDeserializeException();
}
return null;
}
}

View File

@@ -1,5 +1,5 @@
package pm.value;
public enum Target {
ALL, MAIN, DEVICES, APPLICATIONS, APPLICATION, SELF;
ALL, MANAGER, DEVICES, APPLICATIONS, APPLICATION, SELF;
}