package dasherJava.core.input; import java.io.BufferedReader; import java.io.IOException; import dasherJava.DasherJava; import dasherJava.core.network.SingleSocketConnection; import dasherJava.core.startStop.StartStopAction; public class SocketInput implements InputProvider { private final SingleSocketConnection singleSocketConnection; private final Thread socketInputThread; private volatile float inputX = 0.0f; private volatile float inputY = 0.0f; private volatile StartStopAction startStopAction = StartStopAction.NOTHING; private volatile boolean shouldExit = false; public SocketInput(int port) { singleSocketConnection=new SingleSocketConnection("SocketInput", port); socketInputThread=new Thread(() -> { while (!shouldExit) { BufferedReader reader = singleSocketConnection.getCurrentReader(); if (reader==null) continue; //should happen rarely because getCurrentReader() usually blocks try { String line = reader.readLine(); if (line==null) { System.out.println("SocketInput connection closed: End of stream reached"); singleSocketConnection.closeCurrentConnection(); continue; //get new reader } parseSocketInputLine(line); } catch (IOException ex) { System.out.println("SocketInput connection closed: IOException: "+ex.getMessage()); singleSocketConnection.closeCurrentConnection(); } } System.out.println("SocketInputThread: Exiting"); }, "SocketInputThread"); socketInputThread.start(); } @Override public float getInputX() { return inputX; } @Override public float getInputY() { return inputY; } @Override public StartStopAction getStartStopAction() { StartStopAction tmp = startStopAction; startStopAction=StartStopAction.NOTHING; return tmp; } public void terminate() { shouldExit=true; singleSocketConnection.terminate(); socketInputThread.interrupt(); } private void parseSocketInputLine(String line) { try { String labelX = DasherJava.getSettings().getSocketInputLabelX(); String labelY = DasherJava.getSettings().getSocketInputLabelY(); String labelStartStop = DasherJava.getSettings().getSocketInputLabelStartStop(); if (line.startsWith(labelX)) { float value = Float.parseFloat(line.substring(labelX.length()).strip()); //discard whitespace //between label and value, if any inputX=InputProvider.mapToInputRange(value, DasherJava.getSettings().getSocketInputMinX(), DasherJava.getSettings().getSocketInputMaxX()); } else if (line.startsWith(labelY)) { float value = Float.parseFloat(line.substring(labelY.length()).strip()); //discard whitespace //between label and value, if any inputY=InputProvider.mapToInputRange(value, DasherJava.getSettings().getSocketInputMinY(), DasherJava.getSettings().getSocketInputMaxY()); } else if (line.startsWith(labelStartStop)) { int value = (int) Float.parseFloat(line.substring(labelStartStop.length()).strip()); //discard //whitespace between label and value, if any //only three possible commands, so cast to int to discard decimals switch (value) { case 0: startStopAction=StartStopAction.STOP; break; case 1: startStopAction=StartStopAction.START; break; case 2: startStopAction=StartStopAction.TOGGLE; break; default: System.out.println("Invalid socket input start-stop-command \""+value +"\". Use 0 for stopping, 1 for starting and 2 for toggling between start/stop."); break; } } else System.out.println("Invalid socket input line (unknown label) \""+line+"\""); } catch (NumberFormatException ex) { System.out.println("Invalid socket input line \""+line+"\": NumberFormatException: "+ex.getMessage()); } } }