diff --git a/java/src/pm/device/lantextinput/LanTextClient.java b/java/src/pm/device/lantextinput/LanTextClient.java new file mode 100644 index 0000000..ef586fd --- /dev/null +++ b/java/src/pm/device/lantextinput/LanTextClient.java @@ -0,0 +1,35 @@ +package pm.device.lantextinput; + +import java.io.PrintStream; +import java.net.Socket; +import java.util.Scanner; + +public class LanTextClient { + static final String HOST = "127.0.0.1"; //localhost + static final int PORT = 1234; + + protected boolean run; + protected Socket socket; + protected Scanner input; + protected PrintStream output; + + LanTextClient(){ + try { + socket = new Socket(HOST, PORT); + input = new Scanner(System.in); + output = new PrintStream(socket.getOutputStream()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected void start(){ + while(run){ + output.println(input.nextLine()); + } + } + + public static void main(String[] argv) { + new LanTextClient().start(); + } +} diff --git a/java/src/pm/device/lantextinput/LanTextDevice.java b/java/src/pm/device/lantextinput/LanTextDevice.java new file mode 100644 index 0000000..cfb0aa9 --- /dev/null +++ b/java/src/pm/device/lantextinput/LanTextDevice.java @@ -0,0 +1,35 @@ +package pm.device.lantextinput; + +import java.io.IOException; +import java.net.ServerSocket; +import pm.Device; + +public class LanTextDevice extends Device implements Runnable { + static final int PORT = 1234; + + ServerSocket socket; + + public LanTextDevice() { + try { + socket = new ServerSocket(PORT); + } catch (IOException e) { + e.printStackTrace(); + } + initialise(); + } + + public void initialise() { + new Thread(this).start(); + } + + public void run() { + while(true){ + try { + new LanTextListener(socket.accept()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/java/src/pm/device/lantextinput/LanTextListener.java b/java/src/pm/device/lantextinput/LanTextListener.java new file mode 100644 index 0000000..5198fcb --- /dev/null +++ b/java/src/pm/device/lantextinput/LanTextListener.java @@ -0,0 +1,56 @@ +package pm.device.lantextinput; + +import java.io.IOException; +import java.net.Socket; +import java.util.Scanner; + +import pm.Action; +import pm.Target; +import pm.Task; +import pm.task.TaskGatherer; + +public class LanTextListener implements Runnable{ + static final int SLEEP = 50; + + protected boolean run; + protected Socket socket; + protected Scanner input; + + public LanTextListener(Socket socket){ + this.socket = socket; + try { + this.input = new Scanner(socket.getInputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + run = true; + new Thread(this).start(); + } + + public void run() { + while(run && socket.isConnected() && input.hasNext()) { + String textinput = input.next().toUpperCase(); + if(textinput != null) { + try { + TaskGatherer.add( + new Task(Action.valueOf(textinput), Target.APPLICATION)); + } catch(IllegalArgumentException e) { } + } + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + exit(); + } + + protected void exit() { + run = false; + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/java/src/pm/device/textinput/TextDevice.java b/java/src/pm/device/textinput/TextDevice.java new file mode 100644 index 0000000..d9d52fc --- /dev/null +++ b/java/src/pm/device/textinput/TextDevice.java @@ -0,0 +1,45 @@ +package pm.device.textinput; + +import java.util.Scanner; + +import pm.Action; +import pm.Device; +import pm.Target; +import pm.Task; +import pm.task.TaskGatherer; + +public class TextDevice extends Device implements Runnable { + static final int SLEEP = 50; + + protected boolean run; + protected Scanner input; + + public TextDevice() { + initialise(); + run = true; + new Thread(this).start(); + } + + public void initialise() { + input = new Scanner(System.in); + } + + public void run() { + System.out.println("TextDevice activated"); + while(run) { + String textinput = input.next().toUpperCase(); + if(textinput != null) { + System.out.println(textinput); + try { + TaskGatherer.add( + new Task(Action.valueOf(textinput), Target.APPLICATION)); + } catch(IllegalArgumentException e) { } + } + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} diff --git a/java/src/pm/device/textinput/TextinputDevice.java b/java/src/pm/device/textinput/TextinputDevice.java deleted file mode 100644 index 503d99c..0000000 --- a/java/src/pm/device/textinput/TextinputDevice.java +++ /dev/null @@ -1,31 +0,0 @@ -package pm.device.textinput; - -import java.util.Scanner; - -import pm.Device; - -public class TextinputDevice extends Device { - static final int SLEEP = 50; - - Scanner textinputScanner; - boolean run; - - public TextinputDevice() { - textinputScanner = new Scanner(System.in); - run = true; - } - - public void initialise() { - while(run) { - String textinput = textinputScanner.next(); - if(textinput != null) { - System.out.println(textinput); - } - try { - Thread.sleep(SLEEP); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } -}