package com.gofish.sentiment.storage;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.mongo.MongoClient;

import java.util.Optional;

/**
 * @author Luke Herron
 */
public class StorageVerticle extends AbstractVerticle {

    private static final Logger LOG = LoggerFactory.getLogger(StorageVerticle.class);

    private JsonObject config;
    private MongoClient mongo;

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        LOG.info("Bringing up StorageVerticle");

        this.config = Optional.ofNullable(config())
                .orElseThrow(() -> new RuntimeException("Could not load storage service configuration"));

        // We bring up the mongo client here, even though it is utilised in the worker verticles, and not here.
        // This is to enable us to keep a connection open, and simply close it when this verticle closes, rather than
        // closing it everytime the worker verticle stops (which is after every DB call).
        mongo = MongoClient.createShared(vertx, config());

        // TODO: Initialise a service proxy and publish it for service discovery
    }

    public void stop(Future<Void> stopFuture) throws Exception {
        mongo.close();
    }
}

@@ -100,16 +100,63 @@
 Future;%0A
+import io.vertx.core.eventbus.MessageConsumer;%0A
 import i
@@ -304,16 +304,206 @@
 oClient;
+%0Aimport io.vertx.servicediscovery.Record;%0Aimport io.vertx.servicediscovery.ServiceDiscovery;%0Aimport io.vertx.servicediscovery.types.EventBusService;%0Aimport io.vertx.serviceproxy.ProxyHelper;
 %0A%0Aimport
@@ -760,16 +760,147 @@
 t mongo;
+%0A    private MessageConsumer%3CJsonObject%3E messageConsumer;%0A    private ServiceDiscovery serviceDiscovery;%0A    private Record record;
 %0A%0A    @O
@@ -1577,14 +1577,8 @@
   //
- TODO:
  Ini
@@ -1646,96 +1646,1354 @@
     
-%7D%0A%0A    public void stop(Future%3CVoid%3E stopFuture) throws Exception %7B%0A        mongo.close(
+    StorageService storageService = StorageService.create(vertx, config());%0A        messageConsumer = ProxyHelper.registerService(StorageService.class, vertx, storageService, StorageService.ADDRESS);%0A        serviceDiscovery = ServiceDiscovery.create(vertx);%0A        record = EventBusService.createRecord(StorageService.NAME, StorageService.ADDRESS, StorageService.class);%0A%0A        serviceDiscovery.publish(record, resultHandler -%3E %7B%0A            if (resultHandler.succeeded()) %7B%0A                startFuture.complete();%0A            %7D%0A            else %7B%0A                startFuture.fail(resultHandler.cause());%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void stop(Future%3CVoid%3E stopFuture) throws Exception %7B%0A        Future%3CVoid%3E recordUnpublishFuture = Future.future();%0A        Future%3CVoid%3E messageConsumerUnregisterFuture = Future.future();%0A%0A        serviceDiscovery.unpublish(record.getRegistration(), recordUnpublishFuture.completer());%0A        messageConsumer.unregister(messageConsumerUnregisterFuture.completer());%0A%0A        recordUnpublishFuture.compose(v -%3E messageConsumerUnregisterFuture).setHandler(v -%3E %7B%0A            serviceDiscovery.close();%0A            mongo.close();%0A%0A            if (v.succeeded()) %7B%0A                stopFuture.complete();%0A            %7D%0A            else %7B%0A                stopFuture.failed();%0A            %7D%0A        %7D
 );%0A 

Implement a service proxy to expose mongo client commands and service discovery to publish the proxy api to the cluster.