-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conspiracy.java
236 lines (210 loc) · 6.11 KB
/
Conspiracy.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Conspiracy {
public static final int PORT = 53591, MAX_ID = 1000 * 1000;
public static final int MIN_B = 1000, MAX_B = 10000;
private static final String FILENAME = "conspiracy.html";
private final File dir;
private final ServerSocket ss;
private final Collection<Listener> listeners = new ArrayList<Listener>();
private byte[] code = null;
public Conspiracy(File dir) throws IOException {
this.dir = dir;
ss = new ServerSocket();
}
public void run() {
try {
code = load(new File(dir, FILENAME));
ss.bind(getSocketAddress());
while(true) new ClientHandler(ss.accept()).start();
} catch(IOException e) {
e.printStackTrace();
}
}
private byte[] load(File f) throws IOException {
FileInputStream in = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
try {
int offset = 0;
while(offset < b.length) {
int read = in.read(b, offset, b.length - offset);
if(read == -1) throw new EOFException();
offset += read;
}
} finally {
in.close();
}
return b;
}
private SocketAddress getSocketAddress() throws UnknownHostException {
InetAddress address = InetAddress.getByName("0.0.0.0");
return new InetSocketAddress(address, PORT);
}
public static void main(String[] args) throws IOException {
File dir;
if(args.length == 1) dir = new File(args[0]);
else dir = new File(".");
new Conspiracy(dir).run();
}
private class ClientHandler extends Thread {
final Socket s;
ClientHandler(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
Scanner in = new Scanner(s.getInputStream());
PrintStream out = new PrintStream(s.getOutputStream());
while(in.hasNextLine()) {
String request = in.nextLine();
try {
while(in.hasNextLine()) {
if(in.nextLine().isEmpty()) {
Response response = parseRequest(request);
response.send(out);
break;
}
}
} catch(IllegalArgumentException e) {
e.printStackTrace();
break;
}
}
s.close();
} catch(IOException e) {
e.printStackTrace();
}
}
Response parseRequest(String request) {
String url = request.replaceFirst("GET (.*) HTTP/1\\..", "$1");
if(url.equals(request)) throw new IllegalArgumentException();
if(url.equals("/")) return new Response(true, code, "text/html");
if(!url.startsWith("/?")) return new Response(false);
Map<String, String> m = new TreeMap<String, String>();
for(String param : url.replace("/?", "").split("&")) {
int equals = param.indexOf('=');
if(equals == -1) throw new IllegalArgumentException();
String key = param.substring(0, equals);
String value = param.substring(equals + 1);
m.put(key, value);
}
if(m.containsKey("i")) {
int id = getInt(m.get("id"), 0, MAX_ID);
float y = getFloat(m.get("i"), 0f, 1f);
int b = getInt(m.get("b"), MIN_B, MAX_B);
synchronized(listeners) {
for(Listener l : listeners) l.handleEvent(id, y, b, true);
listeners.clear();
}
return new Response(true);
} else if(m.containsKey("e")) {
int id = getInt(m.get("id"), 0, MAX_ID);
float y = getFloat(m.get("e"), 0f, 1f);
int b = getInt(m.get("b"), MIN_B, MAX_B);
synchronized(listeners) {
for(Listener l : listeners) l.handleEvent(id, y, b, false);
listeners.clear();
}
return new Response(true);
} else if(m.containsKey("p")) {
Listener l = new Listener();
synchronized(listeners) {
listeners.add(l);
}
return new Response(true, l.waitForEvent(), "text/xml");
} else {
throw new IllegalArgumentException();
}
}
int getInt(String s, int min, int max) {
if(s == null) throw new IllegalArgumentException("null");
try {
int i = Integer.parseInt(s);
if(i < min || i > max) throw new IllegalArgumentException(s);
return i;
} catch(NumberFormatException e) {
throw new IllegalArgumentException(s);
}
}
float getFloat(String s, float min, float max) {
if(s == null) throw new IllegalArgumentException("null");
try {
float f = Float.parseFloat(s);
if(f < min || f > max) throw new IllegalArgumentException(s);
return f;
} catch(NumberFormatException e) {
throw new IllegalArgumentException(s);
}
}
}
private static class Response {
final boolean ok;
final byte[] content;
final String type;
Response(boolean ok) {
this.ok = ok;
content = new byte[0];
type = "text/plain";
}
Response(boolean ok, byte[] content, String type) {
this.ok = ok;
this.content = content;
this.type = type;
}
void send(PrintStream out) throws IOException {
out.print("HTTP/1.1 " + (ok ? "200 OK" : "404 Not Found") + "\r\n");
out.print("Content-Length: " + content.length + "\r\n");
out.print("Content-Type: " + type + "\r\n");
out.print("\r\n");
out.write(content);
out.flush();
}
}
private static class Listener {
private int id = 0, b = 0;
private float y = 0f;
private boolean inhale = false, eventReceived = false;
synchronized void handleEvent(int id, float y, int b, boolean inhale) {
this.id = id;
this.y = y;
this.b = b;
this.inhale = inhale;
eventReceived = true;
notifyAll();
}
synchronized byte[] waitForEvent() {
while(!eventReceived) {
try {
wait();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
String xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<event>" + "<id>" + id + "</id>" + "<y>" + y + "</y>" +
"<b>" + b + "</b>" + "<in>" + inhale + "</in>" + "</event>";
try {
return xml.getBytes("UTF-8");
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}