Search in sources :

Example 1 with WebsocketListener

use of com.codingchili.core.listener.transport.WebsocketListener in project chili-core by codingchili.

the class Service method start.

@Override
public void start(Future<Void> start) {
    List<Future> deployments = new ArrayList<>();
    for (ListenerSettings listener : context.transports()) {
        handler = new RouterHandler(context);
        Future<String> future = Future.future();
        deployments.add(future);
        switch(listener.getType()) {
            case UDP:
                start(UdpListener::new, listener.getType(), future);
                break;
            case TCP:
                start(TcpListener::new, listener.getType(), future);
                break;
            case WEBSOCKET:
                start(WebsocketListener::new, listener.getType(), future);
                break;
            case REST:
                start(RestListener::new, listener.getType(), future);
                break;
        }
    }
    Logger logger = context.logger(getClass());
    all(deployments).setHandler(done -> {
        // logger.onServiceStarted(this);
        start.complete();
    });
}
Also used : RestListener(com.codingchili.core.listener.transport.RestListener) UdpListener(com.codingchili.core.listener.transport.UdpListener) RouterHandler(com.codingchili.router.controller.RouterHandler) ArrayList(java.util.ArrayList) WebsocketListener(com.codingchili.core.listener.transport.WebsocketListener) Future(io.vertx.core.Future) TcpListener(com.codingchili.core.listener.transport.TcpListener) Logger(com.codingchili.core.logging.Logger) ListenerSettings(com.codingchili.core.listener.ListenerSettings)

Example 2 with WebsocketListener

use of com.codingchili.core.listener.transport.WebsocketListener in project chili-core by codingchili.

the class Service method deploy.

/**
 * Dynamically deploy a new realm, verifies that no existing nodes are already listening
 * on the same address by sending a ping.
 *
 * @param realm the realm to be deployed dynamically.
 */
private void deploy(Future<Void> future, RealmSettings realm) {
    Consumer<RealmContext> deployer = (rc) -> {
        // Check if the routing id for the realm is unique
        context.bus().send(realm.getName(), getPing(), getDeliveryOptions(), response -> {
            if (response.failed()) {
                // If no response then the id is not already in use.
                ListenerSettings settings = rc.getListenerSettings();
                CoreListener listener = new WebsocketListener().settings(() -> settings).handler(new RealmClientHandler(rc));
                // deploy handler for incoming messages from instances.
                rc.handler(() -> new RealmInstanceHandler(rc)).setHandler(instances -> {
                    if (instances.succeeded()) {
                        // deploy handler for incoming messages from clients.
                        rc.listener(() -> listener).setHandler(deploy -> {
                            if (deploy.failed()) {
                                rc.onDeployRealmFailure(realm.getName());
                                throw new RuntimeException(deploy.cause());
                            }
                        }).setHandler(clients -> {
                            if (clients.succeeded()) {
                                future.complete();
                            } else {
                                future.fail(clients.cause());
                            }
                        });
                    } else {
                        future.fail(instances.cause());
                    }
                });
            } else {
                future.fail(new RealmNotUniqueException());
            }
        });
    };
    // set up the realm context asynchronously.
    RealmContext.create(context, realm).setHandler(create -> {
        if (create.succeeded()) {
            deployer.accept(create.result());
        } else {
            future.fail(new RuntimeException(create.cause()));
        }
    });
}
Also used : RealmClientHandler(com.codingchili.realm.controller.RealmClientHandler) RealmInstanceHandler(com.codingchili.realm.controller.RealmInstanceHandler) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) WebsocketListener(com.codingchili.core.listener.transport.WebsocketListener) Future(io.vertx.core.Future) PATH_REALMSERVER(com.codingchili.realm.configuration.RealmServerSettings.PATH_REALMSERVER) ArrayList(java.util.ArrayList) FutureHelper.untyped(com.codingchili.core.context.FutureHelper.untyped) Consumer(java.util.function.Consumer) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) Configurations(com.codingchili.core.files.Configurations) RealmNotUniqueException(com.codingchili.realm.model.RealmNotUniqueException) com.codingchili.realm.configuration(com.codingchili.realm.configuration) JsonObject(io.vertx.core.json.JsonObject) com.codingchili.core.listener(com.codingchili.core.listener) Strings(com.codingchili.common.Strings) Configurations.system(com.codingchili.core.files.Configurations.system) CoreContext(com.codingchili.core.context.CoreContext) RealmNotUniqueException(com.codingchili.realm.model.RealmNotUniqueException) WebsocketListener(com.codingchili.core.listener.transport.WebsocketListener) RealmClientHandler(com.codingchili.realm.controller.RealmClientHandler) RealmInstanceHandler(com.codingchili.realm.controller.RealmInstanceHandler)

Aggregations

WebsocketListener (com.codingchili.core.listener.transport.WebsocketListener)2 Future (io.vertx.core.Future)2 ArrayList (java.util.ArrayList)2 Strings (com.codingchili.common.Strings)1 CoreContext (com.codingchili.core.context.CoreContext)1 FutureHelper.untyped (com.codingchili.core.context.FutureHelper.untyped)1 Configurations (com.codingchili.core.files.Configurations)1 Configurations.system (com.codingchili.core.files.Configurations.system)1 com.codingchili.core.listener (com.codingchili.core.listener)1 ListenerSettings (com.codingchili.core.listener.ListenerSettings)1 RestListener (com.codingchili.core.listener.transport.RestListener)1 TcpListener (com.codingchili.core.listener.transport.TcpListener)1 UdpListener (com.codingchili.core.listener.transport.UdpListener)1 Logger (com.codingchili.core.logging.Logger)1 com.codingchili.realm.configuration (com.codingchili.realm.configuration)1 PATH_REALMSERVER (com.codingchili.realm.configuration.RealmServerSettings.PATH_REALMSERVER)1 RealmClientHandler (com.codingchili.realm.controller.RealmClientHandler)1 RealmInstanceHandler (com.codingchili.realm.controller.RealmInstanceHandler)1 RealmNotUniqueException (com.codingchili.realm.model.RealmNotUniqueException)1 RouterHandler (com.codingchili.router.controller.RouterHandler)1