Search in sources :

Example 1 with RealmClientHandler

use of com.codingchili.realm.controller.RealmClientHandler 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

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 WebsocketListener (com.codingchili.core.listener.transport.WebsocketListener)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 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1