* pakket value gemaakt voor algemene enums

* LanTextClient in eigen pakket client gezet
* TextDevice en LanTextDevice nog generieker gemaakt:
- standaard thread model toegepast met start en stop methoden
- threads verplaatst buiten devices:
-- devices zouden run methode uit de task listener overriden
-- run methode uit tasklistener final gemaakt
This commit is contained in:
2011-02-22 22:26:01 +00:00
parent 24a49e5d49
commit ac1480d24c
31 changed files with 172 additions and 127 deletions

View File

@@ -0,0 +1,50 @@
package pm.device.text;
import java.io.InputStream;
import java.util.Scanner;
import pm.Task;
import pm.task.TaskManager;
import pm.value.Action;
import pm.value.Target;
public class InputListener implements Runnable {
protected static final int SLEEP = 100;
protected boolean run;
protected Scanner input;
public InputListener(InputStream inputStream) {
input = new Scanner(inputStream);
}
public void start() {
new Thread(this).start();
}
public void run() {
run = true;
while (running()) {
String textinput = input.next().toUpperCase();
if(textinput != null) {
try {
TaskManager.add(
new Task(Action.valueOf(textinput), Target.APPLICATION));
} catch(IllegalArgumentException e) {}
}
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected boolean running() {
return run && input.hasNext();
}
public void stop() {
run = false;
}
}

View File

@@ -0,0 +1,19 @@
package pm.device.text;
import pm.Device;
public class TextDevice extends Device implements Runnable {
InputListener inputListener;
public TextDevice() {
inputListener = new InputListener(System.in);
}
public void initialise() {
inputListener.start();
}
public void exit() {
inputListener.stop();
}
}

View File

@@ -0,0 +1,27 @@
package pm.device.text.lan;
import java.io.IOException;
import java.net.ServerSocket;
import pm.Device;
import pm.exception.device.DeviceInitialiseException;
public class LanTextDevice extends Device {
static final int PORT = 1234;
protected ServerSocket socket;
protected SocketListener socketListener;
public void initialise() throws DeviceInitialiseException {
try {
socket = new ServerSocket(PORT);
socketListener = new SocketListener(socket);
socketListener.start();
} catch (IOException e) {
throw new DeviceInitialiseException();
}
}
public void exit() {
socketListener.stop();
}
}

View File

@@ -0,0 +1,47 @@
package pm.device.text.lan;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import pm.device.text.InputListener;
public class SocketListener implements Runnable {
protected boolean run;
protected ServerSocket server;
protected ArrayList<InputListener> inputListenerList;
public SocketListener(ServerSocket server) {
this.server = server;
inputListenerList = new ArrayList<InputListener>();
}
public void start() {
new Thread(this).start();
}
public void run() {
run = true;
while (run) {
try {
Socket socket = server.accept();
InputStream inputStream = socket.getInputStream();
InputListener inputListener = new InputListener(inputStream);
inputListenerList.add(inputListener);
inputListener.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stop() {
run = false;
for (InputListener inputListener : inputListenerList) {
inputListener.stop();
}
}
}