Search in sources :

Example 1 with ChannelCreatedEvent

use of me.matoosh.undernet.event.channel.ChannelCreatedEvent in project UnderNet by itsMatoosh.

the class NeighborNodesManager method onEventCalled.

/**
 * Called when the handled event is called.
 *
 * @param e
 */
@Override
public void onEventCalled(Event e) {
    if (e instanceof ChannelCreatedEvent) {
        // Sending node info to the connected node.
        ChannelCreatedEvent channelCreatedEvent = (ChannelCreatedEvent) e;
        sendNodeInfo(Node.self, channelCreatedEvent.remoteNode);
    } else if (e instanceof ChannelMessageReceivedEvent) {
        ChannelMessageReceivedEvent messageReceivedEvent = (ChannelMessageReceivedEvent) e;
        if (messageReceivedEvent.message.msgId == MsgType.NODE_INFO.ordinal()) {
            NodeInfoMessage message = (NodeInfoMessage) NetworkMessage.deserializeMessage(messageReceivedEvent.message.data.array());
            // TODO: Check the generated id with the database and update.
            logger.info("Received node info for " + messageReceivedEvent.remoteNode + ": " + message.networkID);
            NetworkIdentity networkIdentity = new NetworkIdentity();
            networkIdentity.setNetworkId(message.networkID);
            messageReceivedEvent.remoteNode.setIdentity(networkIdentity);
        }
    }
}
Also used : ChannelMessageReceivedEvent(me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent) NetworkIdentity(me.matoosh.undernet.identity.NetworkIdentity) ChannelCreatedEvent(me.matoosh.undernet.event.channel.ChannelCreatedEvent) NodeInfoMessage(me.matoosh.undernet.p2p.router.data.message.NodeInfoMessage)

Example 2 with ChannelCreatedEvent

use of me.matoosh.undernet.event.channel.ChannelCreatedEvent 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 3 with ChannelCreatedEvent

use of me.matoosh.undernet.event.channel.ChannelCreatedEvent in project UnderNet by itsMatoosh.

the class ServerNetworkMessageHandler method channelActive.

/**
 * Called when the channel is ready for data transfer.
 *
 * @param ctx
 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // Adding the channel to the server list.
    server.channels.add(ctx.channel());
    // Adding a node object to the connection.
    Node clientNode = new Node();
    // Setting the node's address.
    clientNode.address = ctx.channel().remoteAddress();
    clientNode.channel = ctx.channel();
    ctx.channel().attr(ATTRIBUTE_KEY_CLIENT_NODE).set(clientNode);
    // Adding the client node to the connectedNodes list.
    server.router.connectedNodes.add(clientNode);
    // Calling the channel created event.
    EventManager.callEvent(new ChannelCreatedEvent(ctx.channel(), true));
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ChannelCreatedEvent(me.matoosh.undernet.event.channel.ChannelCreatedEvent)

Example 4 with ChannelCreatedEvent

use of me.matoosh.undernet.event.channel.ChannelCreatedEvent in project UnderNet by itsMatoosh.

the class ClientNetworkMessageHandler method channelActive.

/**
 * Calls {@link ChannelHandlerContext#fireChannelActive()} to forward
 * to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
 * <p>
 * Sub-classes may override this method to change behavior.
 *
 * @param ctx
 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // Adding the channel to the client list.
    client.channels.add(ctx.channel());
    // Adding a node object to the connection.
    Node serverNode = new Node();
    // Setting the node's address.
    serverNode.address = ctx.channel().remoteAddress();
    serverNode.channel = ctx.channel();
    ctx.channel().attr(ATTRIBUTE_KEY_SERVER_NODE).set(serverNode);
    // Adding the server node to the connected nodes list.
    client.router.connectedNodes.add(serverNode);
    // Calling the channel created event.
    EventManager.callEvent(new ChannelCreatedEvent(ctx.channel(), false));
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ChannelCreatedEvent(me.matoosh.undernet.event.channel.ChannelCreatedEvent)

Aggregations

ChannelCreatedEvent (me.matoosh.undernet.event.channel.ChannelCreatedEvent)4 Node (me.matoosh.undernet.p2p.node.Node)2 ChannelClosedEvent (me.matoosh.undernet.event.channel.ChannelClosedEvent)1 ChannelErrorEvent (me.matoosh.undernet.event.channel.ChannelErrorEvent)1 ChannelMessageReceivedEvent (me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent)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 ServerStatusEvent (me.matoosh.undernet.event.server.ServerStatusEvent)1 NetworkIdentity (me.matoosh.undernet.identity.NetworkIdentity)1 NodeInfoMessage (me.matoosh.undernet.p2p.router.data.message.NodeInfoMessage)1