java/src/org/jruby/jubilee/Server.java in jubilee-0.2.0 vs java/src/org/jruby/jubilee/Server.java in jubilee-0.2.1

- old
+ new

@@ -5,20 +5,24 @@ import org.jruby.*; import org.jruby.runtime.*; import org.jruby.runtime.builtin.*; import org.jruby.anno.JRubyMethod; +import org.vertx.java.core.json.JsonArray; +import org.vertx.java.core.json.JsonObject; public class Server extends RubyObject { final private Vertx vertx; final private HttpServer httpServer; private RackApplication app; private boolean running; private boolean ssl = false; private String keyStorePath; private String keyStorePassword; + private String eventBusPrefix; private int port; + private String host; public static void createServerClass(Ruby runtime) { RubyModule mJubilee = runtime.defineModule("Jubilee"); RubyClass serverClass = mJubilee.defineClassUnder("VertxServer", runtime.getObject(), ALLOCATOR); serverClass.defineAnnotatedMethods(Server.class); @@ -34,36 +38,68 @@ super(ruby, rubyClass); vertx = Vertx.newVertx(); httpServer = vertx.createHttpServer(); } - @JRubyMethod(name = "initialize", required = 2, optional = 3) - public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block) { - this.port = RubyInteger.num2int(args[1]); - if (args.length > 2) { - this.ssl = args[2].isTrue(); + /** + * Initialize jubilee server, take a rack application and a configuration hash as parameter + * @param context + * @param args + * @param block + * @return + */ + @JRubyMethod(name = "initialize") + public IRubyObject initialize(ThreadContext context, IRubyObject app, IRubyObject config, Block block) { + Ruby runtime = getRuntime(); + RubyHash options = config.convertToHash(); + RubySymbol port_k = runtime.newSymbol("Port"); + RubySymbol host_k = runtime.newSymbol("Host"); + RubySymbol ssl_k = runtime.newSymbol("ssl"); + RubySymbol keystore_path_k = runtime.newSymbol("keystore_path"); + RubySymbol keystore_password_k = runtime.newSymbol("keystore_password"); + RubySymbol eventbus_prefix_k = runtime.newSymbol("eventbus_prefix"); + this.port = RubyInteger.num2int(options.op_aref(context, port_k)); + if (options.has_key_p(host_k).isTrue()) { + this.host = options.op_aref(context, host_k).toString(); + } else { + this.host = "0.0.0.0"; } - this.app = new RackApplication(args[0], this.ssl); - if (args.length > 3) - this.keyStorePath = args[3].toString(); - if (args.length > 4) - this.keyStorePassword = args[4].toString(); + this.ssl = options.op_aref(context, ssl_k).isTrue(); + this.app = new RackApplication(app, this.ssl); + if (options.has_key_p(keystore_path_k).isTrue()) { + this.keyStorePath = options.op_aref(context, keystore_path_k).toString(); + this.keyStorePassword = options.op_aref(context, keystore_password_k).toString(); + } + if (options.has_key_p(eventbus_prefix_k).isTrue()) { + this.eventBusPrefix = options.op_aref(context, eventbus_prefix_k).toString(); + } running = false; return this; } - @JRubyMethod(name = "start", optional = 1) - public IRubyObject start(final ThreadContext context, final IRubyObject[] args, final Block block) { + /** + * Start http server, initialize states + * @param context + * @param block + * @return + */ + @JRubyMethod(name = "start") + public IRubyObject start(final ThreadContext context, final Block block) { this.running = true; httpServer.setAcceptBacklog(10000); httpServer.requestHandler(new Handler<HttpServerRequest>() { public void handle(final HttpServerRequest req) { app.call(req); } }); + if (eventBusPrefix != null) { + JsonObject config = new JsonObject().putString("prefix", eventBusPrefix); + // TODO read inbounds and outbounds from config file + vertx.createSockJSServer(httpServer).bridge(config, new JsonArray(), new JsonArray()); + } if (ssl) httpServer.setSSL(true).setKeyStorePath(this.keyStorePath) .setKeyStorePassword(this.keyStorePassword); - httpServer.listen(this.port); + httpServer.listen(this.port, this.host); return this; } /** * Set timeout for keep alive connection