Skip to content

Commit

Permalink
Prepare real time feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed Sep 5, 2016
1 parent a238fca commit 7ce808e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 88 deletions.
Expand Up @@ -32,6 +32,8 @@ public AbstractWebSocketsServer(String host, int port) {

public abstract Handler<Buffer> getHandler(String path, ServerWebSocket socket);

public abstract Handler<Void> getCloseHandler(ServerWebSocket socket);

public abstract void addHttpHandler(HttpServer httpServer);

public void start() {
Expand All @@ -48,15 +50,15 @@ public void start() {
HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(port).setHost(host));

httpServer.websocketHandler(webSocket -> {

String path = webSocket.path();
webSocket.handler(getHandler(path, webSocket));
webSocket.exceptionHandler(e -> {
e.printStackTrace();
throw new IllegalStateException(e);
});

webSocket.closeHandler((v) -> System.out.println("close the webSocket"));
;
webSocket.closeHandler(getCloseHandler(webSocket));
});

addHttpHandler(httpServer);
Expand Down
19 changes: 14 additions & 5 deletions gs-kernel/src/main/java/org/genericsystem/kernel/EngineServer.java
@@ -1,12 +1,9 @@
package org.genericsystem.kernel;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.ServerWebSocket;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.genericsystem.common.AbstractBackEnd;
import org.genericsystem.common.AbstractCache;
Expand All @@ -16,12 +13,19 @@
import org.genericsystem.common.GSBuffer;
import org.genericsystem.common.Protocol;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.ServerWebSocket;

/**
* @author Nicolas Feybesse
*
*/
public class EngineServer extends AbstractBackEnd {

private Set<AbstractCache> caches = Collections.newSetFromMap(new ConcurrentHashMap<AbstractCache, Boolean>());

public static void main(String[] args) {
new EngineServer(new DefaultPathSingleEngineDeployment("/", null)).start();
}
Expand Down Expand Up @@ -95,6 +99,11 @@ public void addHttpHandler(HttpServer httpServer) {
// TODO Auto-generated method stub

}

@Override
public Handler<Void> getCloseHandler(ServerWebSocket socket) {
return (v) -> System.out.println("Close socket");
}
}

@Override
Expand Down
@@ -1,15 +1,5 @@
package org.genericsystem.reactor.appserver;

import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
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.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,18 +8,29 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.genericsystem.common.AbstractBackEnd;
import org.genericsystem.common.AbstractCache;
import org.genericsystem.common.AbstractWebSocketsServer;
import org.genericsystem.common.GSBuffer;
import org.genericsystem.common.Root;
import org.genericsystem.kernel.Cache;
import org.genericsystem.reactor.HtmlDomNode;
import org.genericsystem.reactor.HtmlDomNode.RootHtmlDomNode;
import org.genericsystem.reactor.appserver.WebAppsConfig.SimpleWebAppConfig;
import org.genericsystem.reactor.gs.GSApp;

import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
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.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
* @author Nicolas Feybesse
*
Expand Down Expand Up @@ -76,8 +77,17 @@ protected Root buildRoot(String persistentDirectoryPath, Set<Class<?>> userClass

private class WebSocketsServer extends AbstractWebSocketsServer {

public void cachesShiftTs() {
for (Cache cache : caches.values()) {
cache.shiftTs();
}
}

private Map<ServerWebSocket, Cache> caches = new ConcurrentHashMap<ServerWebSocket, Cache>();

public WebSocketsServer(String host, int port) {
super(host, port);
// GSVertx.vertx().getVertx().setPeriodic(5000, l -> cachesShiftTs());
}

@Override
Expand All @@ -87,8 +97,9 @@ public Handler<Buffer> getHandler(String path, ServerWebSocket socket) {
if (application == null) {
throw new IllegalStateException("Unable to load an application with path : " + path);
}
AbstractCache cache = application.getEngine().newCache();
Cache cache = (Cache) application.getEngine().newCache();
RootHtmlDomNode rootHtmlDomNode = cache.safeSupply(() -> application.init(socket));
caches.put(socket, cache);
// log.info("Open new socket : " + socket);
return buffer -> {
// log.info("Receive new message for socket : " + socket);
Expand All @@ -103,82 +114,90 @@ public Handler<Buffer> getHandler(String path, ServerWebSocket socket) {
};
}

@Override
public Handler<Void> getCloseHandler(ServerWebSocket socket) {
return (v) -> {
System.out.println("Close socket");
caches.remove(socket);
};
}

@Override
public void addHttpHandler(HttpServer httpServer) {
httpServer.requestHandler(request -> {
// log.info("Request received with path : " + request.path());
String[] items = request.path().substring(1).split("/");
// log.info("Request received with splited items : " + Arrays.toString(items));
String appPath = items.length == 0 ? "" : items[0];
if (appPath.endsWith(".js") || appPath.endsWith(".css") || appPath.endsWith(".ico") || appPath.endsWith(".jpg") || appPath.endsWith(".png"))
appPath = "";
// log.info("Request received with application path : " + appPath);
PersistentApplication application = apps.get("/" + appPath);
if (application == null) {
request.response().end("No application is configured with path : /" + appPath);
log.info("No application is configured with path : /" + appPath);
return;
String[] items = request.path().substring(1).split("/");
// log.info("Request received with splited items : " + Arrays.toString(items));
String appPath = items.length == 0 ? "" : items[0];
if (appPath.endsWith(".js") || appPath.endsWith(".css") || appPath.endsWith(".ico") || appPath.endsWith(".jpg") || appPath.endsWith(".png"))
appPath = "";
// log.info("Request received with application path : " + appPath);
PersistentApplication application = apps.get("/" + appPath);
if (application == null) {
request.response().end("No application is configured with path : /" + appPath);
log.info("No application is configured with path : /" + appPath);
return;
}
// log.info("Request detected for application : " + application.getApplicationClass().getName());
int shift = appPath.isEmpty() ? 0 : 1;
shift += items.length > shift ? 1 : 0;
String resourceToServe = request.path().substring(appPath.length() + shift);
// log.info("Resource to serve : " + resourceToServe);
if ("".equals(resourceToServe)) {
String indexHtml = "<!DOCTYPE html>";
indexHtml += "<html>";
indexHtml += "<head>";
indexHtml += "<meta charset=\"UTF-8\">";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".css\"/>";
indexHtml += "<script>";
indexHtml += "var serviceLocation = \"ws://\" + document.location.host + \"" + request.path() + "\";";
indexHtml += "</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>";
request.response().end(indexHtml);
} else {
InputStream input = application.getApplicationClass().getResourceAsStream("/" + resourceToServe);
if (input == null) {
if (resourceToServe.endsWith(".css")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard reactor.css instead");
input = ApplicationServer.class.getResourceAsStream("/reactor.css");
} else if (resourceToServe.endsWith(".js")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard script.js instead");
input = ApplicationServer.class.getResourceAsStream("/script.js");
} else if (resourceToServe.endsWith("favicon.ico")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard favicon.ico instead");
input = ApplicationServer.class.getResourceAsStream("/favicon.ico");
} else if (resourceToServe.endsWith(".ico") || resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get nothing instead");
request.response().end();
return;
} else
throw new IllegalStateException("Unable to find resource : " + resourceToServe);
}
// log.info("Request detected for application : " + application.getApplicationClass().getName());
int shift = appPath.isEmpty() ? 0 : 1;
shift += items.length > shift ? 1 : 0;
String resourceToServe = request.path().substring(appPath.length() + shift);
// log.info("Resource to serve : " + resourceToServe);
if ("".equals(resourceToServe)) {
String indexHtml = "<!DOCTYPE html>";
indexHtml += "<html>";
indexHtml += "<head>";
indexHtml += "<meta charset=\"UTF-8\">";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".css\"/>";
indexHtml += "<script>";
indexHtml += "var serviceLocation = \"ws://\" + document.location.host + \"" + request.path() + "\";";
indexHtml += "</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>";
request.response().end(indexHtml);
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 {
InputStream input = application.getApplicationClass().getResourceAsStream("/" + resourceToServe);
if (input == null) {
if (resourceToServe.endsWith(".css")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard reactor.css instead");
input = ApplicationServer.class.getResourceAsStream("/reactor.css");
} else if (resourceToServe.endsWith(".js")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard script.js instead");
input = ApplicationServer.class.getResourceAsStream("/script.js");
} else if (resourceToServe.endsWith("favicon.ico")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard favicon.ico instead");
input = ApplicationServer.class.getResourceAsStream("/favicon.ico");
} else if (resourceToServe.endsWith(".ico") || resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get nothing instead");
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);
}
String result = new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.joining("\n"));
request.response().end(result);
}
});
}
});
}

private Buffer makeBuffer(InputStream input) {
Expand Down

0 comments on commit 7ce808e

Please sign in to comment.