Search in sources :

Example 1 with ApiStartupException

use of org.cubeengine.module.apiserver.exception.ApiStartupException in project modules-extra by CubeEngine.

the class ApiServer method onEnable.

@Listener
public void onEnable(GamePostInitializationEvent event) {
    reflector.getDefaultConverterManager().registerConverter(new InetAddressConverter(), InetAddress.class);
    this.configure(reflector.load(ApiConfig.class, moduleFolder.resolve("webapi.yml").toFile()));
    try {
        this.start();
        final ObjectMapper mapper = new ObjectMapper();
        LoggerUtil.attachCallback(null, (f, a, t, msg) -> {
            ObjectNode node = mapper.createObjectNode();
            node.put("msg", msg);
            this.fireEvent("console", node);
        });
    } catch (ApiStartupException ex) {
        this.logger.error(ex, "The web API will not be available as the server failed to start properly...");
    }
    this.registerApiHandlers(ApiServer.class, new CommandController(i18n, tm, cm));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiStartupException(org.cubeengine.module.apiserver.exception.ApiStartupException) Listener(org.spongepowered.api.event.Listener)

Example 2 with ApiStartupException

use of org.cubeengine.module.apiserver.exception.ApiStartupException in project modules-extra by CubeEngine.

the class ApiServer method start.

/**
 * Starts the server
 *
 * @return fluent interface
 */
public ApiServer start() throws ApiStartupException {
    if (!this.isRunning()) {
        final ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            this.eventLoopGroup.set(new NioEventLoopGroup(this.maxThreads.get(), tf));
            serverBootstrap.group(this.eventLoopGroup.get()).channel(NioServerSocketChannel.class).childHandler(new ApiServerInitializer(cm, am, this)).localAddress(this.bindAddress.get(), this.port.get());
            this.bootstrap.set(serverBootstrap);
            this.channel.set(serverBootstrap.bind().sync().channel());
        } catch (Exception e) {
            this.bootstrap.set(null);
            this.channel.set(null);
            this.eventLoopGroup.getAndSet(null).shutdownGracefully(2, 5, TimeUnit.SECONDS);
            throw new ApiStartupException("The API server failed to start!", e);
        }
    }
    return this;
}
Also used : ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) UnknownHostException(java.net.UnknownHostException) ApiStartupException(org.cubeengine.module.apiserver.exception.ApiStartupException) ApiStartupException(org.cubeengine.module.apiserver.exception.ApiStartupException)

Aggregations

ApiStartupException (org.cubeengine.module.apiserver.exception.ApiStartupException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 UnknownHostException (java.net.UnknownHostException)1 Listener (org.spongepowered.api.event.Listener)1