From 55eb8c803d05dec9dc5aeb8ef3c0c3c18e20af1a Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Sat, 2 Feb 2019 15:24:30 -0700 Subject: [PATCH] Convert from a server socket to a client socket --- robopipe/bin/.classpath | 6 ++++ robopipe/bin/.gitignore | 2 -- robopipe/bin/.project | 17 +++++++++++ robopipe/src/robopipe/App.java | 52 +++++++++++++--------------------- 4 files changed, 42 insertions(+), 35 deletions(-) create mode 100644 robopipe/bin/.classpath create mode 100644 robopipe/bin/.project diff --git a/robopipe/bin/.classpath b/robopipe/bin/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/robopipe/bin/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/robopipe/bin/.gitignore b/robopipe/bin/.gitignore index 8da1aea..fa9f326 100644 --- a/robopipe/bin/.gitignore +++ b/robopipe/bin/.gitignore @@ -1,3 +1 @@ -/.classpath -/.project /robopipe/ diff --git a/robopipe/bin/.project b/robopipe/bin/.project new file mode 100644 index 0000000..0f6f6a7 --- /dev/null +++ b/robopipe/bin/.project @@ -0,0 +1,17 @@ + + + src + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/robopipe/src/robopipe/App.java b/robopipe/src/robopipe/App.java index 10b0451..38a9e78 100644 --- a/robopipe/src/robopipe/App.java +++ b/robopipe/src/robopipe/App.java @@ -3,13 +3,9 @@ */ package robopipe; -import java.awt.GraphicsEnvironment; -import java.io.Console; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PrintWriter; import java.math.BigDecimal; import java.net.ServerSocket; import java.net.Socket; @@ -24,7 +20,7 @@ import edu.wpi.first.networktables.NetworkTableInstance; public class App { /// TODO: Always remember to set to false before building - static boolean isEclipse = false; + static boolean isEclipse = true; String[] robotValues; List tableEntrys = new ArrayList(); @@ -36,41 +32,23 @@ public class App { Socket clientSocket; OutputStream os; InputStream in; + static boolean EXIT = false; public static void main(String[] args) { - // Thanks to Frezze98 bolalo on StackOverflow for the code to create a batch - // file to start the program in a command window - Console console = System.console(); - if (console == null && !GraphicsEnvironment.isHeadless() && !isEclipse) { - String filename = new File(App.class.getProtectionDomain().getCodeSource() - .getLocation().getPath()).getName(); - try { - File batch = new File("Launcher.bat"); - if (!batch.exists()) { - batch.createNewFile(); - PrintWriter writer = new PrintWriter(batch); - writer.println("@echo off"); - writer.println("java -jar " + filename); - writer.println("exit"); - writer.flush(); - } - Runtime.getRuntime().exec("cmd /c start \"\" " + batch.getPath()); - } catch (IOException e) { - e.printStackTrace(); - } - } else { - System.out.println("Robopipe Start"); + System.out.println("Robopipe Start"); + while (!EXIT) { new App().run(); } + EXIT = true; } public void run() { try { NetworkTableInstance inst = NetworkTableInstance.getDefault(); NetworkTable table = inst.getTable("/SmartDashboard"); - System.out.println("Waiting for Unity client connection..."); - serverSocket = new ServerSocket(portNumber); - clientSocket = serverSocket.accept(); + System.out.println("Connecting to Unity local server..."); + //serverSocket = new ServerSocket(portNumber); + clientSocket = new Socket("127.0.0.1", portNumber); os = clientSocket.getOutputStream(); in = clientSocket.getInputStream(); System.out.println("Connected"); @@ -85,7 +63,7 @@ public class App { inst.startClientTeam(4388); inst.startDSClient(); - while (true) { + while (!EXIT) { toUnity(); } } catch (IOException e) { @@ -100,13 +78,21 @@ public class App { output += "|"; System.out.println("'" + entry.getName() + "'"); } + output = output.substring(0, output.length() - 1); byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8); System.out.println("Sending '" + output + "' of length: " + byteOutput.length + " to Unity..."); os.write(byteOutput); + System.out.println("Waiting for Response..."); - in.read(); - System.out.println("Response Received"); + byte[] byteInput = new byte[4]; + in.read(byteInput); + String stringInput = new String(byteInput, StandardCharsets.UTF_8); + stringInput = stringInput.substring(0, 4); + if (stringInput.equals("EXIT")) { + EXIT = true; + } + System.out.println("Response: " + stringInput); } public static BigDecimal round(double d, int decimalPlace) {