mirror of
https://github.com/Team4388/2022NoWayHome.git
synced 2026-06-09 08:48:07 -06:00
Add read capability
This commit is contained in:
@@ -22,6 +22,8 @@ public class DesmosServer extends Thread {
|
|||||||
|
|
||||||
private static boolean running = false;
|
private static boolean running = false;
|
||||||
|
|
||||||
|
public static boolean debug = false;
|
||||||
|
|
||||||
private int activePort;
|
private int activePort;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,8 +60,12 @@ public class DesmosServer extends Thread {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
* */
|
* */
|
||||||
public void runServer(int port) throws IOException {
|
public void runServer(int port) throws IOException {
|
||||||
|
System.out.println("Initializing DesmosServer on port " + port + "...");
|
||||||
|
|
||||||
ServerSocket serverSocket = new ServerSocket(port);
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
|
System.out.println("DesmosServer is active!");
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
Socket client = serverSocket.accept();
|
Socket client = serverSocket.accept();
|
||||||
@@ -74,21 +80,39 @@ public class DesmosServer extends Thread {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
* */
|
* */
|
||||||
public void handleClient(Socket client) throws IOException {
|
public void handleClient(Socket client) throws IOException {
|
||||||
InputStreamReader clientStream = new InputStreamReader(client.getInputStream());
|
InputStreamReader clientStream = new InputStreamReader(client.getInputStream());
|
||||||
BufferedReader bufferedReader = new BufferedReader(clientStream);
|
BufferedReader bufferedReader = new BufferedReader(clientStream);
|
||||||
|
|
||||||
ArrayList<String> requestLines = new ArrayList<>();
|
ArrayList<String> headers = new ArrayList<>();
|
||||||
|
|
||||||
while(bufferedReader.ready()) {
|
if(debug) {
|
||||||
String read = bufferedReader.readLine();
|
System.out.println("debug");
|
||||||
requestLines.add(read + "\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String header;
|
||||||
|
while((header = bufferedReader.readLine()).length() != 0) {
|
||||||
|
headers.add(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
String body = "";
|
||||||
|
while(bufferedReader.ready()) {
|
||||||
|
body += (char) bufferedReader.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
readVariables(body);
|
||||||
|
|
||||||
sendResponse(client);
|
sendResponse(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends JSON response
|
* Sends JSON response with format
|
||||||
|
* <p>
|
||||||
|
* [
|
||||||
|
* {"int": "24"},
|
||||||
|
* {"double": "2.4"},
|
||||||
|
* {"point": "(2,4)"},
|
||||||
|
* {"list": "[2,4]"}
|
||||||
|
* ]
|
||||||
*
|
*
|
||||||
* @param The client connection
|
* @param The client connection
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@@ -96,6 +120,13 @@ public class DesmosServer extends Thread {
|
|||||||
public void sendResponse(Socket client) throws IOException {
|
public void sendResponse(Socket client) throws IOException {
|
||||||
OutputStream clientOutput = client.getOutputStream();
|
OutputStream clientOutput = client.getOutputStream();
|
||||||
|
|
||||||
|
// Write Headers
|
||||||
|
clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
|
||||||
|
clientOutput.write("Access-Control-Allow-Origin: *\r\n".getBytes());
|
||||||
|
clientOutput.write("Keep-Alive: timeout=2, max=100\r\n".getBytes());
|
||||||
|
clientOutput.write("Connection: Keep-Alive\r\n".getBytes());
|
||||||
|
clientOutput.write("Content-Type: application/json\r\n\r\n".getBytes());
|
||||||
|
|
||||||
clientOutput.write(getJSONOutput().getBytes());
|
clientOutput.write(getJSONOutput().getBytes());
|
||||||
clientOutput.flush();
|
clientOutput.flush();
|
||||||
clientOutput.close();
|
clientOutput.close();
|
||||||
@@ -111,7 +142,7 @@ public class DesmosServer extends Thread {
|
|||||||
|
|
||||||
if(!desmosVariables.isEmpty()) {
|
if(!desmosVariables.isEmpty()) {
|
||||||
for(String key : desmosVariables.keySet()) {
|
for(String key : desmosVariables.keySet()) {
|
||||||
json += "{"
|
json += "\n\t{"
|
||||||
+ "\"name\":\"" + key + "\","
|
+ "\"name\":\"" + key + "\","
|
||||||
+ "\"value\":\"" + desmosVariables.get(key) + "\""
|
+ "\"value\":\"" + desmosVariables.get(key) + "\""
|
||||||
+ "},";
|
+ "},";
|
||||||
@@ -120,10 +151,20 @@ public class DesmosServer extends Thread {
|
|||||||
json = json.substring(0, json.length()-1); // remove comma at the end
|
json = json.substring(0, json.length()-1); // remove comma at the end
|
||||||
}
|
}
|
||||||
|
|
||||||
json += "]";
|
json += "\n]";
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void readVariables(String requestBody) {
|
||||||
|
for(String variable : requestBody.split("\n")) {
|
||||||
|
if(variable.equals(""))
|
||||||
|
break;
|
||||||
|
|
||||||
|
String[] readVar = variable.split("\t");
|
||||||
|
readVariables.put(readVar[0], readVar[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isRunning() {
|
public static boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
@@ -143,14 +184,10 @@ public class DesmosServer extends Thread {
|
|||||||
desmosVariables.put(name, "(" + point.x + "," + point.y + ")");
|
desmosVariables.put(name, "(" + point.x + "," + point.y + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putIntegerArray(String name, int... arr) {
|
public static void putArray(String name, double... arr) {
|
||||||
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putDoubleArray(String name, double... arr) {
|
|
||||||
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
public static int readInteger(String name) {
|
public static int readInteger(String name) {
|
||||||
@@ -170,4 +207,14 @@ public class DesmosServer extends Thread {
|
|||||||
|
|
||||||
return point;
|
return point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double[] readArray(String name) {
|
||||||
|
String[] unparsed = readVariables.get(name).split(",");
|
||||||
|
double[] arr = new double[unparsed.length];
|
||||||
|
|
||||||
|
for(int i = 0; i < arr.length; i++)
|
||||||
|
arr[i] = Integer.parseInt(unparsed[i]);
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user