Search in sources :

Example 1 with ServerStatusEvent

use of me.matoosh.undernet.event.server.ServerStatusEvent in project UnderNet by itsMatoosh.

the class Router method onEventCalled.

// EVENTS
/**
 * Called when the handled event is called.
 *
 * @param e
 */
@Override
public void onEventCalled(Event e) {
    // Connection established.
    if (e.getClass() == ChannelCreatedEvent.class) {
        ChannelCreatedEvent establishedEvent = (ChannelCreatedEvent) e;
        logger.debug("New connection established with: " + establishedEvent.other);
    } else // Connection dropped.
    if (e.getClass() == ChannelClosedEvent.class) {
        ChannelClosedEvent droppedEvent = (ChannelClosedEvent) e;
    } else // Connection error.
    if (e.getClass() == ChannelErrorEvent.class) {
        ChannelErrorEvent errorEvent = (ChannelErrorEvent) e;
    // TODO: Handle the error.
    } else if (e.getClass() == RouterStatusEvent.class) {
        RouterStatusEvent statusEvent = (RouterStatusEvent) e;
        switch(statusEvent.newStatus) {
            case STOPPED:
                onConnectionEnded();
                break;
            case STARTING:
                break;
            case STARTED:
                break;
            case STOPPING:
                break;
        }
    // TODO: Handle the status change.
    } else if (e.getClass() == RouterErrorEvent.class) {
        onRouterError((RouterErrorEvent) e);
    } else if (e.getClass() == ServerStatusEvent.class) {
        ServerStatusEvent statusEvent = (ServerStatusEvent) e;
        if (statusEvent.newStatus.equals(InterfaceStatus.STARTED)) {
            // In this case client doesn't yet have to be started.
            if (client.status.equals(InterfaceStatus.STARTED)) {
                // Both parts of the router started successfully.
                onRouterStarted();
            }
        } else if (statusEvent.newStatus.equals(InterfaceStatus.STOPPED)) {
            if (client.status.equals(InterfaceStatus.STOPPED)) {
                // Both parts of the router stopped successfully.
                onRouterStopped();
            }
        }
    } else if (e.getClass() == ClientStatusEvent.class) {
        ClientStatusEvent statusEvent = (ClientStatusEvent) e;
        if (statusEvent.newStatus.equals(InterfaceStatus.STARTED)) {
            if (server.status.equals(InterfaceStatus.STARTED)) {
                // Both parts of the router started succesfully.
                onRouterStarted();
            }
        } else if (statusEvent.newStatus.equals(InterfaceStatus.STOPPED)) {
            if (server.status.equals(InterfaceStatus.STOPPED)) {
                // Both parts of the router stopped successfully.
                onRouterStopped();
            }
        }
    } else if (e.getClass() == ClientExceptionEvent.class) {
        ClientExceptionEvent exceptionEvent = (ClientExceptionEvent) e;
        logger.error("Exception occurred with the client, shutting down the router!", exceptionEvent.exception);
        this.stop();
    } else if (e.getClass() == ServerExceptionEvent.class) {
        ServerExceptionEvent exceptionEvent = (ServerExceptionEvent) e;
        logger.error("Exception occurred with the server, shutting down the router!", exceptionEvent.exception);
        this.stop();
    }
}
Also used : ChannelErrorEvent(me.matoosh.undernet.event.channel.ChannelErrorEvent) RouterStatusEvent(me.matoosh.undernet.event.router.RouterStatusEvent) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent) ChannelCreatedEvent(me.matoosh.undernet.event.channel.ChannelCreatedEvent) ClientExceptionEvent(me.matoosh.undernet.event.client.ClientExceptionEvent) ChannelClosedEvent(me.matoosh.undernet.event.channel.ChannelClosedEvent) ServerStatusEvent(me.matoosh.undernet.event.server.ServerStatusEvent) ServerExceptionEvent(me.matoosh.undernet.event.server.ServerExceptionEvent)

