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;
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

View file

@ -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);