private synchronized void addNewAgent( int agentId, SocketChannel socket, String agentName, String agentIP, int agentPort, int flags) { if (agentIP.equals(":same")) { InetAddress agentAddress = socket.socket().getInetAddress(); agentIP = agentAddress.getHostAddress(); } Log.info( "New agent id=" + agentId + " name=" + agentName + " address=" + agentIP + ":" + agentPort + " flags=" + flags); AgentInfo agentInfo = new AgentInfo(); agentInfo.agentId = agentId; agentInfo.flags = flags; agentInfo.socket = socket; agentInfo.agentName = agentName; agentInfo.agentIP = agentIP; agentInfo.agentPort = agentPort; agentInfo.outputBuf = new MVByteBuffer(1024); agentInfo.inputBuf = new MVByteBuffer(1024); agents.put(socket, agentInfo); NewAgentMessage newAgentMessage = new NewAgentMessage(agentId, agentName, agentIP, agentPort, flags); for (Map.Entry<SocketChannel, AgentInfo> entry : agents.entrySet()) { if (entry.getKey() == socket) continue; // Tell other agents about the new one synchronized (entry.getValue().outputBuf) { Message.toBytes(newAgentMessage, entry.getValue().outputBuf); } // Tell new agent about other agents NewAgentMessage otherAgentMessage = new NewAgentMessage( entry.getValue().agentId, entry.getValue().agentName, entry.getValue().agentIP, entry.getValue().agentPort, entry.getValue().flags); synchronized (agentInfo.outputBuf) { Message.toBytes(otherAgentMessage, agentInfo.outputBuf); } } messageIO.addAgent(agentInfo); messageIO.outputReady(); }
public void handleMessageData(int length, MVByteBuffer messageData, AgentInfo agentInfo) { if (length == -1 || messageData == null) { if ((agentInfo.flags & MessageAgent.DOMAIN_FLAG_TRANSIENT) != 0) { Log.info("Lost connection to '" + agentInfo.agentName + "' (transient)"); agents.remove(agentInfo.socket); agentNames.remove(agentInfo.agentName); messageIO.removeAgent(agentInfo); } else Log.info("Lost connection to '" + agentInfo.agentName + "'"); try { agentInfo.socket.close(); } catch (java.io.IOException ex) { Log.exception("close", ex); } agentInfo.socket = null; // ## clear buffers // ## keep agentInfo? to preserve agentId? return; } Message message = (Message) MarshallingRuntime.unmarshalObject(messageData); MessageType msgType = message.getMsgType(); if (Log.loggingDebug) Log.debug( "handleMessageData from " + agentInfo.agentName + "," + message.getMsgId() + " type=" + msgType.getMsgTypeString() + " len=" + length + " class=" + message.getClass().getName()); try { if (message instanceof AllocNameMessage) handleAllocName((AllocNameMessage) message, agentInfo.socket); else if (message instanceof AwaitPluginDependentsMessage) handleAwaitPluginDependents((AwaitPluginDependentsMessage) message, agentInfo.socket); else if (message instanceof PluginAvailableMessage) handlePluginAvailable((PluginAvailableMessage) message, agentInfo.socket); else Log.error( "Unsupported message from " + agentInfo.agentName + "," + message.getMsgId() + " type=" + msgType.getMsgTypeString() + " len=" + length + " class=" + message.getClass().getName()); } catch (java.io.IOException e) { Log.error( "IO error on message from " + agentInfo.agentName + "," + message.getMsgId() + " type=" + msgType.getMsgTypeString() + " len=" + length + " class=" + message.getClass().getName()); } }