add a mutex to the agent so we don't delete it when using it

This commit is contained in:
Stephen Birarda 2013-03-28 14:32:20 -07:00
parent a41b003582
commit de3f5edd69
2 changed files with 53 additions and 45 deletions

View file

@ -34,6 +34,8 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent
activeSocket = NULL; activeSocket = NULL;
linkedData = NULL; linkedData = NULL;
pthread_mutex_init(&deleteMutex, NULL);
} }
Agent::Agent(const Agent &otherAgent) { Agent::Agent(const Agent &otherAgent) {
@ -62,6 +64,8 @@ Agent::Agent(const Agent &otherAgent) {
} else { } else {
linkedData = NULL; linkedData = NULL;
} }
deleteMutex = otherAgent.deleteMutex;
} }
Agent& Agent::operator=(Agent otherAgent) { Agent& Agent::operator=(Agent otherAgent) {
@ -69,6 +73,17 @@ Agent& Agent::operator=(Agent otherAgent) {
return *this; 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() { Agent::~Agent() {
delete publicSocket; delete publicSocket;
delete localSocket; delete localSocket;
@ -148,16 +163,6 @@ bool Agent::operator==(const Agent& otherAgent) {
return matches(otherAgent.publicSocket, otherAgent.localSocket, otherAgent.type); 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) { bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) {
// checks if two agent objects are the same agent (same type + local + public address) // checks if two agent objects are the same agent (same type + local + public address)
return type == otherAgentType return type == otherAgentType

View file

@ -20,42 +20,45 @@
#endif #endif
class Agent { class Agent {
public: void swap(Agent &first, Agent &second);
Agent(); sockaddr *publicSocket, *localSocket, *activeSocket;
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId); char type;
Agent(const Agent &otherAgent); uint16_t agentId;
~Agent(); double firstRecvTimeUsecs;
Agent& operator=(Agent otherAgent); double lastRecvTimeUsecs;
bool operator==(const Agent& otherAgent); AgentData *linkedData;
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);
friend std::ostream& operator<<(std::ostream& os, const Agent* agent); public:
private: Agent();
void swap(Agent &first, Agent &second); Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId);
sockaddr *publicSocket, *localSocket, *activeSocket; Agent(const Agent &otherAgent);
char type; ~Agent();
uint16_t agentId; Agent& operator=(Agent otherAgent);
double firstRecvTimeUsecs; bool operator==(const Agent& otherAgent);
double lastRecvTimeUsecs;
AgentData *linkedData; 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); std::ostream& operator<<(std::ostream& os, const Agent* agent);