Convert from a server socket to a client socket

This commit is contained in:
Keenan D. Buckley
2019-02-02 15:24:30 -07:00
parent df5056346b
commit 55eb8c803d
4 changed files with 42 additions and 35 deletions
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
-2
View File
@@ -1,3 +1 @@
/.classpath
/.project
/robopipe/ /robopipe/
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
+19 -33
View File
@@ -3,13 +3,9 @@
*/ */
package robopipe; package robopipe;
import java.awt.GraphicsEnvironment;
import java.io.Console;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@@ -24,7 +20,7 @@ import edu.wpi.first.networktables.NetworkTableInstance;
public class App { public class App {
/// TODO: Always remember to set to false before building /// TODO: Always remember to set to false before building
static boolean isEclipse = false; static boolean isEclipse = true;
String[] robotValues; String[] robotValues;
List<NetworkTableEntry> tableEntrys = new ArrayList<NetworkTableEntry>(); List<NetworkTableEntry> tableEntrys = new ArrayList<NetworkTableEntry>();
@@ -36,41 +32,23 @@ public class App {
Socket clientSocket; Socket clientSocket;
OutputStream os; OutputStream os;
InputStream in; InputStream in;
static boolean EXIT = false;
public static void main(String[] args) { public static void main(String[] args) {
// Thanks to Frezze98 bolalo on StackOverflow for the code to create a batch System.out.println("Robopipe Start");
// file to start the program in a command window while (!EXIT) {
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");
new App().run(); new App().run();
} }
EXIT = true;
} }
public void run() { public void run() {
try { try {
NetworkTableInstance inst = NetworkTableInstance.getDefault(); NetworkTableInstance inst = NetworkTableInstance.getDefault();
NetworkTable table = inst.getTable("/SmartDashboard"); NetworkTable table = inst.getTable("/SmartDashboard");
System.out.println("Waiting for Unity client connection..."); System.out.println("Connecting to Unity local server...");
serverSocket = new ServerSocket(portNumber); //serverSocket = new ServerSocket(portNumber);
clientSocket = serverSocket.accept(); clientSocket = new Socket("127.0.0.1", portNumber);
os = clientSocket.getOutputStream(); os = clientSocket.getOutputStream();
in = clientSocket.getInputStream(); in = clientSocket.getInputStream();
System.out.println("Connected"); System.out.println("Connected");
@@ -85,7 +63,7 @@ public class App {
inst.startClientTeam(4388); inst.startClientTeam(4388);
inst.startDSClient(); inst.startDSClient();
while (true) { while (!EXIT) {
toUnity(); toUnity();
} }
} catch (IOException e) { } catch (IOException e) {
@@ -100,13 +78,21 @@ public class App {
output += "|"; output += "|";
System.out.println("'" + entry.getName() + "'"); System.out.println("'" + entry.getName() + "'");
} }
output = output.substring(0, output.length() - 1); output = output.substring(0, output.length() - 1);
byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8); byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8);
System.out.println("Sending '" + output + "' of length: " + byteOutput.length + " to Unity..."); System.out.println("Sending '" + output + "' of length: " + byteOutput.length + " to Unity...");
os.write(byteOutput); os.write(byteOutput);
System.out.println("Waiting for Response..."); System.out.println("Waiting for Response...");
in.read(); byte[] byteInput = new byte[4];
System.out.println("Response Received"); 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) { public static BigDecimal round(double d, int decimalPlace) {