Search in sources :

Example 1 with ClientStatusEvent

use of me.matoosh.undernet.event.client.ClientStatusEvent 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 ClientStatusEvent

use of me.matoosh.undernet.event.client.ClientStatusEvent in project UnderNet by itsMatoosh.

the class Client method connect.

/**
 * Connects the client to a node.
 */
public void connect(Node node) {
    if (status == InterfaceStatus.STOPPING) {
        logger.error("Can't connect to nodes, while the client is stopping!");
        return;
    }
    if (status != InterfaceStatus.STARTED) {
        EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTED));
    }
    logger.info("Connecting to node: " + node.address);
    // Making sure the list of client futures exists.
    if (closeFutures == null) {
        closeFutures = new ArrayList<>();
    }
    // Starting the client.
    Bootstrap clientBootstrap = new Bootstrap();
    // Assigning the channel to the client event loop group.
    clientBootstrap.group(workerEventLoopGroup);
    // Using the non blocking io.
    clientBootstrap.channel(NioSocketChannel.class);
    // Making sure the connection is sending the keep alive signal.
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    clientBootstrap.handler(new ClientChannelInitializer(this));
    // Connecting
    // Connecting to the node.
    ChannelFuture future = clientBootstrap.connect(node.address);
    ChannelFuture closeFuture = future.channel().closeFuture();
    closeFuture.addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            // Removing the future from future list.
            closeFutures.remove(future);
            if (closeFutures.size() == 0) {
                // Stopping the worker group.
                EventManager.callEvent(new ClientStatusEvent(Client.this, InterfaceStatus.STOPPED));
            }
        }
    });
    closeFutures.add(closeFuture);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent)

Example 3 with ClientStatusEvent

use of me.matoosh.undernet.event.client.ClientStatusEvent in project UnderNet by itsMatoosh.

the class Client method start.

/**
 * Starts the client and connects to cached nodes based on the settings.
 */
public void start() {
    EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTING));
    // Attempting to connect to each of the 5 most reliable nodes.
    ArrayList<Node> nodesToConnectTo = EntryNodeCache.getMostReliable(5, null);
    if (nodesToConnectTo == null || nodesToConnectTo.size() == 0) {
        logger.warn("There are no cached nodes to connect to! The client will stop.");
        EventManager.callEvent(new ClientExceptionEvent(this, new ClientNoNodesCachedException(this)));
        return;
    }
    // Creating a new event loop group.
    workerEventLoopGroup = new NioEventLoopGroup();
    // Creating a list of client futures.
    closeFutures = new ArrayList<>();
    EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTED));
    // Connecting to the selected nodes.
    for (Node node : nodesToConnectTo) {
        connect(node);
    }
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent) ClientExceptionEvent(me.matoosh.undernet.event.client.ClientExceptionEvent) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ClientStatusEvent (me.matoosh.undernet.event.client.ClientStatusEvent)3 ClientExceptionEvent (me.matoosh.undernet.event.client.ClientExceptionEvent)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 Future (io.netty.util.concurrent.Future)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 RouterStatusEvent (me.matoosh.undernet.event.router.RouterStatusEvent)1 ServerExceptionEvent (me.matoosh.undernet.event.server.ServerExceptionEvent)1 ServerStatusEvent (me.matoosh.undernet.event.server.ServerStatusEvent)1 Node (me.matoosh.undernet.p2p.node.Node)1