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.
This commit is contained in:
35
java/src/pm/device/lantextinput/LanTextClient.java
Normal file
35
java/src/pm/device/lantextinput/LanTextClient.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
35
java/src/pm/device/lantextinput/LanTextDevice.java
Normal file
35
java/src/pm/device/lantextinput/LanTextDevice.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
56
java/src/pm/device/lantextinput/LanTextListener.java
Normal file
56
java/src/pm/device/lantextinput/LanTextListener.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
45
java/src/pm/device/textinput/TextDevice.java
Normal file
45
java/src/pm/device/textinput/TextDevice.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user