* 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:
50
java/src/pm/device/text/InputListener.java
Normal file
50
java/src/pm/device/text/InputListener.java
Normal 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;
|
||||
}
|
||||
}
|
||||
19
java/src/pm/device/text/TextDevice.java
Normal file
19
java/src/pm/device/text/TextDevice.java
Normal 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();
|
||||
}
|
||||
}
|
||||
27
java/src/pm/device/text/lan/LanTextDevice.java
Normal file
27
java/src/pm/device/text/lan/LanTextDevice.java
Normal 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();
|
||||
}
|
||||
}
|
||||
47
java/src/pm/device/text/lan/SocketListener.java
Normal file
47
java/src/pm/device/text/lan/SocketListener.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user