mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 22:35:14 +02:00
Merge pull request #14 from murillodigital/19179
Add unique id to agents and broadcast back
This commit is contained in:
commit
88b899efb7
6 changed files with 64 additions and 12 deletions
|
@ -50,6 +50,7 @@ AgentList agentList(DOMAIN_LISTEN_PORT);
|
||||||
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 += packSocket(currentPosition, agentToAdd->getPublicSocket());
|
currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket());
|
||||||
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
|
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
|
||||||
|
|
||||||
|
@ -83,7 +84,14 @@ int main(int argc, const char * argv[])
|
||||||
agentType = packetData[0];
|
agentType = packetData[0];
|
||||||
unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress);
|
unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress);
|
||||||
|
|
||||||
agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType);
|
if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress,
|
||||||
|
(sockaddr *)&agentLocalAddress,
|
||||||
|
agentType,
|
||||||
|
agentList.getLastAgentId())) {
|
||||||
|
|
||||||
|
agentList.increaseAgentId();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
currentBufferPos = broadcastPacket + 1;
|
currentBufferPos = broadcastPacket + 1;
|
||||||
startPointer = currentBufferPos;
|
startPointer = currentBufferPos;
|
||||||
|
|
|
@ -229,9 +229,9 @@ void Timer(int extra)
|
||||||
|
|
||||||
//
|
//
|
||||||
// Send a message to the domainserver telling it we are ALIVE
|
// Send a message to the domainserver telling it we are ALIVE
|
||||||
//
|
//
|
||||||
unsigned char output[7];
|
unsigned char output[7];
|
||||||
output[0] = 'I';
|
output[0] = 'I';
|
||||||
packSocket(output + 1, localAddress, htons(AGENT_SOCKET_LISTEN_PORT));
|
packSocket(output + 1, localAddress, htons(AGENT_SOCKET_LISTEN_PORT));
|
||||||
|
|
||||||
agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7);
|
agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7);
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
Agent::Agent() {}
|
Agent::Agent() {}
|
||||||
|
|
||||||
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType) {
|
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId) {
|
||||||
publicSocket = new sockaddr;
|
publicSocket = new sockaddr;
|
||||||
memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr));
|
memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr));
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent
|
||||||
memcpy(localSocket, agentLocalSocket, sizeof(sockaddr));
|
memcpy(localSocket, agentLocalSocket, sizeof(sockaddr));
|
||||||
|
|
||||||
type = agentType;
|
type = agentType;
|
||||||
|
agentId = thisAgentId;
|
||||||
|
|
||||||
firstRecvTimeUsecs = usecTimestampNow();
|
firstRecvTimeUsecs = usecTimestampNow();
|
||||||
lastRecvTimeUsecs = usecTimestampNow();
|
lastRecvTimeUsecs = usecTimestampNow();
|
||||||
|
@ -42,6 +43,8 @@ Agent::Agent(const Agent &otherAgent) {
|
||||||
localSocket = new sockaddr;
|
localSocket = new sockaddr;
|
||||||
memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr));
|
memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr));
|
||||||
|
|
||||||
|
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) {
|
||||||
|
@ -80,6 +83,14 @@ void Agent::setType(char newType) {
|
||||||
type = newType;
|
type = newType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t Agent::getAgentId() {
|
||||||
|
return agentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Agent::setAgentId(uint16_t thisAgentId) {
|
||||||
|
agentId = thisAgentId;
|
||||||
|
}
|
||||||
|
|
||||||
double Agent::getFirstRecvTimeUsecs() {
|
double Agent::getFirstRecvTimeUsecs() {
|
||||||
return firstRecvTimeUsecs;
|
return firstRecvTimeUsecs;
|
||||||
}
|
}
|
||||||
|
@ -144,6 +155,7 @@ void Agent::swap(Agent &first, Agent &second) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) {
|
bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) {
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
class Agent {
|
class Agent {
|
||||||
public:
|
public:
|
||||||
Agent();
|
Agent();
|
||||||
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType);
|
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);
|
||||||
|
@ -30,6 +30,8 @@ class Agent {
|
||||||
bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType);
|
bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType);
|
||||||
char getType();
|
char getType();
|
||||||
void setType(char newType);
|
void setType(char newType);
|
||||||
|
uint16_t getAgentId();
|
||||||
|
void setAgentId(uint16_t thisAgentId);
|
||||||
double getFirstRecvTimeUsecs();
|
double getFirstRecvTimeUsecs();
|
||||||
void setFirstRecvTimeUsecs(double newTimeUsecs);
|
void setFirstRecvTimeUsecs(double newTimeUsecs);
|
||||||
double getLastRecvTimeUsecs();
|
double getLastRecvTimeUsecs();
|
||||||
|
@ -49,6 +51,7 @@ class Agent {
|
||||||
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;
|
||||||
double firstRecvTimeUsecs;
|
double firstRecvTimeUsecs;
|
||||||
double lastRecvTimeUsecs;
|
double lastRecvTimeUsecs;
|
||||||
AgentData *linkedData;
|
AgentData *linkedData;
|
||||||
|
|
|
@ -24,11 +24,14 @@ pthread_mutex_t vectorChangeMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) {
|
AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) {
|
||||||
linkedDataCreateCallback = NULL;
|
linkedDataCreateCallback = NULL;
|
||||||
audioMixerSocketUpdate = NULL;
|
audioMixerSocketUpdate = NULL;
|
||||||
|
lastAgentId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) {
|
AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) {
|
||||||
linkedDataCreateCallback = NULL;
|
linkedDataCreateCallback = NULL;
|
||||||
audioMixerSocketUpdate = NULL;
|
audioMixerSocketUpdate = NULL;
|
||||||
|
lastAgentId = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AgentList::~AgentList() {
|
AgentList::~AgentList() {
|
||||||
|
@ -105,10 +108,19 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t AgentList::getLastAgentId() {
|
||||||
|
return lastAgentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AgentList::increaseAgentId() {
|
||||||
|
++lastAgentId;
|
||||||
|
}
|
||||||
|
|
||||||
int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
||||||
int readAgents = 0;
|
int readAgents = 0;
|
||||||
|
|
||||||
char agentType;
|
char agentType;
|
||||||
|
uint16_t agentId;
|
||||||
|
|
||||||
// assumes only IPv4 addresses
|
// assumes only IPv4 addresses
|
||||||
sockaddr_in agentPublicSocket;
|
sockaddr_in agentPublicSocket;
|
||||||
|
@ -121,19 +133,20 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
||||||
|
|
||||||
while((readPtr - startPtr) < dataBytes) {
|
while((readPtr - startPtr) < dataBytes) {
|
||||||
agentType = *readPtr++;
|
agentType = *readPtr++;
|
||||||
|
readPtr += unpackAgentId(readPtr, (uint16_t *)&agentId);
|
||||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket);
|
readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket);
|
||||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket);
|
readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket);
|
||||||
|
|
||||||
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType);
|
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType, agentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return readAgents;
|
return readAgents;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType) {
|
bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId) {
|
||||||
std::vector<Agent>::iterator agent;
|
std::vector<Agent>::iterator agent;
|
||||||
|
|
||||||
for(agent = agents.begin(); agent != agents.end(); agent++) {
|
for (agent = agents.begin(); agent != agents.end(); agent++) {
|
||||||
if (agent->matches(publicSocket, localSocket, agentType)) {
|
if (agent->matches(publicSocket, localSocket, agentType)) {
|
||||||
// we already have this agent, stop checking
|
// we already have this agent, stop checking
|
||||||
break;
|
break;
|
||||||
|
@ -142,7 +155,8 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
|
||||||
|
|
||||||
if (agent == agents.end()) {
|
if (agent == agents.end()) {
|
||||||
// we didn't have this agent, so add them
|
// we didn't have this agent, so add them
|
||||||
Agent newAgent = Agent(publicSocket, localSocket, agentType);
|
|
||||||
|
Agent newAgent = Agent(publicSocket, localSocket, agentType, agentId);
|
||||||
|
|
||||||
if (socketMatch(publicSocket, localSocket)) {
|
if (socketMatch(publicSocket, localSocket)) {
|
||||||
// likely debugging scenario with DS + agent on local network
|
// likely debugging scenario with DS + agent on local network
|
||||||
|
@ -261,4 +275,14 @@ void AgentList::startSilentAgentRemovalThread() {
|
||||||
void AgentList::stopSilentAgentRemovalThread() {
|
void AgentList::stopSilentAgentRemovalThread() {
|
||||||
stopAgentRemovalThread = true;
|
stopAgentRemovalThread = true;
|
||||||
pthread_join(removeSilentAgentsThread, NULL);
|
pthread_join(removeSilentAgentsThread, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int unpackAgentId(unsigned char *packedData, uint16_t *agentId) {
|
||||||
|
memcpy(packedData, agentId, sizeof(uint16_t));
|
||||||
|
return sizeof(uint16_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
int packAgentId(unsigned char *packStore, uint16_t agentId) {
|
||||||
|
memcpy(&agentId, packStore, sizeof(uint16_t));
|
||||||
|
return sizeof(uint16_t);
|
||||||
}
|
}
|
|
@ -34,10 +34,12 @@ class AgentList {
|
||||||
|
|
||||||
std::vector<Agent>& getAgents();
|
std::vector<Agent>& getAgents();
|
||||||
UDPSocket& getAgentSocket();
|
UDPSocket& getAgentSocket();
|
||||||
|
|
||||||
int updateList(unsigned char *packetData, size_t dataBytes);
|
int updateList(unsigned char *packetData, size_t dataBytes);
|
||||||
int indexOfMatchingAgent(sockaddr *senderAddress);
|
int indexOfMatchingAgent(sockaddr *senderAddress);
|
||||||
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType);
|
uint16_t getLastAgentId();
|
||||||
|
void increaseAgentId();
|
||||||
|
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId);
|
||||||
void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
||||||
void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
||||||
void broadcastToAgents(char *broadcastData, size_t dataBytes);
|
void broadcastToAgents(char *broadcastData, size_t dataBytes);
|
||||||
|
@ -49,8 +51,11 @@ class AgentList {
|
||||||
UDPSocket agentSocket;
|
UDPSocket agentSocket;
|
||||||
std::vector<Agent> agents;
|
std::vector<Agent> agents;
|
||||||
pthread_t removeSilentAgentsThread;
|
pthread_t removeSilentAgentsThread;
|
||||||
|
uint16_t lastAgentId;
|
||||||
void handlePingReply(sockaddr *agentAddress);
|
void handlePingReply(sockaddr *agentAddress);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int unpackAgentId(unsigned char *packedData, uint16_t *agentId);
|
||||||
|
int packAgentId(unsigned char *packStore, uint16_t agentId);
|
||||||
|
|
||||||
#endif /* defined(__hifi__AgentList__) */
|
#endif /* defined(__hifi__AgentList__) */
|
||||||
|
|
Loading…
Reference in a new issue