From 72ddc78ad659b7ea320499746fe0e9726bff6cb3 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 25 Feb 2013 11:52:38 -0800 Subject: [PATCH] domain server only returns newest of types in SOLO_AGENT_TYPES_STRING --- domain/src/main.cpp | 36 +++++++++++++++++++++++++++++++----- shared/src/Agent.cpp | 3 +++ shared/src/Agent.h | 1 + shared/src/AgentList.cpp | 2 ++ shared/src/AgentList.h | 1 + 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 5f9b075c48..7dd89df557 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "AgentList.h" #include "SharedUtil.h" @@ -42,6 +43,15 @@ const int LOGOFF_CHECK_INTERVAL = 5000; int lastActiveCount = 0; AgentList agentList(DOMAIN_LISTEN_PORT); +unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { + *currentPosition++ = agentToAdd->type; + + currentPosition += packSocket(currentPosition, agentToAdd->publicSocket); + currentPosition += packSocket(currentPosition, agentToAdd->localSocket); + + // return the new unsigned char * for broadcast packet + return currentPosition; +} int main(int argc, const char * argv[]) { @@ -60,6 +70,8 @@ int main(int argc, const char * argv[]) agentList.startSilentAgentRemovalThread(); + std::map newestSoloAgents; + while (true) { if (agentList.getAgentSocket()->receive((sockaddr *)&agentPublicAddress, packetData, &receivedBytes)) { agentType = packetData[0]; @@ -73,17 +85,31 @@ int main(int argc, const char * argv[]) for(std::vector::iterator agent = agentList.agents.begin(); agent != agentList.agents.end(); agent++) { if (DEBUG_TO_SELF || !agent->matches((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType)) { - *currentBufferPos++ = agent->type; - - currentBufferPos += packSocket(currentBufferPos, agent->publicSocket); - currentBufferPos += packSocket(currentBufferPos, agent->localSocket); + if (strchr(SOLO_AGENT_TYPES_STRING, (int) agent->type) == NULL) { + // this is an agent of which there can be multiple, just add them to the packet + currentBufferPos = addAgentToBroadcastPacket(currentBufferPos, &(*agent)); + } else { + std::cout << "We have a solo agent: " << &(*agent) << "\n"; + // solo agent, we need to only send newest + if (newestSoloAgents[agent->type] == NULL || + newestSoloAgents[agent->type]->firstRecvTimeUsecs < agent->firstRecvTimeUsecs) { + // we have to set the newer solo agent to add it to the broadcast later + newestSoloAgents[agent->type] = &(*agent); + } + } } else { // this is the agent, just update last receive to now agent->lastRecvTimeUsecs = usecTimestampNow(); } } - ; + for (std::map::iterator agentIterator = newestSoloAgents.begin(); + agentIterator != newestSoloAgents.end(); + agentIterator++) { + std::cout << "Newest agent: " << agentIterator->second << "\n"; + // this is the newest alive solo agent, add them to the packet + currentBufferPos = addAgentToBroadcastPacket(currentBufferPos, agentIterator->second); + } if ((packetBytesWithoutLeadingChar = (currentBufferPos - startPointer))) { agentList.getAgentSocket()->send((sockaddr *)&agentPublicAddress, broadcastPacket, packetBytesWithoutLeadingChar + 1); diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index 5b48033225..5790329c80 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -22,6 +22,8 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent memcpy(localSocket, agentLocalSocket, sizeof(sockaddr)); type = agentType; + + firstRecvTimeUsecs = usecTimestampNow(); lastRecvTimeUsecs = usecTimestampNow(); activeSocket = NULL; @@ -43,6 +45,7 @@ Agent::Agent(const Agent &otherAgent) { activeSocket = NULL; } + firstRecvTimeUsecs = otherAgent.firstRecvTimeUsecs; lastRecvTimeUsecs = otherAgent.lastRecvTimeUsecs; type = otherAgent.type; diff --git a/shared/src/Agent.h b/shared/src/Agent.h index efcebc3b05..90073a92b0 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -28,6 +28,7 @@ class Agent { char type; timeval pingStarted; int pingMsecs; + double firstRecvTimeUsecs; double lastRecvTimeUsecs; bool isSelf; AgentData *linkedData; diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index d068ab15fc..fdf4e430ff 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -11,6 +11,8 @@ #include #include "SharedUtil.h" +const char * SOLO_AGENT_TYPES_STRING = "M"; + bool stopAgentRemovalThread = false; pthread_mutex_t vectorChangeMutex = PTHREAD_MUTEX_INITIALIZER; diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index 399340b841..734d64da9e 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -16,6 +16,7 @@ const unsigned short AGENT_SOCKET_LISTEN_PORT = 40103; const int AGENT_SILENCE_THRESHOLD_USECS = 2 * 1000000; +extern const char *SOLO_AGENT_TYPES_STRINGg; class AgentList { public: