Created
January 7, 2018 07:50
-
-
Save iamWing/45ce5432acacf75fbb3b98c0b4c8e0fe to your computer and use it in GitHub Desktop.
Command line application to test the Universal Controller server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Scanner; | |
public class UCJavaTester { | |
/** | |
* Commands examples | |
* | |
* Register player | |
* REGISTER:TestPlayer1 | |
* | |
* Deregister player (0 is the player ID) | |
* DEREGISTER:0 | |
* | |
* Press key | |
* 0:KEY_DOWN:a | |
* 0:KEY_DOWN:a:extra_content | |
* | |
* Joystick (x, y) | |
* 0:JOYSTICK:0.5:-0.3 | |
* | |
* Gyro (x, y, z) | |
* 0:GYRO:0.3:-0.2:0.4 | |
*/ | |
public static void main(String[] args) throws IOException { | |
String ip; | |
int port = 28910; | |
int bufferSize = 1024; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Enter IP address"); | |
ip = scanner.nextLine(); | |
// System.out.println("Enter port number"); | |
// port = scanner.nextInt(); | |
// System.out.println("Enter buffer size"); | |
// bufferSize = scanner.nextInt(); | |
System.out.println("---COMMAND PANEL---"); | |
Socket socket = new Socket(ip, port); | |
InputStream in = socket.getInputStream(); | |
OutputStream out = socket.getOutputStream(); | |
new Thread(() -> { | |
while (true) { | |
StringBuilder msg = new StringBuilder(); | |
byte[] buffer = new byte[bufferSize]; | |
while (msg.lastIndexOf("<EOC>") == -1) { | |
try { | |
int bytesRead = in.read(buffer); | |
if (bytesRead > -1) | |
msg.append((new String(buffer, 0, bytesRead))); | |
} catch (IOException ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
System.out.println("SERVER: {" + msg + "}"); | |
} | |
}).start(); | |
// for (int i = 0; i < 10000; i++) { | |
// String cmd = "TEST_CMD:" + i; | |
// | |
// if (cmd.equals("exit")) | |
// break; | |
// | |
// byte[] toSend = (cmd + "<EOC>").getBytes(StandardCharsets.US_ASCII); | |
// out.write(toSend); | |
// out.flush(); | |
// | |
// Thread.sleep(50); | |
// } | |
while (true) { | |
String cmd = scanner.nextLine(); | |
if (cmd.equals("exit")) | |
break; | |
byte[] toSend = (cmd + "<EOC>").getBytes(StandardCharsets.US_ASCII); | |
out.write(toSend); | |
out.flush(); | |
} | |
in.close(); | |
out.close(); | |
socket.close(); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment