NetworkClient en NetworkServer geimplementeerd, er gaat bij de Client nog wat mis met een nullpointer tot gevolg. Er wordt niet de goede methode aangeroepen wanneer een action er naartoe gestuurd wordt.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
145
java/src/pm/network/NetworkClient.java
Normal file
145
java/src/pm/network/NetworkClient.java
Normal file
@@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
71
java/src/pm/network/NetworkServer.java
Normal file
71
java/src/pm/network/NetworkServer.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user