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/
+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;
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<NetworkTableEntry> tableEntrys = new ArrayList<NetworkTableEntry>();
@@ -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) {