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);
}
}
}
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.");
}
}
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);
}
}
}
Aggregations