-
Notifications
You must be signed in to change notification settings - Fork 5
/
ApplicationServer.java
351 lines (321 loc) · 15.4 KB
/
ApplicationServer.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package org.genericsystem.reactor.appserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.genericsystem.common.AbstractBackEnd;
import org.genericsystem.common.AbstractWebSocketsServer;
import org.genericsystem.common.GSBuffer;
import org.genericsystem.common.GSVertx;
import org.genericsystem.common.Root;
import org.genericsystem.kernel.Cache;
import org.genericsystem.reactor.HtmlDomNode;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.RootHtmlDomNode;
import org.genericsystem.reactor.RootTag;
import org.genericsystem.reactor.appserver.WebAppsConfig.SimpleWebAppConfig;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Context;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.Verticle;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.impl.ConcurrentHashSet;
import io.vertx.core.impl.EventLoopContext;
import io.vertx.core.json.JsonObject;
/**
* @author Nicolas Feybesse
*/
public class ApplicationServer extends AbstractBackEnd {
protected static Logger log = LoggerFactory.getLogger(ApplicationServer.class);
protected Map<String, PersistentApplication> apps = new HashMap<>();
public ApplicationServer(WebAppsConfig options) {
super(options.getHost(), options.getPort());
log.info("Load config : \n{}", options.encodePrettily());
for (String directoryPath : options.getPersistentDirectoryPaths()) {
String path = directoryPath != null ? directoryPath : "/";
Root root = buildRoot(directoryPath, options.getClasses(directoryPath), options.getEngineClass(directoryPath));
log.info("Starts {} with path: {} and persistence directory path: {}.", root.getClass().getSimpleName(), path, directoryPath);
if (directoryPath == null)
directoryPath = "/";
roots.put(path, root);
}
for (String applicationPath : options.getApplicationsPaths()) {
String directoryPath = options.getPersistentDirectoryPath(applicationPath);
String path = directoryPath != null ? directoryPath : "/";
apps.put(applicationPath, new PersistentApplication(options.getApplicationClass(applicationPath), options.getModelClass(applicationPath), roots.get(path), options.getRootId()));
}
}
public static ApplicationServer startSimpleGenericApp(String[] mainArgs, Class<? extends RootTagImpl> htmlAppClass, String homePersistentDirectoryPath) {
ApplicationServer server = new ApplicationServer(new SimpleWebAppConfig(mainArgs, htmlAppClass, homePersistentDirectoryPath));
server.start();
return server;
}
protected Root buildRoot(String persistentDirectoryPath, Set<Class<?>> userClasses, Class<? extends Root> applicationClass) {
try {
return applicationClass.getConstructor(String.class, Class[].class).newInstance(persistentDirectoryPath, userClasses.toArray(new Class[userClasses.size()]));
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
// protected PersistentApplication buildApp(Class<? extends TagTree> applicationClass, String persistentDirectoryPath, List<Class<?>> userClasses, Class<? extends Model> modelClass, Root engine, String rootId) {
// return new PersistentApplication(applicationClass, modelClass, engine, rootId);
// }
private static class DomNodeVerticle extends AbstractVerticle {
private Cache cache;
private ServerWebSocket socket;
private PersistentApplication application;
private RootTag tagTree;
private RootHtmlDomNode rootHtmlDomNode;
DomNodeVerticle(Cache cache, ServerWebSocket socket, PersistentApplication application) {
this.cache = cache;
this.socket = socket;
this.application = application;
}
@Override
public void start(Future<Void> startFuture) {
GSVertx.vertx().getVertx().executeBlocking(future -> {
try {
tagTree = application.getApplicationClass().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
try {
cache.start();
tagTree = application.getApplicationClass().getConstructor(Root.class).newInstance(application.getEngine());
cache.flush();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException | InvocationTargetException | NoSuchMethodException ex) {
throw new IllegalStateException(ex);
}
}
RootHtmlDomNode result = cache.safeSupply(() -> application.init(s -> {
try {
socket.writeFinalTextFrame(s);
} catch (IllegalStateException e) {
// Ignore (socket closed exception)
}
}, tagTree));
future.complete(result);
}, res -> {
if (res.succeeded())
rootHtmlDomNode = (RootHtmlDomNode) res.result();
else
log.error("Failed to build tag tree.", res.cause());
});
}
RootHtmlDomNode getRootHtmlDomNode() {
return rootHtmlDomNode;
}
}
private class CacheSocketContext {
private final Cache cache;
private final ServerWebSocket socket;
private final Context context;
private final DomNodeVerticle domNodeVerticle;
public CacheSocketContext(Cache cache, ServerWebSocket socket, Context context, DomNodeVerticle domNodeVerticle) {
this.cache = cache;
this.socket = socket;
this.context = context;
this.domNodeVerticle = domNodeVerticle;
}
public Cache getCache() {
return cache;
}
public ServerWebSocket getSocket() {
return socket;
}
public Context getContext() {
return context;
}
public DomNodeVerticle getDomNodeVerticle() {
return domNodeVerticle;
}
}
private class WebSocketsServer extends AbstractWebSocketsServer {
private Set<CacheSocketContext> caches = new ConcurrentHashSet<>();
public WebSocketsServer(String host, int port) {
super(host, port);
Verticle verticle = new AbstractVerticle() {
@Override
public void start() throws Exception {
GSVertx.vertx().getVertx().setPeriodic(1000, event -> caches.forEach(cacheSocketContext -> {
((EventLoopContext) cacheSocketContext.getContext()).executeAsync(v -> {
Cache cache = cacheSocketContext.getCache();
if (caches.stream().anyMatch(csc -> cache.equals(csc.getCache())))
cache.safeConsum((f) -> cache.shiftTs());
});
}));
}
};
DeploymentOptions options = new DeploymentOptions().setWorker(true).setMaxWorkerExecuteTime(Long.MAX_VALUE).setWorkerPoolName("vert.x-web-socket-worker").setWorkerPoolSize(10);
GSVertx.vertx().getVertx().deployVerticle(verticle, options, res -> {
if (res.failed())
throw new IllegalStateException(res.cause());
});
}
@Override
public Handler<Buffer> getHandler(String path, ServerWebSocket webSocket) {
PersistentApplication application = apps.get(path);
if (application == null) {
throw new IllegalStateException("Unable to load an application with path : " + path);
}
Cache cache = (Cache) application.getEngine().newCache();
DomNodeVerticle domNodeVerticle = new DomNodeVerticle(cache, webSocket, application);
GSVertx.vertx().getVertx().deployVerticle(domNodeVerticle, new DeploymentOptions().setWorker(true));
caches.add(new CacheSocketContext(cache, webSocket, GSVertx.vertx().getVertx().getOrCreateContext(), domNodeVerticle));
return buffer -> {
GSBuffer gsBuffer = new GSBuffer(buffer);
String message = gsBuffer.getString(0, gsBuffer.length());
JsonObject json = new JsonObject(message);
RootHtmlDomNode rootHtmlDomNode = domNodeVerticle.getRootHtmlDomNode();
if (rootHtmlDomNode != null) {
HtmlDomNode node = rootHtmlDomNode.getNodeById(json.getString(ReactorStatics.ID));
if (node != null) {
cache.safeConsum((x) -> node.handleMessage(json));
} else
log.info("Can't find node with id: {}.", json.getString(ReactorStatics.ID));
} else
log.info("The DOM node tree has not been fully built yet.");
};
}
@Override
public Handler<Void> getCloseHandler(ServerWebSocket socket) {
return (v) -> {
for (CacheSocketContext cacheSocketContext : caches)
if (socket.equals(cacheSocketContext.getSocket())) {
cacheSocketContext.getDomNodeVerticle().getRootHtmlDomNode().getModelContext().destroy();
caches.remove(cacheSocketContext);
}
};
}
@Override
public void addHttpHandler(HttpServer httpServer) {
httpServer.requestHandler(request -> {
String[] items = request.path().substring(1).split("/");
String appPath = items.length == 0 ? "" : items[0];
if (appPath.endsWith(".js") || appPath.endsWith(".css") || appPath.endsWith(".ico") || appPath.endsWith(".jpg") || appPath.endsWith(".png"))
appPath = "";
PersistentApplication application = apps.get("/" + appPath);
if (application == null) {
request.response().end("No application is configured with path : /" + appPath);
log.error("No application is configured with path : /{}.", appPath);
return;
}
int shift = appPath.isEmpty() ? 0 : 1;
shift += items.length > shift ? 1 : 0;
String resourceToServe = request.path().substring(appPath.length() + shift);
if ("".equals(resourceToServe)) {
String indexHtml = "<!DOCTYPE html>";
indexHtml += "<html>";
indexHtml += "<head>";
indexHtml += "<meta charset=\"UTF-8\" name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"/reactor.css\"/>";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".css\"/>";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css\"/>";
indexHtml += "<script>";
indexHtml += "var serviceLocation = \"ws://\" + document.location.host + \"" + request.path() + "\";";
indexHtml += "</script>";
indexHtml += "<script type=\"text/javascript\" src=\"/script.js\"></script>";
indexHtml += "<script type=\"text/javascript\" src=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".js\"></script>";
indexHtml += "</head>";
indexHtml += "<body onload=\"connect();\" id=\"" + application.getRootId() + "\">";
indexHtml += "</body>";
indexHtml += "</html>";
application.setIndexHtml(indexHtml);
request.response().end(indexHtml);
} else {
InputStream input = application.getApplicationClass().getResourceAsStream("/" + resourceToServe);
if (input == null) {
if (resourceToServe.endsWith(".css")) {
log.warn("Unable to find resource : /{}, get the reactor standard reactor.css instead", resourceToServe);
input = ApplicationServer.class.getResourceAsStream("/reactor.css");
} else if (resourceToServe.endsWith(".js")) {
log.warn("Unable to find resource : /{}, get the reactor standard script.js instead", resourceToServe);
input = ApplicationServer.class.getResourceAsStream("/script.js");
} else if (resourceToServe.endsWith("favicon.ico")) {
log.warn("Unable to find resource : /{}, get the reactor standard favicon.ico instead", resourceToServe);
input = ApplicationServer.class.getResourceAsStream("/favicon.ico");
} else if (resourceToServe.endsWith(".ico") || resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
log.warn("Unable to find resource : /{}, get nothing instead.", resourceToServe);
request.response().end();
return;
} else
throw new IllegalStateException("Unable to find resource : " + resourceToServe);
}
if (resourceToServe.endsWith(".ico")) {
MultiMap headers = request.response().headers();
Buffer buffer = makeBuffer(input);
headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon");
headers.add(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length()));
headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=" + 86400);
request.response().end(buffer);
} else if (resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
// TODO
MultiMap headers = request.response().headers();
Buffer buffer = makeBuffer(input);
// headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon");
headers.add(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length()));
headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=" + 86400);
request.response().end(buffer);
} else {
String result = new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.joining("\n"));
request.response().end(result);
}
}
});
}
private Buffer makeBuffer(InputStream input) {
Buffer buffer = Buffer.buffer();
int read;
byte[] data = new byte[4096];
try {
while ((read = input.read(data, 0, data.length)) != -1) {
if (read == data.length) {
buffer.appendBytes(data);
} else {
byte[] slice = new byte[read];
System.arraycopy(data, 0, slice, 0, slice.length);
buffer.appendBytes(slice);
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
return buffer;
}
@Override
public void start() {
super.start();
String logo = "\n";
logo += ("____________________________________________________________________________________________________________\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|____________| ____ _ ____ __ /______________|\n");
logo += ("|____________| / ___)___ _ _____ ___ /_)__ / ___)_ __ ___ / / ___ ____ /_______________|\n");
logo += ("|____________| / /___/ __)/ \\/ / __)/ _ )/ |/ _)/___ \\/ \\/ ) __)/___)/ __)/ ) /________________|\n");
logo += ("|____________| / /_ / __)/ / __)/ \\/ / /_ ___/ /\\ (__ / /_ / __)/ / / / /_________________|\n");
logo += ("|____________| \\____(____(_/\\_(____(_/\\_(__(____(____/ \\ (____(____(____(_/_/_/ /__________________|\n");
logo += ("|____________| /_/ /___________________|\n");
logo += ("|____________|_________________________________________________________________________/____________________|\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|___________________________________________________________________________________________________________| \n");
log.info(logo);
log.info("Generic System Server is ready!");
}
}
@Override
protected WebSocketsServer buildWebSocketsServer(String host, int port) {
return new WebSocketsServer(host, port);
}
}