aboutsummaryrefslogtreecommitdiff
path: root/central/src/main/java/moe/yuuta/dn42peering/asn/ASNVerticle.java
blob: aef9e1c3ff5867050b6c2e10a228f6e23e3e1fcb (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package moe.yuuta.dn42peering.asn;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.CompositeFuture;
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.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.serviceproxy.ServiceBinder;
import io.vertx.sqlclient.PoolOptions;

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

    private MessageConsumer<JsonObject> consumer;
    private MySQLPool pool;

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        final JsonObject json = vertx.getOrCreateContext().config().getJsonObject("database");
        final MySQLConnectOptions opt = new MySQLConnectOptions(json);
        pool = MySQLPool.pool(vertx, opt, new PoolOptions().setMaxSize(5));

        consumer = new ServiceBinder(vertx)
                .setAddress(IASNService.ADDRESS)
                .setIncludeDebugInfo(true)
                .register(IASNService.class, new ASNServiceImpl(vertx, pool));
        consumer.completionHandler(ar -> {
            if(ar.succeeded()) {
                startPromise.complete();
            } else {
                startPromise.fail(ar.cause());
            }
        });
    }

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