From 8d5a53b2c9cc53aafc93d41f7be7cf3ec3cffc98 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Tue, 12 Feb 2013 20:48:09 -0800 Subject: [PATCH] Added agent type to domain server, interface client --- domain/src/main.cpp | 22 +++++++++++++++------- interface/src/Agent.cpp | 34 +++++++++++++--------------------- interface/src/Agent.h | 2 +- interface/src/Head.h | 4 ++-- interface/src/main.cpp | 12 +++++++----- interface/src/octal.cpp | 21 --------------------- 6 files changed, 38 insertions(+), 57 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 2e0a41a278..f514ca25e5 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -5,6 +5,8 @@ // Created by Philip Rosedale on 11/20/12. // Copyright (c) 2012 High Fidelity, Inc. All rights reserved. // +// The Domain Server +// #include #include @@ -33,12 +35,13 @@ const int MAX_AGENTS = 1000; const int LOGOFF_CHECK_INTERVAL = 2000; struct AgentList { + char agentType; uint32_t ip; in_addr sin_addr; in_port_t port; float x, y, z; bool active; - timeval time; + timeval time, connectTime; } agents[MAX_AGENTS]; int num_agents = 0; @@ -89,7 +92,7 @@ int network_init() return handle; } -int addAgent(uint32_t ip, in_port_t port, float x, float y, float z) { +int addAgent(uint32_t ip, in_port_t port, char agentType, float x, float y, float z) { // Search for agent in list and add if needed int i = 0; int is_new = 0; @@ -104,7 +107,9 @@ int addAgent(uint32_t ip, in_port_t port, float x, float y, float z) { agents[i].active = true; agents[i].sin_addr.s_addr = ip; agents[i].port = port; + agents[i].agentType = agentType; gettimeofday(&agents[i].time, NULL); + if (is_new) gettimeofday(&agents[i].connectTime, NULL); if (i == num_agents) { num_agents++; } @@ -117,7 +122,7 @@ void update_agent_list(timeval now) { for (i = 0; i < num_agents; i++) { if ((diffclock(agents[i].time, now) > LOGOFF_CHECK_INTERVAL) && agents[i].active) { - std::cout << "Expired Agent from " << + std::cout << "Expired Agent type " << agents[i].agentType << " from " << inet_ntoa(agents[i].sin_addr) << ":" << agents[i].port << "\n"; agents[i].active = false; } @@ -135,12 +140,14 @@ void send_agent_list(int handle, sockaddr_in * dest_address) { //std::cout << "send list to: " << inet_ntoa(dest_address->sin_addr) << "\n"; for (i = 0; i < num_agents; i++) { if (agents[i].active) { + // Write the type of the agent + buffer[length++] = agents[i].agentType; // Write agent's IP address address = inet_ntoa(agents[i].sin_addr); memcpy(&buffer[length], address, strlen(address)); length += strlen(address); // Add port number - buffer[length++] = ':'; + buffer[length++] = ' '; sprintf(portstring, "%d\n", agents[i].port); memcpy(&buffer[length], portstring, strlen(portstring)); length += strlen(portstring); @@ -185,9 +192,10 @@ int main(int argc, const char * argv[]) //std::cout << "Packet from: " << inet_ntoa(dest_address.sin_addr) //<< " " << packet_data << "\n"; float x,y,z; - sscanf(packet_data, "%f,%f,%f", &x, &y, &z); - if (addAgent(dest_address.sin_addr.s_addr, ntohs(dest_address.sin_port), x, y, z)) { - std::cout << "Added Agent from " << + char agentType; + sscanf(packet_data, "%c %f,%f,%f", &agentType, &x, &y, &z); + if (addAgent(dest_address.sin_addr.s_addr, ntohs(dest_address.sin_port), agentType, x, y, z)) { + std::cout << "Added Agent, type " << agentType << " from " << inet_ntoa(dest_address.sin_addr) << ":" << ntohs(dest_address.sin_port) << "\n"; } // Reply with packet listing nearby active agents diff --git a/interface/src/Agent.cpp b/interface/src/Agent.cpp index ea358c1fbb..b36edf4bad 100644 --- a/interface/src/Agent.cpp +++ b/interface/src/Agent.cpp @@ -20,6 +20,7 @@ struct AgentList { unsigned short port; timeval pingStarted; int pingMsecs; + char agentType; Head head; } agents[MAX_AGENTS]; @@ -35,18 +36,16 @@ int update_agents(char * data, int length) { size_t spot; size_t start_spot = 0; std::string address, port; + char agentType; unsigned short nPort = 0; + unsigned int iPort = 0; spot = packet.find_first_of (",", 0); while (spot != std::string::npos) { std::string thisAgent = packet.substr(start_spot, spot-start_spot); - if (thisAgent.find_first_of(":", 0) != std::string::npos) { - address = thisAgent.substr(0, thisAgent.find_first_of(":", 0)); - port = thisAgent.substr(thisAgent.find_first_of(":", 0) + 1, - thisAgent.length() - thisAgent.find_first_of(":", 0)); - nPort = atoi(port.c_str()); - } - //std::cout << "IP: " << address << ", port: " << nPort << "\n"; - add_agent((char *)address.c_str(), nPort); + //std::cout << "raw string: " << thisAgent << "\n"; + sscanf(thisAgent.c_str(), "%c %s %u", &agentType, address.c_str(), &iPort); + nPort = (unsigned short) iPort; + add_agent((char *)address.c_str(), nPort, agentType); numAgents++; start_spot = spot + 1; if (start_spot < packet.length()) @@ -83,7 +82,7 @@ void update_agent(char * address, unsigned short port, char * data, int length) // // Look for an agent by it's IP number, add if it does not exist in local list // -int add_agent(char * address, unsigned short port) { +int add_agent(char * address, unsigned short port, char agentType) { //std::cout << "Checking for " << IP->c_str() << " "; for (int i = 0; i < num_agents; i++) { if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) { @@ -94,6 +93,7 @@ int add_agent(char * address, unsigned short port) { if (num_agents < MAX_AGENTS) { strcpy(agents[num_agents].address, address); agents[num_agents].port = port; + agents[num_agents].agentType = agentType; std::cout << "Added Agent # " << num_agents << " with Address " << agents[num_agents].address << ":" << agents[num_agents].port << "\n"; num_agents++; @@ -110,17 +110,9 @@ int add_agent(char * address, unsigned short port) { int broadcastToAgents(UDPSocket *handle, char * data, int length) { int sent_bytes; //printf("broadcasting to %d agents\n", num_agents); - for (int i = 0; i < num_agents; i++) { - //printf("bcast to %s\n", agents[i].address); - // - // STUPID HACK: For some reason on OSX with NAT translation packets sent to localhost are - // received as from the NAT translated port but have to be sent to the local port number. - // - //if (1) //(strcmp("192.168.1.53",agents[i].address) == 0) - // sent_bytes = handle->send(agents[i].address, 40103, data, length); - //else + for (int i = 0; i < num_agents; i++) { + //std::cout << "to: Agent address " << agents[i].address << " port " << agents[i].port << "\n"; sent_bytes = handle->send(agents[i].address, agents[i].port, data, length); - if (sent_bytes != length) { std::cout << "Broadcast packet fail!\n"; return 0; @@ -135,7 +127,7 @@ void pingAgents(UDPSocket *handle) { for (int i = 0; i < num_agents; i++) { gettimeofday(&agents[i].pingStarted, NULL); handle->send(agents[i].address, agents[i].port, payload, 1); - printf("\nSent Ping at %d usecs\n", agents[i].pingStarted.tv_usec); + //printf("\nSent Ping at %d usecs\n", agents[i].pingStarted.tv_usec); } } @@ -146,7 +138,7 @@ void setAgentPing(char * address, unsigned short port) { timeval pingReceived; gettimeofday(&pingReceived, NULL); float pingMsecs = diffclock(&agents[i].pingStarted, &pingReceived); - printf("Received ping at %d usecs, Agent ping = %3.1f\n", pingReceived.tv_usec, pingMsecs); + //printf("Received ping at %d usecs, Agent ping = %3.1f\n", pingReceived.tv_usec, pingMsecs); agents[i].pingMsecs = pingMsecs; } } diff --git a/interface/src/Agent.h b/interface/src/Agent.h index e3b0585de1..305864aa43 100644 --- a/interface/src/Agent.h +++ b/interface/src/Agent.h @@ -20,7 +20,7 @@ int update_agents(char * data, int length); -int add_agent(char * address, unsigned short port); +int add_agent(char * address, unsigned short port, char agentType); int broadcastToAgents(UDPSocket * handle, char * data, int length); void pingAgents(UDPSocket *handle); void setAgentPing(char * address, unsigned short port); diff --git a/interface/src/Head.h b/interface/src/Head.h index a2a57381a7..558fb4f6db 100644 --- a/interface/src/Head.h +++ b/interface/src/Head.h @@ -88,8 +88,8 @@ public: float getLoudness() {return loudness;}; float getAverageLoudness() {return averageLoudness;}; - float setAverageLoudness(float al) {averageLoudness = al;}; - float setLoudness(float l) {loudness = l;}; + void setAverageLoudness(float al) {averageLoudness = al;}; + void setLoudness(float l) {loudness = l;}; void SetNewHeadTarget(float, float); glm::vec3 getPos() { return position; }; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index b1de646265..07528e785a 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -56,7 +56,7 @@ int simulate_on = 1; const int MAX_PACKET_SIZE = 1500; const int AGENT_UDP_PORT = 40103; -char DOMAINSERVER_IP[] = "192.168.1.53"; +char DOMAINSERVER_IP[] = "127.0.0.1"; const int DOMAINSERVER_PORT = 40102; UDPSocket agentSocket(AGENT_UDP_PORT); @@ -224,9 +224,11 @@ void Timer(int extra) glutTimerFunc(1000,Timer,0); gettimeofday(&timer_start, NULL); + // // Send a message to the domainserver telling it we are ALIVE + // char output[100]; - sprintf(output, "%f,%f,%f", location[0], location[1], location[2]); + sprintf(output, "%c %f,%f,%f", 'I', location[0], location[1], location[2]); int packet_size = strlen(output); agentSocket.send(DOMAINSERVER_IP, DOMAINSERVER_PORT, output, packet_size); @@ -239,9 +241,9 @@ void Timer(int extra) gettimeofday(&starttest, NULL); char junk[1000]; junk[0] = 'J'; - for (int i = 0; i < 3000; i++) + for (int i = 0; i < 10000; i++) { - agentSocket.send("192.168.1.38", AGENT_UDP_PORT, junk, 1000); + agentSocket.send((char *)"192.168.1.38", AGENT_UDP_PORT, junk, 1000); } gettimeofday(&endtest, NULL); float sendTime = diffclock(&starttest, &endtest); @@ -801,7 +803,7 @@ void read_network() // // Got Ping, reply immediately! // - printf("Replying to ping!\n"); + //printf("Replying to ping!\n"); char reply[] = "R"; agentSocket.send(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), reply, 1); } else if (incoming_packet[0] == 'R') { diff --git a/interface/src/octal.cpp b/interface/src/octal.cpp index e2a176747a..4e5f1eb7c5 100644 --- a/interface/src/octal.cpp +++ b/interface/src/octal.cpp @@ -27,24 +27,3 @@ void getVoxel(float * pos, int scale, float * vpos) { vpos[Z] = floor(pos[Z]*vscale)/vscale; } -// -// Given a pointer to an octal code and some domain owner data, iterate the tree and add the node(s) as needed. -// -domainNode* createNode(int lengthInBits, char * octalData, - char * hostname, char * nickname, int domain_id) { - domainNode * currentNode = &rootNode; - for (int i = 0; i < lengthInBits; i+=3) { - char octet = 0; - if (i%8 < 6) octet = octalData[i/8] << (i%8); - else { - octet = octalData[i/8] << (i%8) && ((octalData[i/8 + 1] >> (i%8)) << (8 - i%8)); - } - if (currentNode->child[octet] == NULL) currentNode->child[octet] = new domainNode; - currentNode = currentNode->child[octet]; - } - // Copy in the new node info... - strcpy(currentNode->hostname, hostname); - strcpy(currentNode->nickname, nickname); - currentNode->domain_id = domain_id; - return currentNode; -}