Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
Fixed a bug where suggested speed would not go to 0 when authority was
0.
  • Loading branch information
just10ying committed Apr 16, 2015
1 parent df67379 commit 1fd0ac4
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 81 deletions.
5 changes: 3 additions & 2 deletions CTCServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void handle(HttpExchange exchange) throws IOException {
message = message.replaceAll("/rbracket/", "]");
MessageLibrary.httpAcknowledge(exchange);
System.out.println("Sending to Track Controller: " + message);
MessageLibrary.sendMessage("10.0.0.19", 8003, message); // UNCOMMENT THIS
MessageLibrary.sendMessage("localhost", 8003, message); // UNCOMMENT THIS
break;
case "send_all":
MessageLibrary.httpAcknowledge(exchange);
Expand All @@ -96,7 +96,7 @@ public void handle(HttpExchange exchange) throws IOException {
timeMessage = timeMessage.replaceAll("/lbracket/", "[");
timeMessage = timeMessage.replaceAll("/rbracket/", "]");
System.out.println("Sending to All: " + timeMessage);
MessageLibrary.sendMessage("10.0.0.19", 8003, timeMessage); // UNCOMMENT THIS
MessageLibrary.sendMessage("localhost", 8003, timeMessage); // UNCOMMENT THIS
// MessageLibrary.sendMessage(8005, timeMessage); // UNCOMMENT THIS
// MessageLibrary.sendMessage(8007, timeMessage); // UNCOMMENT THIS
// MessageLibrary.sendMessage(8009, timeMessage); // UNCOMMENT THIS
Expand Down Expand Up @@ -128,6 +128,7 @@ public void run() {
while ((inputLine = in.readLine()) != null) {
completeInput.append(inputLine);
}
System.out.println("Receiving from " + completeInput.toString());
// Message input complete. Append the message to the list.
String JSONMessage = messageToJSON(completeInput.toString());
if (!JSONMessage.equals("Protocol error")) {
Expand Down
8 changes: 8 additions & 0 deletions ExampleMessages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Example Messages:

Track Controller : Train 0 : set, Location = 7
Track Controller : Red 27 : set, Switch = [28, 76]
Track Controller : Red 47 : set, Crossing = Closed
Track Controller : Green 3 : set, Status = Italian_flag
Track Controller : Green 4 : set, Heater = On
Track Controller : Green 5 : set, Lights = Green
3 changes: 3 additions & 0 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ function checkServerUpdates() {
}
// Decrease authority.
train.Authority(train.Authority() - 1);
if (train.Authority() == 0) {
train.Suggested_Speed(0);
}
}
}
break;
Expand Down
1 change: 0 additions & 1 deletion static/json/ExampleSchedule.json

This file was deleted.

156 changes: 78 additions & 78 deletions static/json/TrackInfo.json

Large diffs are not rendered by default.

Binary file added util/MessageLibrary$MessageQueue.class
Binary file not shown.
Binary file added util/MessageLibrary$MessageSender.class
Binary file not shown.
Binary file added util/MessageLibrary.class
Binary file not shown.
123 changes: 123 additions & 0 deletions util/MessageLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import java.io.*;
import java.util.*;
import java.net.*;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class MessageLibrary {

public class MessageQueue {



}

// Sends a message to another module with the specified hostname (localhost) and port.
public static void sendMessage(String hostName, int portNumber, String message) {
new MessageSender(hostName, portNumber, message).start();
}

// Assumes host is localhost
public static void sendMessage(int portNumber, String message) {
new MessageSender("localhost", portNumber, message).start();
}

static class MessageSender extends Thread {

String hostName;
int portNumber;
String message;

public MessageSender(String hostName, int portNumber, String message) {
this.hostName = hostName;
this.portNumber = portNumber;
this.message = message;
}

public void run() {
Socket hostSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String line = null;

// Open connection and wait for host
do {
try {
hostSocket = new Socket(hostName, portNumber);
out = new PrintWriter(hostSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(hostSocket.getInputStream()));
line = in.readLine();
}
catch (Exception e) {}
} while (line == null);

// Send message
out.println(this.message);

// Close resources
try {
hostSocket.close();
out.close();
in.close();
}
catch (Exception e) {}
}
}

// Read the contents of a file into a string.
public static byte[] readFile(String fileName) throws IOException {
File file = new File(fileName);
byte fileContent[] = new byte[(int) file.length()];
FileInputStream input = new FileInputStream(file);
input.read(fileContent);
return fileContent;
}


// Sends an HTTP response given a string of bytes
public static void sendHttpResponse(HttpExchange exchange, byte response[]) throws IOException {
exchange.sendResponseHeaders(200, response.length);
OutputStream os = exchange.getResponseBody();
os.write(response);
os.close();
}

// Reconstructs a hash table given an input string.
public static Hashtable<String, String> stringToHashtable(String input) {
Hashtable<String, String> result = new Hashtable<String, String>();
String tuples[] = input.replace(", ", ",").split(",");
for (int index = 0; index < tuples.length; index++) {
String pair[] = tuples[index].split("=");
result.put(pair[0], pair[1]);
}
return result;
}

// Sends an HTTP response given a string
public static void sendHttpResponse(HttpExchange exchange, String response) throws IOException {
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}

public static void httpAcknowledge(HttpExchange exchange) throws IOException {
sendHttpResponse(exchange, "OK");
}

public static Hashtable<String, String> getHttpGetParams(HttpExchange exchange) {
Hashtable<String, String> paramTable = new Hashtable<String, String>();
String paramString = exchange.getRequestURI().getQuery();
if (paramString != null) {
String[] paramList = paramString.split("&");
for (String s : paramList) {
String[] param = s.split("=");
paramTable.put(param[0], param[1]);
}
}
return paramTable;
}

}
Binary file added util/MessageParser.class
Binary file not shown.
Binary file added util/MessageTester.class
Binary file not shown.
Binary file added util/Tester.class
Binary file not shown.
Binary file added util/TrackModelDriver 0.1.zip
Binary file not shown.

0 comments on commit 1fd0ac4

Please sign in to comment.