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