From e078b1c3d0c5682caa0c8c03117aa48bf5c88793 Mon Sep 17 00:00:00 2001 From: Bram Veenboer Date: Tue, 15 Feb 2011 19:55:11 +0000 Subject: [PATCH] Textinput afgemaakt en afgeleide daarvan voor lan verbinding gemaakt. Getest en werkt beide! In eerste instantie was het de bedoeling om de LanTextDevice de TextDevice te laten extenden en om alleen de Scanner te overriden naar eentje vanaf een socket ipv System.in, maar dit werkte niet goed. Daarnaast worden alle acties nu automatisch naar aleen de applicatie gestuurd, niet naar main. Of het erg is is de vraag, want waarschijnlijk wil je vanaf het netwerk of lokaal via text alleen simpele dingen in de mediaspeler doen. --- .../pm/device/lantextinput/LanTextClient.java | 35 ++++++++++++ .../pm/device/lantextinput/LanTextDevice.java | 35 ++++++++++++ .../device/lantextinput/LanTextListener.java | 56 +++++++++++++++++++ java/src/pm/device/textinput/TextDevice.java | 45 +++++++++++++++ .../pm/device/textinput/TextinputDevice.java | 31 ---------- 5 files changed, 171 insertions(+), 31 deletions(-) create mode 100644 java/src/pm/device/lantextinput/LanTextClient.java create mode 100644 java/src/pm/device/lantextinput/LanTextDevice.java create mode 100644 java/src/pm/device/lantextinput/LanTextListener.java create mode 100644 java/src/pm/device/textinput/TextDevice.java delete mode 100644 java/src/pm/device/textinput/TextinputDevice.java 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(); - } - } - } -}