Example 2 with ServerStatusEvent

use of me.matoosh.undernet.event.server.ServerStatusEvent in project UnderNet by itsMatoosh.

the class Server method start.

/**
 * Starts the server.
 * @throws Exception
 */
public void start() {
    // Changing the server status to starting.
    shouldStop = false;
    EventManager.callEvent(new ServerStatusEvent(Server.this, InterfaceStatus.STARTING));
    // Creating the worker and boss server event groups.
    bossEventLoopGroup = new NioEventLoopGroup();
    workerEventLoopGroup = new NioEventLoopGroup();
    // Bootstraping the server.
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    // Assigning event loops to the server.
    serverBootstrap.group(bossEventLoopGroup, workerEventLoopGroup).channel(// Using the non blocking io for transfer.
    NioServerSocketChannel.class).childHandler(new ServerChannelInitializer(this)).option(ChannelOption.SO_BACKLOG, // Setting the number of pending connections to keep in the queue.
    UnderNet.networkConfig.backlogCapacity()).childOption(ChannelOption.SO_KEEPALIVE, // Making sure the connection event loop sends keep alive messages.
    true);
    // Binding and starting to accept incoming connections.
    try {
        serverFuture = serverBootstrap.bind(UnderNet.networkConfig.listeningPort()).sync();
        EventManager.callEvent(new ServerStatusEvent(Server.this, InterfaceStatus.STARTED));
        // Waiting for the server to close.
        if (shouldStop) {
            serverFuture.channel().close();
        }
        serverFuture.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        logger.error("Error binding the server!", e);
        // Changing the server status to stopping.
        EventManager.callEvent(new ServerStatusEvent(Server.this, InterfaceStatus.STOPPING));
    } finally {
        // Stopping the event loop groups.
        try {
            serverFuture = null;
            bossEventLoopGroup.shutdownGracefully().sync();
            bossEventLoopGroup = null;
            workerEventLoopGroup.shutdownGracefully().sync();
            workerEventLoopGroup = null;
        } catch (InterruptedException e) {
            logger.error("Server shutdown has been interrupted!", e);
        }
        EventManager.callEvent(new ServerStatusEvent(Server.this, InterfaceStatus.STOPPED));
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerStatusEvent(me.matoosh.undernet.event.server.ServerStatusEvent)

Example 3 with ServerStatusEvent

use of me.matoosh.undernet.event.server.ServerStatusEvent in project UnderNet by itsMatoosh.

the class Server method stop.

/**
 * Stops the server.
 */
public void stop() {
    // Changing the server status to stopping.
    EventManager.callEvent(new ServerStatusEvent(Server.this, InterfaceStatus.STOPPING));
    shouldStop = true;
    // Stopping the server.
    if (serverFuture != null) {
        // Closing the current channel
        serverFuture.channel().close();
        // Closing the parent channel (the one attached to the bind)
        if (serverFuture.channel().parent() != null) {
            serverFuture.channel().parent().close();
        }
    }
}
Also used : ServerStatusEvent(me.matoosh.undernet.event.server.ServerStatusEvent)

Aggregations

ServerStatusEvent (me.matoosh.undernet.event.server.ServerStatusEvent)3 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 ChannelClosedEvent (me.matoosh.undernet.event.channel.ChannelClosedEvent)1 ChannelCreatedEvent (me.matoosh.undernet.event.channel.ChannelCreatedEvent)1 ChannelErrorEvent (me.matoosh.undernet.event.channel.ChannelErrorEvent)1 ClientExceptionEvent (me.matoosh.undernet.event.client.ClientExceptionEvent)1 ClientStatusEvent (me.matoosh.undernet.event.client.ClientStatusEvent)1 RouterStatusEvent (me.matoosh.undernet.event.router.RouterStatusEvent)1 ServerExceptionEvent (me.matoosh.undernet.event.server.ServerExceptionEvent)1