Search in sources :

Example 1 with RouterStatusEvent

use of me.matoosh.undernet.event.router.RouterStatusEvent in project UnderNet by itsMatoosh.

the class Router method start.

/**
 * Starts the router.
 * Starts the server listening process and establishes client connections.
 *
 * @param networkIdentity The identity with which to join the network.
 */
public void start(NetworkIdentity networkIdentity) {
    // Checking whether the router is already running.
    if (status != InterfaceStatus.STOPPED) {
        logger.warn("Can't start, because the router is already running!");
        return;
    }
    // Caching the network identity.
    Node.self.setIdentity(networkIdentity);
    // Checking whether the setup needs to be ran.
    if (server == null || client == null) {
        setup();
    }
    // Setting the status to starting.
    EventManager.callEvent(new RouterStatusEvent(this, InterfaceStatus.STARTING));
    // Starting the client. Using a separate thread for blocking api.
    new Thread(new Runnable() {

        public void run() {
            client.start();
        }
    }).start();
    // Starting the server. Using a separate thread for blocking api.
    new Thread(new Runnable() {

        public void run() {
            server.start();
        }
    }).start();
}
Also used : RouterStatusEvent(me.matoosh.undernet.event.router.RouterStatusEvent)

Example 2 with RouterStatusEvent

use of me.matoosh.undernet.event.router.RouterStatusEvent in project UnderNet by itsMatoosh.

the class Router method onRouterStopped.

/**
 * Called when the router stops.
 */
private void onRouterStopped() {
    // Setting the status to stopped.
    if (status != InterfaceStatus.STOPPED) {
        EventManager.callEvent(new RouterStatusEvent(this, InterfaceStatus.STOPPED));
        // GC
        System.runFinalization();
        System.gc();
    }
}
Also used : RouterStatusEvent(me.matoosh.undernet.event.router.RouterStatusEvent)

Example 3 with RouterStatusEvent

use of me.matoosh.undernet.event.router.RouterStatusEvent 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)

Aggregations

RouterStatusEvent (me.matoosh.undernet.event.router.RouterStatusEvent)3 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 ServerExceptionEvent (me.matoosh.undernet.event.server.ServerExceptionEvent)1 ServerStatusEvent (me.matoosh.undernet.event.server.ServerStatusEvent)1