-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstractBackEnd.java
69 lines (55 loc) · 1.63 KB
/
AbstractBackEnd.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
package org.genericsystem.common;
import java.lang.invoke.MethodHandles;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
/**
* @author Nicolas Feybesse
*
* @param <T>
*/
public abstract class AbstractBackEnd {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected Map<String, Root> roots = new HashMap<>();
protected AbstractWebSocketsServer webSocketsServer;
public AbstractBackEnd(String host, int port) {
webSocketsServer = buildWebSocketsServer(host, port);
}
abstract protected AbstractWebSocketsServer buildWebSocketsServer(String host, int port);
public void start() {
webSocketsServer.start();
}
public void stop() {
webSocketsServer.stop(roots);
}
public AbstractWebSocketsServer getwebSocket() {
return this.webSocketsServer;
}
public Map<String, Root> getRoots() {
return roots;
}
public static <T> T synchronizeTask(Handler<Handler<AsyncResult<T>>> consumer) {
BlockingQueue<AsyncResult<T>> blockingQueue = new ArrayBlockingQueue<>(1);
consumer.handle(res -> {
try {
blockingQueue.put(res);
} catch (InterruptedException e) {
logger.warn("Interrupted exception.", e);
}
});
AsyncResult<T> res = null;
try {
res = blockingQueue.take();
} catch (InterruptedException e) {
logger.warn("Interrupted exception.", e);
}
if (res.failed())
throw new IllegalStateException(res.cause());
return res.result();
}
}