Search in sources :

Example 1 with ChannelMessageReceivedEvent

use of me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent 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 ChannelMessageReceivedEvent

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

the class FileTransferManager method onEventCalled.

/**
 * Called when the handled event is called.
 *
 * @param e
 */
@Override
public void onEventCalled(Event e) {
    if (e instanceof ChannelMessageReceivedEvent) {
        ChannelMessageReceivedEvent messageReceivedEvent = (ChannelMessageReceivedEvent) e;
        if (messageReceivedEvent.message.msgId == MsgType.FILE_REQ.ordinal()) {
            // File request received.
            // Deserializing msg.
            FileTransferRequestMessage requestMsg = (FileTransferRequestMessage) NetworkMessage.deserializeMessage(messageReceivedEvent.message.data.array());
            logger.info("Received a file transfer request for: " + requestMsg.transferId);
            // A file was requested from this node. Checking if the requested transfer has been prepared.
            for (final FileTransfer transfer : outboundTransfers) {
                if (NetworkID.compare(transfer.id.data, requestMsg.transferId.data) == 0) {
                    // Checking if the recipient is the same.
                    if (transfer.recipient == messageReceivedEvent.remoteNode) {
                        executor.submit(new Runnable() {

                            @Override
                            public void run() {
                                transfer.startSending();
                            }
                        });
                    }
                }
            }
        } else if (messageReceivedEvent.message.msgId == MsgType.FILE_CHUNK.ordinal()) {
            // File chunk received.
            // Deserializing msg.
            FileChunk fileChunk = (FileChunk) NetworkMessage.deserializeMessage(messageReceivedEvent.message.data.array());
            // A file was requested from this node. Checking if the requested transfer has been prepared.
            for (final FileTransfer transfer : inboundTransfers) {
                if (NetworkID.compare(transfer.id.data, fileChunk.transferId.data) == 0) {
                    // Locating the right file transfer.
                    // Running chunk received callback.
                    transfer.onChunkReceived(fileChunk);
                    return;
                }
            }
        }
    } else if (e instanceof FileTransferFinishedEvent) {
        // Removing the transfer from the queue.
        FileTransferFinishedEvent fileTransferFinishedEvent = (FileTransferFinishedEvent) e;
        if (fileTransferFinishedEvent.transfer.fileTransferType == FileTransferType.INBOUND) {
            inboundTransfers.remove(fileTransferFinishedEvent.transfer);
        } else {
            outboundTransfers.remove(fileTransferFinishedEvent.transfer);
        }
        logger.info("File transfer " + fileTransferFinishedEvent.transfer + " finished.");
    }
}
Also used : ChannelMessageReceivedEvent(me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent) FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent)

Example 3 with ChannelMessageReceivedEvent

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

the class NetworkDatabase method onEventCalled.

/**
 * Called when the handled event is called.
 *
 * @param e
 */
@Override
public void onEventCalled(Event e) {
    // Message received event.
    if (e instanceof ChannelMessageReceivedEvent) {
        // Checking message type.
        ChannelMessageReceivedEvent messageEvent = (ChannelMessageReceivedEvent) e;
        if (messageEvent.message.msgId == MsgType.NODE_PING.ordinal()) {
            // Sending a ping message back.
            if (messageEvent.message.data.get() == 0x01) {
                return;
            }
            logger.info("Ping!");
            NetworkMessage msg = new NetworkMessage(MsgType.NODE_PING, new byte[] { 0x01 });
            messageEvent.remoteNode.send(msg);
        }
    }
}
Also used : ChannelMessageReceivedEvent(me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent) NetworkMessage(me.matoosh.undernet.p2p.router.data.message.NetworkMessage)

Aggregations

ChannelMessageReceivedEvent (me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent)3 ChannelCreatedEvent (me.matoosh.undernet.event.channel.ChannelCreatedEvent)1 FileTransferFinishedEvent (me.matoosh.undernet.event.ftp.FileTransferFinishedEvent)1 NetworkIdentity (me.matoosh.undernet.identity.NetworkIdentity)1 NetworkMessage (me.matoosh.undernet.p2p.router.data.message.NetworkMessage)1 NodeInfoMessage (me.matoosh.undernet.p2p.router.data.message.NodeInfoMessage)1