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 run() { synchronized (pluginStartGroup) { waitForDependencies(); } if (Log.loggingDebug) Log.debug( "Dependency satisfied for type=" + await.getPluginType() + " name=" + await.getPluginName()); MVByteBuffer buffer = new MVByteBuffer(1024); ResponseMessage response = new ResponseMessage(await); Message.toBytes(response, buffer); buffer.flip(); try { if (!ChannelUtil.writeBuffer(buffer, agentSocket)) { Log.error("could not write await dependencies response"); } } catch (java.io.IOException e) { Log.exception("could not write await dependencies response", e); } }
void handleAllocName(AllocNameMessage allocName, SocketChannel agentSocket) throws java.io.IOException { if (allocName.getMsgType() != MessageTypes.MSG_TYPE_ALLOC_NAME) { Log.error("DomainServer: invalid alloc name message"); return; } String agentName = allocName(allocName.getType(), allocName.getAgentName()); MVByteBuffer buffer = new MVByteBuffer(1024); AllocNameResponseMessage allocNameResponse = new AllocNameResponseMessage(allocName, agentName); Message.toBytes(allocNameResponse, buffer); buffer.flip(); if (!ChannelUtil.writeBuffer(buffer, agentSocket)) { throw new RuntimeException("could not write alloc name response"); } }
boolean handleAgentHello(AgentHelloMessage agentHello) throws java.io.IOException { if (agentHello.getMsgType() != MessageTypes.MSG_TYPE_AGENT_HELLO) { Log.error( "DomainServer: invalid agent hello, got message type " + agentHello.getMsgType() + " from " + agentSocket); return false; } int agentId; synchronized (domainServer) { agentId = getNextAgentId(); if (!agentNames.contains(agentHello.getAgentName())) agentNames.add(agentHello.getAgentName()); } MVByteBuffer buffer = new MVByteBuffer(1024); HelloResponseMessage helloResponse = new HelloResponseMessage(agentId, domainStartTime, agentNames, encodedDomainKey); Message.toBytes(helloResponse, buffer); buffer.flip(); if (!ChannelUtil.writeBuffer(buffer, agentSocket)) { Log.error("could not write to new agent, " + agentSocket); return false; } addNewAgent( agentId, agentSocket, agentHello.getAgentName(), agentHello.getAgentIP(), agentHello.getAgentPort(), agentHello.getFlags()); return true; }