aboutsummaryrefslogtreecommitdiff
path: root/central/src/main/java/moe/yuuta/dn42peering/asn/ASNHttpVerticle.java
blob: 32f1a24c975c1e34210095036127128a8ebd087c (plain)
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
package moe.yuuta.dn42peering.asn;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.serviceproxy.ServiceBinder;

public class ASNHttpVerticle extends AbstractVerticle {
    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    private MessageConsumer<JsonObject> consumer;

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        consumer = new ServiceBinder(vertx)
                .setAddress(IASNHttpService.ADDRESS)
                .register(IASNHttpService.class, new IASNHttpServiceImpl(vertx));
        consumer.completionHandler(ar -> {
            if (ar.succeeded()) {
                startPromise.complete();
            } else {
                startPromise.fail(ar.cause());
            }
        });
    }

    @Override
    public void stop(Promise<Void> stopPromise) throws Exception {
        Future.future(f -> consumer.unregister(ar -> {
            if (ar.succeeded()) f.complete();
            else f.fail(ar.cause());
        })).onComplete(ar -> {
            if (ar.succeeded()) stopPromise.complete();
            else stopPromise.fail(ar.cause());
        });
    }
}