-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebInterface.java
127 lines (99 loc) · 3.59 KB
/
WebInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import org.simpleframework.http.core.Container;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import org.simpleframework.http.Response;
import org.simpleframework.http.Request;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.io.*;
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
import visualizer.GEdge;
public class WebInterface implements Container {
MapSearcher mapSearcher;
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("USAGE: java WebInterface <file>.co");
return;
}
Tree tree;
try {
tree = new Tree(args[0]);
}
catch (IOException e) {
System.out.println ("Couldn't load file.");
return;
}
MapSearcher mapSearcher = new MapSearcher(tree);
WebInterface webInterface = new WebInterface(mapSearcher);
}
public WebInterface(MapSearcher mapSearcher_) {
mapSearcher = mapSearcher_;
Container container = this;
Connection connection;
SocketAddress address = new InetSocketAddress(8080);
try {
connection = new SocketConnection(container);
connection.connect(address);
}
catch (IOException e) {
System.out.println ("Could not listen on port 8080!");
}
}
public void handle(Request request, Response response) {
PrintStream body;
try {
body = response.getPrintStream();
}
catch(Exception e) {
return;
}
long time = System.currentTimeMillis();
response.set("Server", "CS40 Maps");
response.setDate("Date", time);
response.setDate("Last-Modified", time);
try {
int maxy = Integer.parseInt(request.getForm().get("maxy"));
int miny = Integer.parseInt(request.getForm().get("miny"));
int maxx = Integer.parseInt(request.getForm().get("maxx"));
int minx = Integer.parseInt(request.getForm().get("minx"));
LinkedList<GEdge> edges = mapSearcher.getEdges(maxy,miny,maxx,minx);
response.set("Content-Type", "application/json");
body.println("[");
for (GEdge gEdge : edges) {
body.print("[");
body.print(gEdge.vlon);
body.print(",");
body.print(gEdge.vlat);
body.print(",");
body.print(gEdge.wlon);
body.print(",");
body.print(gEdge.wlat);
body.println("],");
}
body.println("]");
}
catch (Exception e) {
System.out.println ("Badly formed request, sending HTML.");
response.set("Content-Type", "text/html");
FileInputStream in;
try {
in = new FileInputStream("HTMLVisualizer.html");
}
catch (IOException ioe) {
body.close();
return;
}
Scanner scanner = new Scanner(in);
try {
while (scanner.hasNextLine())
body.println(scanner.nextLine());
}
finally {
scanner.close();
}
}
body.close();
}
}