mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 18:55:01 +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) {
|
||||
*currentPosition++ = agentToAdd->getType();
|
||||
|
||||
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId());
|
||||
currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket());
|
||||
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
|
||||
|
||||
|
@ -83,7 +84,14 @@ int main(int argc, const char * argv[])
|
|||
agentType = packetData[0];
|
||||
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;
|
||||
startPointer = currentBufferPos;
|
||||
|
|
|
@ -229,9 +229,9 @@ void Timer(int extra)
|
|||
|
||||
//
|
||||
// Send a message to the domainserver telling it we are ALIVE
|
||||
//
|
||||
//
|
||||
unsigned char output[7];
|
||||
output[0] = 'I';
|
||||
output[0] = 'I';
|
||||
packSocket(output + 1, localAddress, htons(AGENT_SOCKET_LISTEN_PORT));
|
||||
|
||||
agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7);
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
Agent::Agent() {}
|
||||
|
||||
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType) {
|
||||
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId) {
|
||||
publicSocket = new sockaddr;
|
||||
memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr));
|
||||
|
||||
|
@ -27,6 +27,7 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent
|
|||
memcpy(localSocket, agentLocalSocket, sizeof(sockaddr));
|
||||
|
||||
type = agentType;
|
||||
agentId = thisAgentId;
|
||||
|
||||
firstRecvTimeUsecs = usecTimestampNow();
|
||||
lastRecvTimeUsecs = usecTimestampNow();
|
||||
|
@ -42,6 +43,8 @@ Agent::Agent(const Agent &otherAgent) {
|
|||
localSocket = new sockaddr;
|
||||
memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr));
|
||||
|
||||
agentId = otherAgent.agentId;
|
||||
|
||||
if (otherAgent.activeSocket == otherAgent.publicSocket) {
|
||||
activeSocket = publicSocket;
|
||||
} else if (otherAgent.activeSocket == otherAgent.localSocket) {
|
||||
|
@ -80,6 +83,14 @@ void Agent::setType(char newType) {
|
|||
type = newType;
|
||||
}
|
||||
|
||||
uint16_t Agent::getAgentId() {
|
||||
return agentId;
|
||||
}
|
||||
|
||||
void Agent::setAgentId(uint16_t thisAgentId) {
|
||||
agentId = thisAgentId;
|
||||
}
|
||||
|
||||
double Agent::getFirstRecvTimeUsecs() {
|
||||
return firstRecvTimeUsecs;
|
||||
}
|
||||
|
@ -144,6 +155,7 @@ void Agent::swap(Agent &first, Agent &second) {
|
|||
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) {
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
class Agent {
|
||||
public:
|
||||
Agent();
|
||||
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType);
|
||||
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId);
|
||||
Agent(const Agent &otherAgent);
|
||||
~Agent();
|
||||
Agent& operator=(Agent otherAgent);
|
||||
|
@ -30,6 +30,8 @@ class Agent {
|
|||
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();
|
||||
|
@ -49,6 +51,7 @@ class Agent {
|
|||
void swap(Agent &first, Agent &second);
|
||||
sockaddr *publicSocket, *localSocket, *activeSocket;
|
||||
char type;
|
||||
uint16_t agentId;
|
||||
double firstRecvTimeUsecs;
|
||||
double lastRecvTimeUsecs;
|
||||
AgentData *linkedData;
|
||||
|
|
|
@ -24,11 +24,14 @@ pthread_mutex_t vectorChangeMutex = PTHREAD_MUTEX_INITIALIZER;
|
|||
AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) {
|
||||
linkedDataCreateCallback = NULL;
|
||||
audioMixerSocketUpdate = NULL;
|
||||
lastAgentId = 0;
|
||||
}
|
||||
|
||||
AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) {
|
||||
linkedDataCreateCallback = NULL;
|
||||
audioMixerSocketUpdate = NULL;
|
||||
lastAgentId = 0;
|
||||
|
||||
}
|
||||
|
||||
AgentList::~AgentList() {
|
||||
|
@ -105,10 +108,19 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
uint16_t AgentList::getLastAgentId() {
|
||||
return lastAgentId;
|
||||
}
|
||||
|
||||
void AgentList::increaseAgentId() {
|
||||
++lastAgentId;
|
||||
}
|
||||
|
||||
int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
||||
int readAgents = 0;
|
||||
|
||||
char agentType;
|
||||
uint16_t agentId;
|
||||
|
||||
// assumes only IPv4 addresses
|
||||
sockaddr_in agentPublicSocket;
|
||||
|
@ -121,19 +133,20 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
|||
|
||||
while((readPtr - startPtr) < dataBytes) {
|
||||
agentType = *readPtr++;
|
||||
readPtr += unpackAgentId(readPtr, (uint16_t *)&agentId);
|
||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket);
|
||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket);
|
||||
|
||||
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType);
|
||||
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType, agentId);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for(agent = agents.begin(); agent != agents.end(); agent++) {
|
||||
for (agent = agents.begin(); agent != agents.end(); agent++) {
|
||||
if (agent->matches(publicSocket, localSocket, agentType)) {
|
||||
// we already have this agent, stop checking
|
||||
break;
|
||||
|
@ -142,7 +155,8 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
|
|||
|
||||
if (agent == agents.end()) {
|
||||
// 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)) {
|
||||
// likely debugging scenario with DS + agent on local network
|
||||
|
@ -261,4 +275,14 @@ void AgentList::startSilentAgentRemovalThread() {
|
|||
void AgentList::stopSilentAgentRemovalThread() {
|
||||
stopAgentRemovalThread = true;
|
||||
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();
|
||||
UDPSocket& getAgentSocket();
|
||||
|
||||
|
||||
int updateList(unsigned char *packetData, size_t dataBytes);
|
||||
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 updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
||||
void broadcastToAgents(char *broadcastData, size_t dataBytes);
|
||||
|
@ -49,8 +51,11 @@ class AgentList {
|
|||
UDPSocket agentSocket;
|
||||
std::vector<Agent> agents;
|
||||
pthread_t removeSilentAgentsThread;
|
||||
|
||||
uint16_t lastAgentId;
|
||||
void handlePingReply(sockaddr *agentAddress);
|
||||
};
|
||||
|
||||
int unpackAgentId(unsigned char *packedData, uint16_t *agentId);
|
||||
int packAgentId(unsigned char *packStore, uint16_t agentId);
|
||||
|
||||
#endif /* defined(__hifi__AgentList__) */
|
||||
|
|
Loading…
Reference in a new issue