diff --git a/java/src/pm/Application.java b/java/src/pm/Application.java index 40ca543..eed205d 100644 --- a/java/src/pm/Application.java +++ b/java/src/pm/Application.java @@ -1,9 +1,9 @@ package pm; import pm.event.EventHandler; -import pm.selector.Selectable; +import pm.manager.Titled; -public abstract class Application extends EventHandler implements Selectable { +public abstract class Application extends EventHandler implements Titled, Exitable { protected String title; protected boolean active; @@ -17,7 +17,9 @@ public abstract class Application extends EventHandler implements Selectable { } public void exit() { - deactivate(); + if (active()) { + deactivate(); + } stop(); } } \ No newline at end of file diff --git a/java/src/pm/Client.java b/java/src/pm/Client.java index f86bf87..a064d6b 100644 --- a/java/src/pm/Client.java +++ b/java/src/pm/Client.java @@ -1,36 +1,51 @@ package pm; -import pm.event.router.GlobalRouter; -import pm.exception.event.spreader.NetworkSpreaderException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; -public class Client extends Manager { - public static final String TITLE = "Client"; - - public static final String IP = "192.168.1.100"; +import pm.device.javainput.extreme3d.Extreme3DDevice; +import pm.device.javainput.rumblepad.RumblepadDevice; +import pm.device.jintellitype.JIntellitypeDevice; +import pm.device.network.NetworkDevice; +import pm.device.panel.PanelDevice; +import pm.device.player.PlayerDevice; +import pm.device.wiimote.WiimoteDevice; +import pm.event.EventRouter; +import pm.event.router.GlobalRouter; +import pm.exception.event.router.GlobalRouterException; + +public class Client { + protected Log log = LogFactory.getLog(getClass()); + + public static final String IP = "127.0.0.1"; public static final int PORT = 6789; - public Client(String ip, int port) throws NetworkSpreaderException { - super(new GlobalRouter(ip, port)); - } + protected EventRouter eventRouter; + protected Device[] deviceArray; - public Client() throws NetworkSpreaderException { + public Client() throws GlobalRouterException { this(IP, PORT); } + public Client(String ip, int port) throws GlobalRouterException { + eventRouter = new GlobalRouter(ip, port); + deviceArray = new Device[] { + new WiimoteDevice(), + new PanelDevice(), + new JIntellitypeDevice(), + new PlayerDevice(), + new RumblepadDevice(), + new Extreme3DDevice(), + new NetworkDevice()}; + } + public void start() { - initialise(); - super.start(false); + log.debug("Client"); + Mimis mimis = new Mimis(eventRouter, deviceArray); + mimis.start(); } public static void main(String[] args) { - try { - new Client().start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public String title() { - return TITLE; + new Main().start(); } } diff --git a/java/src/pm/Device.java b/java/src/pm/Device.java index 43cdfa8..090a370 100644 --- a/java/src/pm/Device.java +++ b/java/src/pm/Device.java @@ -10,9 +10,9 @@ import pm.macro.State; import pm.macro.state.Hold; import pm.macro.state.Press; import pm.macro.state.Release; -import pm.selector.Selectable; +import pm.manager.Titled; -public abstract class Device extends EventHandler implements Selectable { +public abstract class Device extends EventHandler implements Titled, Exitable { protected String title; protected boolean active; protected SequenceListener sequenceListener; @@ -84,7 +84,9 @@ public abstract class Device extends EventHandler implements Selectable { } public void exit() { - deactivate(); + if (active()) { + deactivate(); + } stop(); } } diff --git a/java/src/pm/Event.java b/java/src/pm/Event.java index 51ec275..7470111 100644 --- a/java/src/pm/Event.java +++ b/java/src/pm/Event.java @@ -17,13 +17,11 @@ public class Event { } public boolean compatible(EventListener eventListener) { - System.out.println("Event compatible()"); - System.out.println(eventListener); switch (target) { case ALL: return true; - case MANAGER: - return eventListener instanceof Manager; + case MIMIS: + return eventListener instanceof Mimis; case DEVICES: return eventListener instanceof Device; case APPLICATIONS: diff --git a/java/src/pm/Exitable.java b/java/src/pm/Exitable.java new file mode 100644 index 0000000..4f9fc25 --- /dev/null +++ b/java/src/pm/Exitable.java @@ -0,0 +1,5 @@ +package pm; + +public interface Exitable { + public void exit(); +} diff --git a/java/src/pm/GUI.java b/java/src/pm/GUI.java index f7c6013..bd79f12 100644 --- a/java/src/pm/GUI.java +++ b/java/src/pm/GUI.java @@ -2,11 +2,14 @@ package pm; import java.awt.GridLayout; import java.awt.TextArea; +import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.JSeparator; +import javax.swing.JToggleButton; +import javax.swing.SwingConstants; +import javax.swing.WindowConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,37 +22,44 @@ public class GUI extends JFrame { protected static final String APPLICATION_TITLE = "Applications"; protected static final String DEVICE_TITLE = "Devices"; - public GUI(Application[] applicationArray, Device[] deviceArray) { + protected Mimis mimis; + + public GUI(Mimis mimis, Manager applicationManager, Manager deviceManager) { super(TITLE); + this.mimis = mimis; + createFrame(applicationManager, deviceManager); + } + + protected void createFrame(Manager applicationManager, Manager deviceManager) { setLayout(new GridLayout(0, 1)); - //add(new JSeparator()); - JPanel controlPanel = createControlPanel(applicationArray, deviceArray); - JPanel feedbackPanel = createFeedbackPanel(); + JPanel controlPanel = createControlPanel(applicationManager, deviceManager); add(controlPanel); + JPanel feedbackPanel = createFeedbackPanel(); add(feedbackPanel); setResizable(false); setVisible(true); + setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); pack(); } - protected JPanel createControlPanel(Application[] applicationArray, Device[] deviceArray) { + protected JPanel createControlPanel(Manager applicationManager, Manager deviceManager) { JPanel controlPanel = new JPanel(new GridLayout(1, 0)); - Selector applicationSelector = new Selector(APPLICATION_TITLE); - for (Application application : applicationArray) { - applicationSelector.add(application, application.title()); - } - controlPanel.add(applicationSelector); - - - Selector deviceSelector = new Selector(DEVICE_TITLE); - for (Device device : deviceArray) { - deviceSelector.add(device, device.title()); - } - controlPanel.add(deviceSelector); - + JPanel applicationPanel = createManagerPanel(applicationManager, APPLICATION_TITLE); + controlPanel.add(applicationPanel); + JPanel devicePanel = createManagerPanel(deviceManager, DEVICE_TITLE); + controlPanel.add(devicePanel); return controlPanel; } - + + protected JPanel createManagerPanel(Manager manager, String title) { + JPanel panel = new JPanel(new GridLayout(0, 1)); + panel.add(new JLabel(title, SwingConstants.CENTER)); + for (JToggleButton button : manager.getButtons()) { + panel.add(button); + } + return panel; + } + protected JPanel createFeedbackPanel() { JPanel feedbackPanel = new JPanel(); TextArea textArea = new TextArea(); @@ -57,4 +67,17 @@ public class GUI extends JFrame { feedbackPanel.add(textArea); return feedbackPanel; } + + protected void processWindowEvent(WindowEvent e) { + if (e.getID() == WindowEvent.WINDOW_CLOSING) { + log.debug("Window closing"); + exit(); + mimis.exit(); + } + } + + protected void exit() { + log.debug("Dispose"); + dispose(); + } } diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index c300cf9..39682f5 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -16,63 +16,39 @@ import pm.device.network.NetworkDevice; import pm.device.panel.PanelDevice; import pm.device.player.PlayerDevice; import pm.device.wiimote.WiimoteDevice; +import pm.event.EventRouter; import pm.event.router.LocalRouter; -import pm.util.ArrayCycle; -import pm.value.Action; -public class Main extends Manager { +public class Main { protected Log log = LogFactory.getLog(getClass()); - protected ArrayCycle applicationCycle; + + protected EventRouter eventRouter; + protected Application[] applicationArray; + protected Device[] deviceArray; public Main() { - super(new LocalRouter()); - } - - protected void action(Action action) { - log.debug(String.format("action(%s)", action)); - switch (action) { - case NEXT: - eventRouter.set(applicationCycle.next()); - System.out.println(applicationCycle.current()); - break; - case PREVIOUS: - eventRouter.set(applicationCycle.previous()); - System.out.println(applicationCycle.current()); - break; - case EXIT: - exit(); - break; - } - } - - public void start() { - super.start(); - Application[] applicationArray = new Application[] { + eventRouter = new LocalRouter(); + applicationArray = new Application[] { new iTunesApplication(), new GomPlayerApplication(), new WMPApplication(), new MPCApplication(), new VLCApplication(), new WinampApplication()}; - applicationCycle = new ArrayCycle(applicationArray); - Device[] deviceArray = new Device[] { - new WiimoteDevice(), - new PanelDevice(), - new JIntellitypeDevice(), - new PlayerDevice(), - new RumblepadDevice(), - new Extreme3DDevice(), - new NetworkDevice()}; - GUI gui = new GUI(applicationArray, deviceArray); - eventRouter.set(applicationCycle.current()); - super.start(false); + deviceArray = new Device[] { + new WiimoteDevice(), + new PanelDevice(), + new JIntellitypeDevice(), + new PlayerDevice(), + new RumblepadDevice(), + new Extreme3DDevice(), + new NetworkDevice()}; } - public void exit() { - System.out.println("Exit applications..."); - for (Application application : applicationCycle) { - application.exit(); - } + public void start() { + log.debug("Main"); + Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray); + mimis.start(); } public static void main(String[] args) { diff --git a/java/src/pm/Manager.java b/java/src/pm/Manager.java index 685bb43..271264d 100644 --- a/java/src/pm/Manager.java +++ b/java/src/pm/Manager.java @@ -1,47 +1,61 @@ package pm; -import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import javax.swing.JToggleButton; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import pm.device.javainput.extreme3d.Extreme3DDevice; -import pm.device.javainput.rumblepad.RumblepadDevice; -import pm.device.jintellitype.JIntellitypeDevice; -import pm.device.network.NetworkDevice; -import pm.device.panel.PanelDevice; -import pm.device.player.PlayerDevice; -import pm.device.wiimote.WiimoteDevice; -import pm.event.EventHandler; -import pm.event.EventRouter; +import pm.manager.SelectButton; +import pm.manager.Titled; -public abstract class Manager extends EventHandler { +public class Manager extends Worker { protected Log log = LogFactory.getLog(getClass()); + protected static final long serialVersionUID = 1L; + protected static final int INTERVAL = 5000; - protected ArrayList deviceList; - - public Manager(EventRouter eventRouter) { - EventHandler.initialise(eventRouter); - eventRouter.start(); + protected T[] manageableArray; + protected Map> buttonMap; + + public Manager(String title) { + log.debug("Manager constructed"); } - public void start() { - initialise(); - Device[] deviceArray = new Device[] { - new WiimoteDevice(), - new PanelDevice(), - new JIntellitypeDevice(), - new PlayerDevice(), - new RumblepadDevice(), - new Extreme3DDevice(), - new NetworkDevice()}; + public Manager(T[] manageableArray) { + this.manageableArray = manageableArray; + createButtons(); } - public void exit() { - log.debug("Exit devices..."); - for (Device device : deviceList) { - device.exit(); + public void stop() { + super.stop(); + for (T manageable : manageableArray) { + manageable.exit(); } - stop(); + super.stop(); + } + + protected void createButtons() { + buttonMap = new HashMap>(); + for (T manageable : manageableArray) { + SelectButton button = new SelectButton(manageable); + buttonMap.put(manageable, button); + } + } + + protected JToggleButton[] getButtons() { + return buttonMap.values().toArray(new JToggleButton[]{}); + } + + protected void work() { + long before = Calendar.getInstance().getTimeInMillis(); + for (T manageable : manageableArray) { + boolean active = manageable.active(); + buttonMap.get(manageable).setPressed(active); + } + long after = Calendar.getInstance().getTimeInMillis(); + int sleep = INTERVAL - (int) (after - before); + sleep(sleep); } } diff --git a/java/src/pm/Mimis.java b/java/src/pm/Mimis.java new file mode 100644 index 0000000..b4d4ff9 --- /dev/null +++ b/java/src/pm/Mimis.java @@ -0,0 +1,86 @@ +package pm; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import pm.event.EventHandler; +import pm.event.EventRouter; +import pm.util.ArrayCycle; +import pm.value.Action; + +public class Mimis extends EventHandler { + protected Log log = LogFactory.getLog(getClass()); + + protected ArrayCycle applicationCycle; + protected Device[] deviceArray; + protected Application[] applicationArray; + protected GUI gui; + + protected Manager applicationManager; + protected Manager deviceManager; + + public Mimis(EventRouter eventRouter) { + this(eventRouter, new Application[] {}, new Device[] {}); + } + + public Mimis(EventRouter eventRouter, Application[] applicationArray) { + this(eventRouter, applicationArray, new Device[] {}); + } + + public Mimis(EventRouter eventRouter, Device[] deviceArray) { + this(eventRouter, new Application[] {}, deviceArray); + } + + public Mimis(EventRouter eventRouter, Application[] applicationArray, Device[] deviceArray) { + EventHandler.initialise(eventRouter); + applicationManager = new Manager(applicationArray); + deviceManager = new Manager(deviceArray); + + this.applicationArray = applicationArray; + this.deviceArray = deviceArray; + applicationCycle = new ArrayCycle(applicationArray); + } + + public void start() { + log.debug("Start managers"); + applicationManager.start(); + deviceManager.start(); + + log.debug("Create gui"); + gui = new GUI(this, applicationManager, deviceManager); + + if (applicationCycle.size() > 0) { + log.debug("Initialise application cycle"); + eventRouter.set(applicationCycle.current()); + } + super.start(false); + } + + public void exit() { + log.debug("Stop event router"); + eventRouter.stop(); + + log.debug("Stop managers"); + applicationManager.stop(); + deviceManager.stop(); + + stop(); + } + + protected void action(Action action) { + log.debug(String.format("action(%s)", action)); + switch (action) { + case NEXT: + eventRouter.set(applicationCycle.next()); + System.out.println(applicationCycle.current()); + break; + case PREVIOUS: + eventRouter.set(applicationCycle.previous()); + System.out.println(applicationCycle.current()); + break; + case EXIT: + exit(); + break; + } + } +} diff --git a/java/src/pm/Selector.java b/java/src/pm/Selector.java deleted file mode 100644 index 8394901..0000000 --- a/java/src/pm/Selector.java +++ /dev/null @@ -1,29 +0,0 @@ -package pm; - -import java.awt.GridLayout; - -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.SwingConstants; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import pm.selector.SelectButton; - -public class Selector extends JPanel { - protected Log log = LogFactory.getLog(getClass()); - protected static final long serialVersionUID = 1L; - - public Selector(String title) { - setLayout(new GridLayout(0, 1)); - add(new JLabel(title, SwingConstants.CENTER)); - log.debug("Selector constructed"); - } - - protected void add(T worker, String title) { - SelectButton button = new SelectButton(worker, title); - add(button); - log.debug(String.format("Item added: %s", title)); - } -} diff --git a/java/src/pm/Worker.java b/java/src/pm/Worker.java index e02c4b4..6670d5b 100644 --- a/java/src/pm/Worker.java +++ b/java/src/pm/Worker.java @@ -8,34 +8,36 @@ public abstract class Worker implements Runnable { protected static final boolean THREAD = true; protected static final int SLEEP = 100; - public static final int CHECK_ALIVE_INTERVAL = 1000; protected boolean running = false; protected boolean active = false; - protected boolean connected; - - protected Object lock; - public void start(boolean thread) { + log.debug("Start"); running = true; + activate(); if (thread) { + log.debug("Start thread"); new Thread(this).start(); } else { + log.debug("Run directly"); run(); } - activate(); } public void start() { start(THREAD); - monitorConnection(); - connected = false; } public void stop() { + log.debug("Stop"); + if (active()) { + deactivate(); + } running = false; - deactivate(); + synchronized (this) { + notifyAll(); + } } protected void sleep(int time) { @@ -57,12 +59,6 @@ public abstract class Worker implements Runnable { } public void activate() { - if (!running) { - start(); - } - synchronized (this) { - notifyAll(); - } active = true; } @@ -70,13 +66,6 @@ public abstract class Worker implements Runnable { active = false; } - public void deactivate(boolean stop) { - deactivate(); - if (stop && running) { - stop(); - } - } - public final void run() { while (running) { if (active()) { @@ -84,41 +73,15 @@ public abstract class Worker implements Runnable { } else { try { synchronized (this) { + log.debug("Wait"); wait(); } } catch (InterruptedException e) { log.info(e); } } - } + } } protected abstract void work(); - - public boolean connected() { - return connected; - } - - protected void monitorConnection() { - new Thread() { - protected long timestamp = System.currentTimeMillis(); - - public void run() { - while (super.isAlive()) { - if (timestamp + (2 * CHECK_ALIVE_INTERVAL) > System.currentTimeMillis()) { - timestamp = System.currentTimeMillis(); - connected = true; - log.debug("Het gaat nog helemaal goed"); - } else { - log.debug("Het interval is overschreden"); - return; - } - try { - Thread.sleep(CHECK_ALIVE_INTERVAL); - } catch (InterruptedException e) {} - } - connected = false; - } - }.start(); - } } diff --git a/java/src/pm/application/itunes/iTunesApplication.java b/java/src/pm/application/itunes/iTunesApplication.java index cf6b417..02eb96e 100644 --- a/java/src/pm/application/itunes/iTunesApplication.java +++ b/java/src/pm/application/itunes/iTunesApplication.java @@ -36,6 +36,19 @@ public class iTunesApplication extends Application implements iTunesEventsInterf super.activate(); } + public boolean active() { + log.info("Check iTunes"); + try { + iTunes.getCurrentTrack(); + active = true; + } catch (Exception e) { + log.fatal(e); + active = false; + } + log.info(active); + return active; + } + public void deactivate() { try { synchronized (iTunes) { diff --git a/java/src/pm/client/LanTextClient.java b/java/src/pm/client/LanTextClient.java deleted file mode 100644 index b8cc90a..0000000 --- a/java/src/pm/client/LanTextClient.java +++ /dev/null @@ -1,38 +0,0 @@ -package pm.client; - -import java.io.PrintStream; -import java.net.Socket; -import java.util.Scanner; - -public class LanTextClient { - static final String HOST = "127.0.0.1"; // localhost - static final int PORT = 1234; - - protected Socket socket; - protected Scanner input; - protected PrintStream output; - - public LanTextClient(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 LanTextClient() { - this(HOST, PORT); - } - - protected void start() { - while (true) { - output.println(input.nextLine()); - } - } - - public static void main(String[] argv) { - new LanTextClient().start(); - } -} diff --git a/java/src/pm/device/javainput/JavaInputDevice.java b/java/src/pm/device/javainput/JavaInputDevice.java index f9f7c20..558b1a8 100644 --- a/java/src/pm/device/javainput/JavaInputDevice.java +++ b/java/src/pm/device/javainput/JavaInputDevice.java @@ -24,7 +24,7 @@ public abstract class JavaInputDevice extends Device { protected JavaInputListener javaInputListener; protected Button previousDirectionalButton; - public void initialise(String name) throws DeviceInitialiseException { + public void activate(String name) throws DeviceInitialiseException { try { javaInputListener = new JavaInputListener(this, getDevice(name)); javaInputListener.start(); @@ -33,7 +33,7 @@ public abstract class JavaInputDevice extends Device { } } - public void exit() { + public void deactivate() { javaInputListener.exit(); } diff --git a/java/src/pm/device/javainput/extreme3d/Extreme3DDevice.java b/java/src/pm/device/javainput/extreme3d/Extreme3DDevice.java index 19ca4b0..7175180 100644 --- a/java/src/pm/device/javainput/extreme3d/Extreme3DDevice.java +++ b/java/src/pm/device/javainput/extreme3d/Extreme3DDevice.java @@ -40,7 +40,7 @@ public class Extreme3DDevice extends JavaInputDevice { new Press(Extreme3DButton.TWO), new Press(Extreme3DButton.ELEVEN), new Release(Extreme3DButton.ONE)), - new Task(Target.MANAGER, Action.EXIT)); + new Task(Target.MIMIS, Action.EXIT)); } catch (MacroException e) { e.printStackTrace(); } diff --git a/java/src/pm/device/jintellitype/JIntellitypeDevice.java b/java/src/pm/device/jintellitype/JIntellitypeDevice.java index e6da617..97752d9 100644 --- a/java/src/pm/device/jintellitype/JIntellitypeDevice.java +++ b/java/src/pm/device/jintellitype/JIntellitypeDevice.java @@ -33,10 +33,10 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell jit.addIntellitypeListener(this); add( new Hotkey(Key.PRIOR), - new Task(Target.MANAGER, Action.PREVIOUS)); + new Task(Target.MIMIS, Action.PREVIOUS)); add( new Hotkey(Key.NEXT), - new Task(Target.MANAGER, Action.NEXT)); + new Task(Target.MIMIS, Action.NEXT)); add( new Press(CommandButton.VOLUME_DOWN), new Task(Target.APPLICATIONS, Action.VOLUME_DOWN)); @@ -45,7 +45,7 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell new Task(Target.APPLICATIONS, Action.VOLUME_UP)); add( new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'), - new Task(Target.MANAGER, Action.EXIT)); + new Task(Target.MIMIS, Action.EXIT)); add( new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'), new Task(Target.APPLICATION, Action.NEXT)); diff --git a/java/src/pm/device/lirc/LircDevice.java b/java/src/pm/device/lirc/LircDevice.java new file mode 100644 index 0000000..c81767d --- /dev/null +++ b/java/src/pm/device/lirc/LircDevice.java @@ -0,0 +1,24 @@ +package pm.device.lirc; + +import pm.Device; + +public class LircDevice extends Device { + protected static final String TITLE = "Lirc"; + + protected LircService lircService; + + public LircDevice(String title) { + super(title); + lircService = new LircService(); + } + + public void activate() { + lircService.activate(); + super.activate(); + } + + public void deactivate() { + lircService.deactivate(); + super.deactivate(); + } +} diff --git a/java/src/pm/device/lirc/LircService.java b/java/src/pm/device/lirc/LircService.java new file mode 100644 index 0000000..310887d --- /dev/null +++ b/java/src/pm/device/lirc/LircService.java @@ -0,0 +1,70 @@ +package pm.device.lirc; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.Socket; +import java.net.UnknownHostException; + +import pm.Worker; + +public class LircService extends Worker { + public static final String IP = "127.0.0.1"; + public static final int PORT = 6789; + + protected String ip; + protected int port; + protected Socket socket; + protected InputStream inputStream; + protected OutputStream outputStream; + protected BufferedReader bufferedReader; + protected PrintWriter printWriter; + + public LircService() { + this(IP, PORT); + } + + public LircService(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public void activate() { + try { + socket = new Socket(ip, port); + + inputStream = socket.getInputStream(); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + outputStream = socket.getOutputStream(); + printWriter = new PrintWriter(outputStream); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void deactivate() { + try { + inputStream.close(); + outputStream.close(); + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + super.deactivate(); + } + + public void work() { + try { + String string = bufferedReader.readLine(); + System.out.println(string); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/java/src/pm/device/network/NetworkDevice.java b/java/src/pm/device/network/NetworkDevice.java index fd25ca3..673e53b 100644 --- a/java/src/pm/device/network/NetworkDevice.java +++ b/java/src/pm/device/network/NetworkDevice.java @@ -33,7 +33,7 @@ public class NetworkDevice extends Device { this(PORT); } - public void initialise() { + public void activate() { try { server = new Server(port); server.start(); @@ -42,7 +42,7 @@ public class NetworkDevice extends Device { } } - public void exit() { + public void deactivate() { server.stop(); } @@ -65,19 +65,21 @@ public class NetworkDevice extends Device { System.out.println("Server started"); } - public void run() { - while (running) { - 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) {} - } + public void work() { + 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) {} + } + + public void stop() { for (Client client : clientList) { client.stop(); } + super.stop(); } } @@ -93,7 +95,7 @@ public class NetworkDevice extends Device { clientList.add(this); } - public void run() { + public void work() { try { Object object; do { @@ -109,6 +111,9 @@ public class NetworkDevice extends Device { } catch (ClassNotFoundException e) { e.printStackTrace(); } + } + + public void stop() { System.out.println("stoppen"); try { disconnect(); @@ -117,7 +122,7 @@ public class NetworkDevice extends Device { clientList.remove(this); } } - + public void send(Object object) throws IOException { objectOutputStream.writeObject(object); } diff --git a/java/src/pm/device/panel/PanelDevice.java b/java/src/pm/device/panel/PanelDevice.java index 4aab111..aca490b 100644 --- a/java/src/pm/device/panel/PanelDevice.java +++ b/java/src/pm/device/panel/PanelDevice.java @@ -15,7 +15,7 @@ public class PanelDevice extends Device implements PanelButtonListener { super(TITLE); } - public void initialise() { + public void activate() { panel = new Panel(this); //panel.updateTime(12342398); //panel.updatePosition(43); @@ -31,7 +31,7 @@ public class PanelDevice extends Device implements PanelButtonListener { add(new Press(PanelButton.VOLUME_UP), new Task(Target.APPLICATION, Action.VOLUME_UP)); } - public void exit() { + public void deactivate() { panel.dispose(); } diff --git a/java/src/pm/device/wiimote/WiimoteDevice.java b/java/src/pm/device/wiimote/WiimoteDevice.java index ca45ffb..fca363e 100644 --- a/java/src/pm/device/wiimote/WiimoteDevice.java +++ b/java/src/pm/device/wiimote/WiimoteDevice.java @@ -177,9 +177,9 @@ public class WiimoteDevice extends Device implements GestureListener { } public void deactivate() { - wiimote.deactivateMotionSensing(); + wiimote.deactivateMotionSensing(); } - + public void exit() { wiimoteService.exit(); super.exit(); diff --git a/java/src/pm/event/EventHandler.java b/java/src/pm/event/EventHandler.java index aedbb83..6c9cf92 100644 --- a/java/src/pm/event/EventHandler.java +++ b/java/src/pm/event/EventHandler.java @@ -42,9 +42,4 @@ public abstract class EventHandler extends EventListener { } protected void action(Action action) {} - - public void activate() { - super.activate(); - add(new Feedback()); - } } \ No newline at end of file diff --git a/java/src/pm/event/EventListener.java b/java/src/pm/event/EventListener.java index 62172fc..034a034 100644 --- a/java/src/pm/event/EventListener.java +++ b/java/src/pm/event/EventListener.java @@ -7,30 +7,40 @@ import pm.Worker; public abstract class EventListener extends Worker { protected Queue eventQueue; - protected Object available; + protected Object work; public EventListener() { eventQueue = new ConcurrentLinkedQueue(); - available = new Object(); + work = new Object(); } public void add(Event event) { eventQueue.add(event); - synchronized (available) { - available.notifyAll(); + synchronized (work) { + work.notifyAll(); } } public final void work() { while (eventQueue.isEmpty()) { - synchronized (available) { + synchronized (work) { try { - available.wait(); + work.wait(); } catch (InterruptedException e) {} + if (!running) { + return; + } } } event(eventQueue.poll()); } + public void stop() { + super.stop(); + synchronized (work) { + work.notifyAll(); + } + } + public abstract void event(Event event); } \ No newline at end of file diff --git a/java/src/pm/event/router/GlobalRouter.java b/java/src/pm/event/router/GlobalRouter.java index d5266db..74a62ae 100644 --- a/java/src/pm/event/router/GlobalRouter.java +++ b/java/src/pm/event/router/GlobalRouter.java @@ -9,20 +9,20 @@ import pm.Event; import pm.Worker; import pm.event.EventRouter; import pm.event.Feedback; -import pm.exception.event.spreader.NetworkSpreaderException; +import pm.exception.event.router.GlobalRouterException; public class GlobalRouter extends EventRouter { protected Socket socket; protected ObjectOutputStream objectOutputStream; protected ObjectInputStream objectInputStream; - public GlobalRouter(String ip, int port) throws NetworkSpreaderException { + public GlobalRouter(String ip, int port) throws GlobalRouterException { try { socket = new Socket(ip, port); objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); objectInputStream = new ObjectInputStream(socket.getInputStream()); new Worker() { - public void run() { + public void work() { try { Object object; do { @@ -41,7 +41,7 @@ public class GlobalRouter extends EventRouter { return; } catch (UnknownHostException e) { } catch (IOException e) {} - throw new NetworkSpreaderException(); + throw new GlobalRouterException(); } public void event(Event event) { diff --git a/java/src/pm/exception/event/router/GlobalRouterException.java b/java/src/pm/exception/event/router/GlobalRouterException.java new file mode 100644 index 0000000..589ef13 --- /dev/null +++ b/java/src/pm/exception/event/router/GlobalRouterException.java @@ -0,0 +1,7 @@ +package pm.exception.event.router; + +import pm.exception.event.SpreaderException; + +public class GlobalRouterException extends SpreaderException { + protected static final long serialVersionUID = 1L; +} diff --git a/java/src/pm/exception/event/spreader/NetworkSpreaderException.java b/java/src/pm/exception/event/spreader/NetworkSpreaderException.java deleted file mode 100644 index 8b16718..0000000 --- a/java/src/pm/exception/event/spreader/NetworkSpreaderException.java +++ /dev/null @@ -1,7 +0,0 @@ -package pm.exception.event.spreader; - -import pm.exception.event.SpreaderException; - -public class NetworkSpreaderException extends SpreaderException { - protected static final long serialVersionUID = 1L; -} diff --git a/java/src/pm/manager/SelectButton.java b/java/src/pm/manager/SelectButton.java new file mode 100644 index 0000000..6832f67 --- /dev/null +++ b/java/src/pm/manager/SelectButton.java @@ -0,0 +1,41 @@ +package pm.manager; + +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import javax.swing.Action; +import javax.swing.JToggleButton; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import pm.Worker; + +public class SelectButton extends JToggleButton implements ItemListener { + protected Log log = LogFactory.getLog(getClass()); + + protected static final long serialVersionUID = 1L; + protected T activatable; + protected Action action; + + public SelectButton(T activatable) { + this.activatable = activatable; + setText(activatable.title()); + addItemListener(this); + //getModel().setPressed(true); + } + + public void itemStateChanged(ItemEvent itemEvent) { + int state = itemEvent.getStateChange(); + if (state == ItemEvent.SELECTED) { + System.out.println("Selected"); + activatable.activate(); + } else { + System.out.println("Deselected"); + activatable.deactivate(); + } + } + + public void setPressed(boolean pressed) { + getModel().setPressed(pressed); + } +} diff --git a/java/src/pm/manager/Titled.java b/java/src/pm/manager/Titled.java new file mode 100644 index 0000000..2057bcd --- /dev/null +++ b/java/src/pm/manager/Titled.java @@ -0,0 +1,5 @@ +package pm.manager; + +public interface Titled { + public String title(); +} diff --git a/java/src/pm/selector/SelectButton.java b/java/src/pm/selector/SelectButton.java deleted file mode 100644 index 3a39780..0000000 --- a/java/src/pm/selector/SelectButton.java +++ /dev/null @@ -1,52 +0,0 @@ -package pm.selector; - -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; -import javax.swing.JToggleButton; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import pm.Worker; - -public class SelectButton extends JToggleButton implements ItemListener { - protected Log log = LogFactory.getLog(getClass()); - - protected static final long serialVersionUID = 1L; - protected T activatable; - - public static final int CHECK_ALIVE_INTERVAL = 1000; - - public SelectButton(T activatable, String title) { - this.activatable = activatable; - setText(title); - addItemListener(this); - monitorApplication(); - } - - public void itemStateChanged(ItemEvent itemEvent) { - int state = itemEvent.getStateChange(); - if (state == ItemEvent.SELECTED) { - System.out.println("Selected"); - activatable.activate(); - } else { - System.out.println("Deselected"); - activatable.deactivate(); - } - } - - protected void monitorApplication() { - new Thread() { - public void run() { - while (super.isAlive()) { - if (!activatable.connected()) { - //log.debug("Nu moet het knopje uit gaan!"); - } - try { - Thread.sleep(CHECK_ALIVE_INTERVAL); - } catch (InterruptedException e) {} - } - } - }.start(); - } -} diff --git a/java/src/pm/selector/Selectable.java b/java/src/pm/selector/Selectable.java deleted file mode 100644 index 8160f21..0000000 --- a/java/src/pm/selector/Selectable.java +++ /dev/null @@ -1,5 +0,0 @@ -package pm.selector; - -public interface Selectable { - public String title(); -} diff --git a/java/src/pm/util/ArrayCycle.java b/java/src/pm/util/ArrayCycle.java index 895dcdb..22ab970 100644 --- a/java/src/pm/util/ArrayCycle.java +++ b/java/src/pm/util/ArrayCycle.java @@ -8,10 +8,12 @@ public class ArrayCycle extends ArrayList { protected int index = 0; //protected Object nonEmpty; - public ArrayCycle(E... elementArary) { + public ArrayCycle(E... elementArray) { + if (elementArray != null) { //nonEmpty = new Object(); - for (E element : elementArary) { - add(element); + for (E element : elementArray) { + add(element); + } } } diff --git a/java/src/pm/value/Target.java b/java/src/pm/value/Target.java index b472072..3da473f 100644 --- a/java/src/pm/value/Target.java +++ b/java/src/pm/value/Target.java @@ -1,5 +1,5 @@ package pm.value; public enum Target { - ALL, MANAGER, DEVICES, APPLICATIONS, APPLICATION, SELF; + ALL, MIMIS, DEVICES, APPLICATIONS, APPLICATION, SELF; } \ No newline at end of file