-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
326 lines (300 loc) · 12 KB
/
Connection.java
File metadata and controls
326 lines (300 loc) · 12 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.github.redreaperlp.socketapi.communication;
import com.github.redreaperlp.socketapi.communication.handler.RequestHandler;
import com.github.redreaperlp.socketapi.communication.request.Request;
import com.github.redreaperlp.socketapi.communication.request.requests.RequestPing;
import com.github.redreaperlp.socketapi.communication.request.special.RequestPromising;
import com.github.redreaperlp.socketapi.communication.response.Response;
import com.github.redreaperlp.socketapi.ns.NetInstance;
import com.github.redreaperlp.socketapi.ns.server.SocketServer;
import org.json.JSONObject;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public abstract class Connection {
private Socket socket;
private NetInstance netInstance;
private BufferedReader reader;
private BufferedWriter writer;
private Thread incomingThread;
private Thread outgoingThread;
private Thread timeoutThread;
private Thread pingThread;
private final RequestManager requestManager;
private final RequestHandler requestHandler = new RequestHandler();
public final List<RequestPromising> pendingResponses = new ArrayList<>();
private final List<Request> requestQueue = new ArrayList<>();
private long pingInterval = 100;
public Connection(Socket socket, NetInstance netInstance) {
this.socket = socket;
this.netInstance = netInstance;
registerHandlers();
requestManager = new RequestManager(netInstance);
requestManager.setConnection(this);
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public BufferedReader getReader() {
return reader;
}
public BufferedWriter getWriter() {
return writer;
}
public void end(boolean endSocket) {
System.out.println(requestQueue.size() + " requests left in queue");
try {
if (!requestQueue.isEmpty() && endSocket) {
for (Request request : requestQueue) {
if (request instanceof RequestPromising promising) {
promising.failed(408);
promising.done();
}
}
}
if (pingThread != null && pingThread.isAlive()) pingThread.interrupt();
if (incomingThread != null && incomingThread.isAlive()) incomingThread.interrupt();
if (outgoingThread != null && outgoingThread.isAlive()) outgoingThread.interrupt();
if (timeoutThread != null && timeoutThread.isAlive()) timeoutThread.interrupt();
if (!socket.isClosed() && endSocket) socket.close();
if (reader != null && !reader.ready() && endSocket) reader.close();
if (writer != null && endSocket) writer.close();
if (endSocket && netInstance instanceof SocketServer server) {
server.removeConnection(this);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void end() {
end(true);
}
/**
* starts the incoming thread
*/
public void incoming() {
incomingThread = new Thread(() -> {
while (!incomingThread.isInterrupted()) {
try {
String line = reader.readLine();
if (line != null) {
if (netInstance.usesEncrytion()) {
line = netInstance.decrypt(line);
}
JSONObject jsonObject = new JSONObject(line);
resolve(jsonObject);
}
} catch (IOException e) {
connectionError();
System.out.println("Incoming thread interrupted");
return;
}
}
});
incomingThread.setName("Incoming Listener");
incomingThread.start();
}
/**
* Sends all requests in the queue to the receiver and clears the queue
* @apiNote Thread waits until the queue is not empty and continues afterwards
*/
public void outgoing() {
outgoingThread = new Thread(() -> {
while (!outgoingThread.isInterrupted()) {
while (!requestQueue.isEmpty()) {
Request request = requestQueue.remove(0);
if (request == null) continue;
JSONObject jsonObject = new JSONObject();
if (request instanceof RequestPromising promising) {
if (!promising.isResponding()) {
jsonObject.put("type", request.getName());
jsonObject.put("data", request.getData());
} else {
Response response = promising.getResponse();
response.pack();
jsonObject.put("type", "response");
jsonObject.put("data", response.getData());
}
jsonObject.put("id", promising.getId());
} else {
jsonObject.put("type", request.getName());
jsonObject.put("data", request.getData());
}
try {
if (netInstance.usesEncrytion()) {
writer.write(netInstance.encrypt(jsonObject.toString()));
} else {
writer.write(jsonObject.toString());
}
writer.newLine();
writer.flush();
} catch (IOException e) {
connectionError();
System.out.println("Outgoing thread interrupted");
return;
}
}
synchronized (requestQueue) {
try {
requestQueue.wait();
} catch (InterruptedException e) {
System.out.println("Outgoing thread interrupted");
return;
}
}
}
});
outgoingThread.setName("Outgoing Listener");
outgoingThread.start();
}
public void timeout() {
timeoutThread = new Thread(() -> {
while (!timeoutThread.isInterrupted()) {
while (!pendingResponses.isEmpty()) {
synchronized (pendingResponses) {
List<RequestPromising> toRemove = new ArrayList<>();
for (RequestPromising promising : pendingResponses) {
if (System.currentTimeMillis() - promising.getTimeSent() > getPingInterval() * 3) {
System.out.println("Request " + promising.getName() + " timed out");
promising.failed(408);
promising.done();
toRemove.add(promising);
}
}
pendingResponses.removeAll(toRemove);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Timeout thread interrupted");
return;
}
}
synchronized (pendingResponses) {
try {
pendingResponses.wait();
} catch (InterruptedException e) {
System.out.println("Timeout thread interrupted");
return;
}
}
}
});
timeoutThread.setName("Timeout Listener");
timeoutThread.start();
}
public void connectionError() {
netInstance.notifyConnectionClosed(this);
}
public void ping() {
pingThread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Ping thread interrupted");
return;
}
getRequestManager().getRequest(RequestPing.class).complete();
}
});
pingThread.setName("Ping Thread");
pingThread.start();
}
/**
* @param jsonObject The json object to resolve
*/
private void resolve(JSONObject jsonObject) {
if (jsonObject.has("type")) {
if (jsonObject.getString("type").equals("response")) {
JSONObject data = jsonObject.getJSONObject("data");
long id = data.getLong("id");
RequestPromising toRem = null;
synchronized (pendingResponses) {
for (RequestPromising promising : pendingResponses) {
if (promising.getId() == id) {
getRequestHandler().handleRequest(promising, data);
promising.setResponse(data == null ? new JSONObject() : data);
promising.validateResponse();
promising.done();
toRem = promising;
}
}
pendingResponses.remove(toRem);
}
} else {
String type = jsonObject.getString("type");
Request s = getRequestManager().getRequest(type, jsonObject.getLong("id"));
if (s instanceof RequestPromising promising) {
getRequestHandler().handleRequest(promising, jsonObject.has("data") ? jsonObject.getJSONObject("data") : new JSONObject());
promising.validateRequest();
promising.getResponse().queue();
}
}
} else {
System.out.println("Received invalid request " + jsonObject);
}
}
/**
* @return The request manager
*/
public RequestManager getRequestManager() {
return requestManager;
}
/**
* Queues a request to be sent
* @param request The request to queue
*/
public void queue(Request request) {
if (request == null) return;
synchronized (requestQueue) {
requestQueue.add(request);
requestQueue.notifyAll();
}
if (request instanceof RequestPromising promising) {
synchronized (pendingResponses) {
pendingResponses.add(promising);
pendingResponses.notifyAll();
}
}
}
/**
* Queues a request with priority, this means it will be sent before all other requests
* @param request The request to queue
*/
public void queuePriority(Request request) {
if (request instanceof RequestPromising promising) {
synchronized (pendingResponses) {
pendingResponses.notifyAll();
pendingResponses.add(promising);
}
}
synchronized (requestQueue) {
requestQueue.add(0, request);
requestQueue.notifyAll();
}
}
public Socket getSocket() {
return socket;
}
/**
* This method is called when the connection is established
* @param con The connection
* @apiNote Can be overridden to do something
*/
public void notifier(Connection con) {
}
public long getPingInterval() {
return pingInterval;
}
public void setPingInterval(long pingInterval) {
this.pingInterval = pingInterval;
}
public RequestHandler getRequestHandler() {
return requestHandler;
}
protected abstract void registerHandlers();
}