refactor Agent member variables

This commit is contained in:
Stephen Birarda 2013-05-15 14:12:21 -07:00
parent 2c242a7704
commit 48b57c92bd
6 changed files with 71 additions and 116 deletions

View file

@ -114,10 +114,10 @@ int main(int argc, const char* argv[]) {
if (agentBuffer->getEndOfLastWrite()) { if (agentBuffer->getEndOfLastWrite()) {
if (!agentBuffer->isStarted() if (!agentBuffer->isStarted()
&& agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) {
printf("Held back buffer for agent with ID %d.\n", agent->getAgentId()); printf("Held back buffer for agent with ID %d.\n", agent->getAgentID());
agentBuffer->setShouldBeAddedToMix(false); agentBuffer->setShouldBeAddedToMix(false);
} else if (agentBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { } else if (agentBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
printf("Buffer from agent with ID %d starved.\n", agent->getAgentId()); printf("Buffer from agent with ID %d starved.\n", agent->getAgentID());
agentBuffer->setStarted(false); agentBuffer->setStarted(false);
agentBuffer->setShouldBeAddedToMix(false); agentBuffer->setShouldBeAddedToMix(false);
} else { } else {

View file

@ -37,7 +37,7 @@
const int AVATAR_LISTEN_PORT = 55444; const int AVATAR_LISTEN_PORT = 55444;
unsigned char *addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { unsigned char *addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) {
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId()); currentPosition += packAgentId(currentPosition, agentToAdd->getAgentID());
AvatarData *agentData = (AvatarData *)agentToAdd->getLinkedData(); AvatarData *agentData = (AvatarData *)agentToAdd->getLinkedData();
currentPosition += agentData->getBroadcastData(currentPosition); currentPosition += agentData->getBroadcastData(currentPosition);

View file

@ -50,7 +50,7 @@ int lastActiveCount = 0;
unsigned char* addAgentToBroadcastPacket(unsigned char* currentPosition, Agent* agentToAdd) { unsigned char* addAgentToBroadcastPacket(unsigned char* currentPosition, Agent* agentToAdd) {
*currentPosition++ = agentToAdd->getType(); *currentPosition++ = agentToAdd->getType();
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId()); currentPosition += packAgentId(currentPosition, agentToAdd->getAgentID());
currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket());
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
@ -149,7 +149,7 @@ int main(int argc, const char * argv[])
agent->setLastHeardMicrostamp(timeNow); agent->setLastHeardMicrostamp(timeNow);
// grab the ID for this agent so we can send it back with the packet // grab the ID for this agent so we can send it back with the packet
packetAgentID = agent->getAgentId(); packetAgentID = agent->getAgentID();
if (packetData[0] == PACKET_HEADER_DOMAIN_RFD if (packetData[0] == PACKET_HEADER_DOMAIN_RFD
&& memchr(SOLO_AGENT_TYPES, agentType, sizeof(SOLO_AGENT_TYPES))) { && memchr(SOLO_AGENT_TYPES, agentType, sizeof(SOLO_AGENT_TYPES))) {

View file

@ -32,69 +32,67 @@ int packAgentId(unsigned char *packStore, uint16_t agentId) {
return sizeof(uint16_t); return sizeof(uint16_t);
} }
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId) : Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentID) :
_isAlive(true) _isAlive(true)
{ {
if (agentPublicSocket != NULL) { if (agentPublicSocket) {
publicSocket = new sockaddr; _publicSocket = new sockaddr;
memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr)); memcpy(_publicSocket, agentPublicSocket, sizeof(sockaddr));
} else { } else {
publicSocket = NULL; _publicSocket = NULL;
} }
if (agentLocalSocket != NULL) { if (agentLocalSocket) {
localSocket = new sockaddr; _localSocket = new sockaddr;
memcpy(localSocket, agentLocalSocket, sizeof(sockaddr)); memcpy(_localSocket, agentLocalSocket, sizeof(sockaddr));
} else { } else {
localSocket = NULL; _localSocket = NULL;
} }
type = agentType; _type = agentType;
agentId = thisAgentId; _agentID = thisAgentID;
_wakeMicrostamp = usecTimestampNow(); _wakeMicrostamp = usecTimestampNow();
_lastHeardMicrostamp = usecTimestampNow(); _lastHeardMicrostamp = usecTimestampNow();
activeSocket = NULL; _activeSocket = NULL;
linkedData = NULL; _linkedData = NULL;
_bytesReceivedMovingAverage = NULL; _bytesReceivedMovingAverage = NULL;
} }
Agent::Agent(const Agent &otherAgent) { Agent::Agent(const Agent &otherAgent) {
_isAlive = otherAgent._isAlive; _isAlive = otherAgent._isAlive;
if (otherAgent.publicSocket != NULL) { if (otherAgent._publicSocket) {
publicSocket = new sockaddr; _publicSocket = new sockaddr(*otherAgent._localSocket);
memcpy(publicSocket, otherAgent.publicSocket, sizeof(sockaddr));
} else { } else {
publicSocket = NULL; _publicSocket = NULL;
} }
if (otherAgent.localSocket != NULL) { if (otherAgent._localSocket) {
localSocket = new sockaddr; _localSocket = new sockaddr(*otherAgent._localSocket);
memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr));
} else { } else {
localSocket = NULL; _localSocket = NULL;
} }
agentId = otherAgent.agentId; _agentID = otherAgent._agentID;
if (otherAgent.activeSocket == otherAgent.publicSocket) { if (otherAgent._activeSocket == otherAgent._publicSocket) {
activeSocket = publicSocket; _activeSocket = _publicSocket;
} else if (otherAgent.activeSocket == otherAgent.localSocket) { } else if (otherAgent._activeSocket == otherAgent._localSocket) {
activeSocket = localSocket; _activeSocket = _localSocket;
} else { } else {
activeSocket = NULL; _activeSocket = NULL;
} }
_wakeMicrostamp = otherAgent._wakeMicrostamp; _wakeMicrostamp = otherAgent._wakeMicrostamp;
_lastHeardMicrostamp = otherAgent._lastHeardMicrostamp; _lastHeardMicrostamp = otherAgent._lastHeardMicrostamp;
type = otherAgent.type; _type = otherAgent._type;
if (otherAgent.linkedData != NULL) { if (otherAgent._linkedData) {
linkedData = otherAgent.linkedData->clone(); _linkedData = otherAgent._linkedData->clone();
} else { } else {
linkedData = NULL; _linkedData = NULL;
} }
if (otherAgent._bytesReceivedMovingAverage != NULL) { if (otherAgent._bytesReceivedMovingAverage != NULL) {
@ -114,28 +112,24 @@ void Agent::swap(Agent &first, Agent &second) {
using std::swap; using std::swap;
swap(first._isAlive, second._isAlive); swap(first._isAlive, second._isAlive);
swap(first.publicSocket, second.publicSocket); swap(first._publicSocket, second._publicSocket);
swap(first.localSocket, second.localSocket); swap(first._localSocket, second._localSocket);
swap(first.activeSocket, second.activeSocket); swap(first._activeSocket, second._activeSocket);
swap(first.type, second.type); swap(first._type, second._type);
swap(first.linkedData, second.linkedData); swap(first._linkedData, second._linkedData);
swap(first.agentId, second.agentId); swap(first._agentID, second._agentID);
swap(first._wakeMicrostamp, second._wakeMicrostamp); swap(first._wakeMicrostamp, second._wakeMicrostamp);
swap(first._lastHeardMicrostamp, second._lastHeardMicrostamp); swap(first._lastHeardMicrostamp, second._lastHeardMicrostamp);
swap(first._bytesReceivedMovingAverage, second._bytesReceivedMovingAverage); swap(first._bytesReceivedMovingAverage, second._bytesReceivedMovingAverage);
} }
Agent::~Agent() { Agent::~Agent() {
delete publicSocket; delete _publicSocket;
delete localSocket; delete _localSocket;
delete linkedData; delete _linkedData;
delete _bytesReceivedMovingAverage; delete _bytesReceivedMovingAverage;
} }
char Agent::getType() const {
return type;
}
// Names of Agent Types // Names of Agent Types
const char* AGENT_TYPE_NAME_DOMAIN = "Domain"; const char* AGENT_TYPE_NAME_DOMAIN = "Domain";
const char* AGENT_TYPE_NAME_VOXEL = "Voxel Server"; const char* AGENT_TYPE_NAME_VOXEL = "Voxel Server";
@ -146,7 +140,7 @@ const char* AGENT_TYPE_NAME_AUDIO_INJECTOR = "Audio Injector";
const char* AGENT_TYPE_NAME_UNKNOWN = "Unknown"; const char* AGENT_TYPE_NAME_UNKNOWN = "Unknown";
const char* Agent::getTypeName() const { const char* Agent::getTypeName() const {
switch (this->type) { switch (this->_type) {
case AGENT_TYPE_DOMAIN: case AGENT_TYPE_DOMAIN:
return AGENT_TYPE_NAME_DOMAIN; return AGENT_TYPE_NAME_DOMAIN;
case AGENT_TYPE_VOXEL: case AGENT_TYPE_VOXEL:
@ -164,63 +158,23 @@ const char* Agent::getTypeName() const {
} }
} }
void Agent::setType(char newType) {
type = newType;
}
uint16_t Agent::getAgentId() {
return agentId;
}
void Agent::setAgentId(uint16_t thisAgentId) {
agentId = thisAgentId;
}
sockaddr* Agent::getPublicSocket() {
return publicSocket;
}
void Agent::setPublicSocket(sockaddr *newSocket) {
publicSocket = newSocket;
}
sockaddr* Agent::getLocalSocket() {
return localSocket;
}
void Agent::setLocalSocket(sockaddr *newSocket) {
publicSocket = newSocket;
}
sockaddr* Agent::getActiveSocket() {
return activeSocket;
}
void Agent::activateLocalSocket() { void Agent::activateLocalSocket() {
activeSocket = localSocket; _activeSocket = _localSocket;
} }
void Agent::activatePublicSocket() { void Agent::activatePublicSocket() {
activeSocket = publicSocket; _activeSocket = _publicSocket;
}
AgentData* Agent::getLinkedData() {
return linkedData;
}
void Agent::setLinkedData(AgentData *newData) {
linkedData = newData;
} }
bool Agent::operator==(const Agent& otherAgent) { bool Agent::operator==(const Agent& otherAgent) {
return matches(otherAgent.publicSocket, otherAgent.localSocket, otherAgent.type); return matches(otherAgent._publicSocket, otherAgent._localSocket, otherAgent._type);
} }
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
&& socketMatch(publicSocket, otherPublicSocket) && socketMatch(_publicSocket, otherPublicSocket)
&& socketMatch(localSocket, otherLocalSocket); && socketMatch(_localSocket, otherLocalSocket);
} }
void Agent::recordBytesReceived(int bytesReceived) { void Agent::recordBytesReceived(int bytesReceived) {
@ -250,15 +204,15 @@ float Agent::getAverageKilobitsPerSecond() {
void Agent::printLog(Agent const& agent) { void Agent::printLog(Agent const& agent) {
char publicAddressBuffer[16] = {'\0'}; char publicAddressBuffer[16] = {'\0'};
unsigned short publicAddressPort = loadBufferWithSocketInfo(publicAddressBuffer, agent.publicSocket); unsigned short publicAddressPort = loadBufferWithSocketInfo(publicAddressBuffer, agent._publicSocket);
//char localAddressBuffer[16] = {'\0'}; //char localAddressBuffer[16] = {'\0'};
//unsigned short localAddressPort = loadBufferWithSocketInfo(localAddressBuffer, agent.localSocket); //unsigned short localAddressPort = loadBufferWithSocketInfo(localAddressBuffer, agent.localSocket);
::printLog("# %d %s (%c) @ %s:%d\n", ::printLog("# %d %s (%c) @ %s:%d\n",
agent.agentId, agent._agentID,
agent.getTypeName(), agent.getTypeName(),
agent.type, agent._type,
publicAddressBuffer, publicAddressBuffer,
publicAddressPort); publicAddressPort);
} }

View file

@ -23,7 +23,7 @@
class Agent { class Agent {
public: public:
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId); Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentID);
Agent(const Agent &otherAgent); Agent(const Agent &otherAgent);
~Agent(); ~Agent();
Agent& operator=(Agent otherAgent); Agent& operator=(Agent otherAgent);
@ -31,12 +31,12 @@ public:
bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType);
char getType() const; char getType() const { return _type; }
void setType(char type) { _type = type; }
const char* getTypeName() const; const char* getTypeName() const;
void setType(char newType);
uint16_t getAgentId(); uint16_t getAgentID() const { return _agentID; }
void setAgentId(uint16_t thisAgentId); void setAgentID(uint16_t agentID) { _agentID = agentID;}
double getWakeMicrostamp() const { return _wakeMicrostamp; } double getWakeMicrostamp() const { return _wakeMicrostamp; }
void setWakeMicrostamp(double wakeMicrostamp) { _wakeMicrostamp = wakeMicrostamp; } void setWakeMicrostamp(double wakeMicrostamp) { _wakeMicrostamp = wakeMicrostamp; }
@ -44,17 +44,18 @@ public:
double getLastHeardMicrostamp() const { return _lastHeardMicrostamp; } double getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
void setLastHeardMicrostamp(double lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; } void setLastHeardMicrostamp(double lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
sockaddr* getPublicSocket(); sockaddr* getPublicSocket() const { return _publicSocket; }
void setPublicSocket(sockaddr *newSocket); void setPublicSocket(sockaddr *publicSocket) { _publicSocket = publicSocket; }
sockaddr* getLocalSocket(); sockaddr* getLocalSocket() const { return _localSocket; }
void setLocalSocket(sockaddr *newSocket); void setLocalSocket(sockaddr *localSocket) { _localSocket = localSocket; }
sockaddr* getActiveSocket();
sockaddr* getActiveSocket() const { return _activeSocket; }
void activatePublicSocket(); void activatePublicSocket();
void activateLocalSocket(); void activateLocalSocket();
AgentData* getLinkedData(); AgentData* getLinkedData() const { return _linkedData; }
void setLinkedData(AgentData *newData); void setLinkedData(AgentData *linkedData) { _linkedData = linkedData; }
bool isAlive() const { return _isAlive; }; bool isAlive() const { return _isAlive; };
void setAlive(bool isAlive) { _isAlive = isAlive; }; void setAlive(bool isAlive) { _isAlive = isAlive; };
@ -67,13 +68,13 @@ public:
private: private:
void swap(Agent &first, Agent &second); void swap(Agent &first, Agent &second);
sockaddr *publicSocket, *localSocket, *activeSocket; sockaddr* _publicSocket, *_localSocket, *_activeSocket;
char type; char _type;
uint16_t agentId; uint16_t _agentID;
double _wakeMicrostamp; double _wakeMicrostamp;
double _lastHeardMicrostamp; double _lastHeardMicrostamp;
SimpleMovingAverage* _bytesReceivedMovingAverage; SimpleMovingAverage* _bytesReceivedMovingAverage;
AgentData* linkedData; AgentData* _linkedData;
bool _isAlive; bool _isAlive;
}; };

View file

@ -184,7 +184,7 @@ Agent* AgentList::agentWithAddress(sockaddr *senderAddress) {
Agent* AgentList::agentWithID(uint16_t agentID) { Agent* AgentList::agentWithID(uint16_t agentID) {
for(AgentList::iterator agent = begin(); agent != end(); agent++) { for(AgentList::iterator agent = begin(); agent != end(); agent++) {
if (agent->getAgentId() == agentID) { if (agent->getAgentID() == agentID) {
return &(*agent); return &(*agent);
} }
} }