diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index 9fe6503..b9ec37a 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -28,6 +28,7 @@ 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; @@ -52,22 +53,23 @@ public class Main extends EventListener { } public void initialise() throws DeviceInitialiseException { - add(new JIntellitypeDevice()); + //add(new JIntellitypeDevice()); //add(new PlayerDevice()); //add(new RumblepadDevice()); - add(new WiimoteDevice()); + //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 WinampApplication()); + //add(new iTunesApplication()); //add(new VLCApplication()); //add(new MPCApplication()); startApplications(); @@ -124,7 +126,7 @@ public class Main extends EventListener { } protected void action(Action action) { - System.out.println("Main: " + action); + System.out.println("NetworkClient: " + action); switch (action) { case NEXT: applicationCycle.next(); diff --git a/java/src/pm/event/EventManager.java b/java/src/pm/event/EventManager.java index abea509..f474867 100644 --- a/java/src/pm/event/EventManager.java +++ b/java/src/pm/event/EventManager.java @@ -70,7 +70,7 @@ public class EventManager { } } - public static void add(Task task) { +/* public static void add(Task task) { add(null, task); } @@ -79,7 +79,7 @@ public class EventManager { add(null, task); } } - +*/ public static void remove(EventListener eventListener) { eventListenerList.remove(eventListener); } diff --git a/java/src/pm/network/NetworkClient.java b/java/src/pm/network/NetworkClient.java new file mode 100644 index 0000000..8432774 --- /dev/null +++ b/java/src/pm/network/NetworkClient.java @@ -0,0 +1,145 @@ +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 deviceList; + protected MessageSender messageSender; + + public NetworkClient() { + super(); + deviceList = new ArrayList(); + 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 removeList = new ArrayList(); + 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); + } + } + } +} diff --git a/java/src/pm/network/NetworkServer.java b/java/src/pm/network/NetworkServer.java new file mode 100644 index 0000000..70570e2 --- /dev/null +++ b/java/src/pm/network/NetworkServer.java @@ -0,0 +1,71 @@ +package pm.network; + +import java.io.IOException; +import java.io.InputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +import pm.Device; +import pm.exception.device.DeviceInitialiseException; +import pm.value.Action; + +public class NetworkServer extends Device { + public static final int PORT = 1234; + protected MessageReceiver messageReceiver; + + public NetworkServer() { + messageReceiver = new MessageReceiver(PORT); + messageReceiver.start(); + System.out.println("NetworkServer started"); + } + + public void action(Action action) { + this.action(action); + } + + protected class MessageReceiver extends Thread { + protected ServerSocket server; + + public MessageReceiver(int port) { + try { + server = new ServerSocket(port); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("MessageReceiver started"); + } + + public void run() { + while (true) { + System.out.println("MessageReceiver is waiting for clients"); + try { + Socket socket = server.accept(); + final InputStream inputStream = socket.getInputStream(); + new Thread() { + 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); + } catch(IllegalArgumentException e) {} + } + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }.start(); + System.out.println("Client connected"); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/java/src/pm/value/Action.java b/java/src/pm/value/Action.java index b6f9338..83d92f4 100644 --- a/java/src/pm/value/Action.java +++ b/java/src/pm/value/Action.java @@ -17,4 +17,19 @@ public enum Action { VOLUME_UP, FULLSCREEN, TRAIN, STOP, SAVE, RECOGNIZE, LOAD, SHUFFLE, FADEOUT, QUIT, VISUALISER, LIKE, DISLIKE; + + 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; + } + } + } + return null; + } }