public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
GraphLabMessage message = (GraphLabMessage) e.getMessage();
switch(message.getMessageId()) {
case MessageIds.HANDSHAKE:
HandshakeMessage handshake = (HandshakeMessage) message;
// Must be faster way to do this..
if (channelIdToNodeId.values().contains(handshake.getNodeId())) {
System.out.println("Warning: already had node id, replacing");
Integer keyToRemove = null;
for(Map.Entry<Integer, Integer> entry : channelIdToNodeId.entrySet()) {
if (entry.getValue() == handshake.getNodeId()) {
keyToRemove = entry.getKey();
break;
}
}
if (keyToRemove != null) channelIdToNodeId.remove(keyToRemove);
}
// Register
GraphLabNodeInfo nodeInfo = new GraphLabNodeInfo(handshake.getNodeId(),
InetAddress.getByName(handshake.getAddress()),
handshake.getPort());
synchronized (this) {
nodes.put(handshake.getNodeId(),
nodeInfo);
channelIdToNodeId.put(e.getChannel().getId(), handshake.getNodeId());
nodesToChannels.put(handshake.getNodeId(), ctx.getChannel());
// Send all other nodes this node
for(Integer nodeId: nodesToChannels.keySet()) {
System.out.println("*" + nodeId + "; " + handshake.getNodeId());
if (nodeId != handshake.getNodeId()) {
sendClient(nodeId, new NodeInfoMessage(nodeInfo));
// Send other clients to this node
sendClient(nodeInfo.getId(), new NodeInfoMessage(nodes.get(nodeId)));
}
}