application cycle verwijderd, structuur input listener aangepast en event manager netter gemaakt.
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
package pm.device.text;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import pm.Listener;
|
||||
import pm.event.Task;
|
||||
import pm.event.EventManager;
|
||||
import pm.exception.task.TaskNotSupportedException;
|
||||
import pm.value.Action;
|
||||
import pm.value.Target;
|
||||
|
||||
public class InputListener extends Listener {
|
||||
protected Scanner input;
|
||||
|
||||
public InputListener(InputStream inputStream) {
|
||||
input = new Scanner(inputStream);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
run = true;
|
||||
while (running()) {
|
||||
String string = input.next().toUpperCase();
|
||||
if(string != null) {
|
||||
try {
|
||||
EventManager.add(
|
||||
new Task(Action.valueOf(string), Target.APPLICATION));
|
||||
} catch(IllegalArgumentException e) {}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(SLEEP);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean running() {
|
||||
return run && input.hasNext();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
package pm.device.text;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import pm.Device;
|
||||
import pm.Listener;
|
||||
import pm.event.EventManager;
|
||||
import pm.event.Task;
|
||||
import pm.value.Action;
|
||||
import pm.value.Target;
|
||||
|
||||
public class TextDevice extends Device {
|
||||
InputListener inputListener;
|
||||
@@ -16,4 +24,33 @@ public class TextDevice extends Device {
|
||||
public void exit() {
|
||||
inputListener.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void add(String string) {
|
||||
EventManager.add(new Task(Action.valueOf(string), Target.APPLICATION));
|
||||
}
|
||||
|
||||
public class InputListener extends Listener {
|
||||
protected Scanner input;
|
||||
|
||||
public InputListener(InputStream inputStream) {
|
||||
input = new Scanner(inputStream);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
run = true;
|
||||
while (run && input.hasNext()) {
|
||||
String string = input.next().toUpperCase();
|
||||
if(string != null) {
|
||||
try {
|
||||
add(string);
|
||||
} catch(IllegalArgumentException e) {}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(SLEEP);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,15 @@ import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import pm.Listener;
|
||||
import pm.device.text.InputListener;
|
||||
import pm.device.text.TextDevice;
|
||||
|
||||
public class SocketListener extends Listener {
|
||||
protected ServerSocket server;
|
||||
protected ArrayList<InputListener> inputListenerList;
|
||||
protected ArrayList<TextDevice.InputListener> inputListenerList;
|
||||
|
||||
public SocketListener(ServerSocket server) {
|
||||
this.server = server;
|
||||
inputListenerList = new ArrayList<InputListener>();
|
||||
inputListenerList = new ArrayList<TextDevice.InputListener>();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -23,7 +23,7 @@ public class SocketListener extends Listener {
|
||||
try {
|
||||
Socket socket = server.accept();
|
||||
InputStream inputStream = socket.getInputStream();
|
||||
InputListener inputListener = new InputListener(inputStream);
|
||||
TextDevice.InputListener inputListener = new TextDevice().new InputListener(inputStream);
|
||||
inputListenerList.add(inputListener);
|
||||
inputListener.start();
|
||||
} catch (IOException e) {
|
||||
@@ -34,7 +34,7 @@ public class SocketListener extends Listener {
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
for (InputListener inputListener : inputListenerList) {
|
||||
for (TextDevice.InputListener inputListener : inputListenerList) {
|
||||
inputListener.stop();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user