From de3f5edd69fc2d5b67e817282af22074830bccd7 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 28 Mar 2013 14:32:20 -0700 Subject: [PATCH] add a mutex to the agent so we don't delete it when using it --- shared/src/Agent.cpp | 25 +++++++++------ shared/src/Agent.h | 73 +++++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index 8ec12c9020..677b4884fe 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -34,6 +34,8 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent activeSocket = NULL; linkedData = NULL; + + pthread_mutex_init(&deleteMutex, NULL); } Agent::Agent(const Agent &otherAgent) { @@ -62,6 +64,8 @@ Agent::Agent(const Agent &otherAgent) { } else { linkedData = NULL; } + + deleteMutex = otherAgent.deleteMutex; } Agent& Agent::operator=(Agent otherAgent) { @@ -69,6 +73,17 @@ Agent& Agent::operator=(Agent otherAgent) { return *this; } +void Agent::swap(Agent &first, Agent &second) { + using std::swap; + swap(first.publicSocket, second.publicSocket); + swap(first.localSocket, second.localSocket); + swap(first.activeSocket, second.activeSocket); + swap(first.type, second.type); + swap(first.linkedData, second.linkedData); + swap(first.agentId, second.agentId); + swap(first.deleteMutex, second.deleteMutex); +} + Agent::~Agent() { delete publicSocket; delete localSocket; @@ -148,16 +163,6 @@ bool Agent::operator==(const Agent& otherAgent) { return matches(otherAgent.publicSocket, otherAgent.localSocket, otherAgent.type); } -void Agent::swap(Agent &first, Agent &second) { - using std::swap; - swap(first.publicSocket, second.publicSocket); - swap(first.localSocket, second.localSocket); - swap(first.activeSocket, second.activeSocket); - swap(first.type, second.type); - swap(first.linkedData, second.linkedData); - swap(first.agentId, second.agentId); -} - bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) { // checks if two agent objects are the same agent (same type + local + public address) return type == otherAgentType diff --git a/shared/src/Agent.h b/shared/src/Agent.h index 4fc7bd9a83..1b86e95e9b 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -20,42 +20,45 @@ #endif class Agent { - public: - Agent(); - Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId); - Agent(const Agent &otherAgent); - ~Agent(); - Agent& operator=(Agent otherAgent); - bool operator==(const Agent& otherAgent); - - bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); - char getType(); - void setType(char newType); - uint16_t getAgentId(); - void setAgentId(uint16_t thisAgentId); - double getFirstRecvTimeUsecs(); - void setFirstRecvTimeUsecs(double newTimeUsecs); - double getLastRecvTimeUsecs(); - void setLastRecvTimeUsecs(double newTimeUsecs); - sockaddr* getPublicSocket(); - void setPublicSocket(sockaddr *newSocket); - sockaddr* getLocalSocket(); - void setLocalSocket(sockaddr *newSocket); - sockaddr* getActiveSocket(); - void activatePublicSocket(); - void activateLocalSocket(); - AgentData* getLinkedData(); - void setLinkedData(AgentData *newData); + void swap(Agent &first, Agent &second); + sockaddr *publicSocket, *localSocket, *activeSocket; + char type; + uint16_t agentId; + double firstRecvTimeUsecs; + double lastRecvTimeUsecs; + AgentData *linkedData; - friend std::ostream& operator<<(std::ostream& os, const Agent* agent); - private: - void swap(Agent &first, Agent &second); - sockaddr *publicSocket, *localSocket, *activeSocket; - char type; - uint16_t agentId; - double firstRecvTimeUsecs; - double lastRecvTimeUsecs; - AgentData *linkedData; +public: + Agent(); + Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId); + Agent(const Agent &otherAgent); + ~Agent(); + Agent& operator=(Agent otherAgent); + bool operator==(const Agent& otherAgent); + + bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); + + pthread_mutex_t deleteMutex; + + char getType(); + void setType(char newType); + uint16_t getAgentId(); + void setAgentId(uint16_t thisAgentId); + double getFirstRecvTimeUsecs(); + void setFirstRecvTimeUsecs(double newTimeUsecs); + double getLastRecvTimeUsecs(); + void setLastRecvTimeUsecs(double newTimeUsecs); + sockaddr* getPublicSocket(); + void setPublicSocket(sockaddr *newSocket); + sockaddr* getLocalSocket(); + void setLocalSocket(sockaddr *newSocket); + sockaddr* getActiveSocket(); + void activatePublicSocket(); + void activateLocalSocket(); + AgentData* getLinkedData(); + void setLinkedData(AgentData *newData); + + friend std::ostream& operator<<(std::ostream& os, const Agent* agent); }; std::ostream& operator<<(std::ostream& os, const Agent* agent);