From 507b6e006449f1ccf35793ee0ef894beef3bbcac Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 25 Mar 2013 16:24:14 -0700 Subject: [PATCH] use abstracted DS check in method from AgentList --- domain/src/main.cpp | 11 ++++++- interface/src/main.cpp | 52 +++--------------------------- mixer/src/main.cpp | 52 ++---------------------------- voxel/src/main.cpp | 73 ++---------------------------------------- 4 files changed, 19 insertions(+), 169 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 60164ded9c..03229b57e1 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -45,7 +45,7 @@ const int LOGOFF_CHECK_INTERVAL = 5000; #define DEBUG_TO_SELF 0 int lastActiveCount = 0; -AgentList agentList(DOMAIN_LISTEN_PORT); +AgentList agentList('D', DOMAIN_LISTEN_PORT); unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { *currentPosition++ = agentToAdd->getType(); @@ -74,6 +74,8 @@ int main(int argc, const char * argv[]) sockaddr_in agentPublicAddress, agentLocalAddress; agentLocalAddress.sin_family = AF_INET; + in_addr_t serverLocalAddress = getLocalAddress(); + agentList.startSilentAgentRemovalThread(); while (true) { @@ -83,6 +85,13 @@ int main(int argc, const char * argv[]) agentType = packetData[0]; unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress); + // check the agent public address + // if it matches our local address we're on the same box + // so hardcode the EC2 public address for now + if (agentPublicAddress.sin_addr.s_addr == serverLocalAddress) { + agentPublicAddress.sin_addr.s_addr = 895283510; + } + agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType); currentBufferPos = broadcastPacket + 1; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 3df7672a2f..d4bd0e3f6f 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -46,7 +46,7 @@ #include "Particle.h" #include "Texture.h" #include "Cloud.h" -#include "AgentList.h" +#include #include "VoxelSystem.h" #include "Lattice.h" #include "Finger.h" @@ -64,11 +64,7 @@ int simulate_on = 1; // Network Socket and network constants // -char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string -const int DOMAINSERVER_PORT = 40102; - -AgentList agentList; +AgentList agentList('I'); pthread_t networkReceiveThread; bool stopNetworkReceiveThread = false; @@ -89,14 +85,11 @@ int WIDTH = 1200; int HEIGHT = 800; int fullscreen = 0; -in_addr_t localAddress; - Oscilloscope audioScope(256,200,true); #define HAND_RADIUS 0.25 // Radius of in-world 'hand' of you Head myHead; // The rendered head of oneself - glm::vec3 box(WORLD_SIZE,WORLD_SIZE,WORLD_SIZE); ParticleSystem balls(0, box, @@ -227,15 +220,6 @@ void Timer(int extra) glutTimerFunc(1000,Timer,0); gettimeofday(&timer_start, NULL); - // - // Send a message to the domainserver telling it we are ALIVE - // - unsigned char output[7]; - output[0] = 'I'; - packSocket(output + 1, localAddress, htons(AGENT_SOCKET_LISTEN_PORT)); - - agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); - // Ping the agents we can see agentList.pingAgents(); @@ -948,43 +932,17 @@ void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { int main(int argc, char** argv) { -#ifndef _WIN32 - struct ifaddrs * ifAddrStruct=NULL; - struct ifaddrs * ifa=NULL; - - getifaddrs(&ifAddrStruct); - - for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4 - // is a valid IP4 Address - localAddress = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; - } - } -#endif - - // Lookup the IP address of things we have hostnames - if (atoi(DOMAIN_IP) == 0) { - struct hostent* pHostInfo; - if ((pHostInfo = gethostbyname(DOMAIN_HOSTNAME)) != NULL) { - sockaddr_in tempAddress; - memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); - strcpy(DOMAIN_IP, inet_ntoa(tempAddress.sin_addr)); - printf("Domain server %s: %s\n", DOMAIN_HOSTNAME, DOMAIN_IP); - - } else { - printf("Failed lookup domainserver\n"); - } - } else printf("Using static domainserver IP: %s\n", DOMAIN_IP); - // the callback for our instance of AgentList is attachNewHeadToAgent agentList.linkedDataCreateCallback = &attachNewHeadToAgent; + #ifndef _WIN32 agentList.audioMixerSocketUpdate = &audioMixerUpdate; #endif // start the thread which checks for silent agents agentList.startSilentAgentRemovalThread(); - + agentList.startDomainServerCheckInThread(); + #ifdef _WIN32 WSADATA WsaData; int wsaresult = WSAStartup( MAKEWORD(2,2), &WsaData ); diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index f9936d71be..b1e159bddf 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -53,11 +53,7 @@ const int AGENT_LOOPBACK_MODIFIER = 307; const int LOOPBACK_SANITY_CHECK = 0; -char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup -const int DOMAINSERVER_PORT = 40102; - -AgentList agentList(MIXER_LISTEN_PORT); +AgentList agentList('M', MIXER_LISTEN_PORT); StDev stdev; void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) { @@ -235,29 +231,6 @@ void *sendBuffer(void *args) pthread_exit(0); } -void *reportAliveToDS(void *args) { - - timeval lastSend; - unsigned char output[7]; - - while (true) { - gettimeofday(&lastSend, NULL); - - *output = 'M'; - packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); -// packSocket(output + 1, 788637888, htons(MIXER_LISTEN_PORT)); - agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); - - double usecToSleep = 1000000 - (usecTimestampNow() - usecTimestamp(&lastSend)); - - if (usecToSleep > 0) { - usleep(usecToSleep); - } else { - std::cout << "No sleep required!"; - } - } -} - void attachNewBufferToAgent(Agent *newAgent) { if (newAgent->getLinkedData() == NULL) { newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL)); @@ -273,26 +246,7 @@ int main(int argc, const char * argv[]) agentList.linkedDataCreateCallback = attachNewBufferToAgent; agentList.startSilentAgentRemovalThread(); - - // Lookup the IP address of things we have hostnames - if (atoi(DOMAIN_IP) == 0) { - struct hostent* pHostInfo; - if ((pHostInfo = gethostbyname(DOMAIN_HOSTNAME)) != NULL) { - sockaddr_in tempAddress; - memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); - strcpy(DOMAIN_IP, inet_ntoa(tempAddress.sin_addr)); - printf("Domain server %s: %s\n", DOMAIN_HOSTNAME, DOMAIN_IP); - - } else { - printf("Failed lookup domainserver\n"); - } - } else { - printf("Using static domainserver IP: %s\n", DOMAIN_IP); - } - - // setup the agentSocket to report to domain server - pthread_t reportAliveThread; - pthread_create(&reportAliveThread, NULL, reportAliveToDS, NULL); + agentList.startDomainServerCheckInThread(); unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE]; @@ -342,8 +296,6 @@ int main(int argc, const char * argv[]) } } - agentList.stopSilentAgentRemovalThread(); - pthread_join(reportAliveThread, NULL); pthread_join(sendBufferThread, NULL); return 0; diff --git a/voxel/src/main.cpp b/voxel/src/main.cpp index ab1e2aacdf..8fe9154ab0 100644 --- a/voxel/src/main.cpp +++ b/voxel/src/main.cpp @@ -39,41 +39,9 @@ const int MIN_BRIGHTNESS = 64; const float DEATH_STAR_RADIUS = 4.0; const float MAX_CUBE = 0.05f; -char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup -const int DOMAINSERVER_PORT = 40102; - const int MAX_VOXEL_TREE_DEPTH_LEVELS = 10; -AgentList agentList(VOXEL_LISTEN_PORT); -in_addr_t localAddress; - -void *reportAliveToDS(void *args) { - - timeval lastSend; - unsigned char output[7]; - - while (true) { - gettimeofday(&lastSend, NULL); - - *output = 'V'; - packSocket(output + 1, 895283510, htons(VOXEL_LISTEN_PORT)); -// packSocket(output + 1, 788637888, htons(VOXEL_LISTEN_PORT)); - agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); - - double usecToSleep = 1000000 - (usecTimestampNow() - usecTimestamp(&lastSend)); - - if (usecToSleep > 0) { - #ifdef _WIN32 - Sleep( static_cast(1000.0f*usecToSleep) ); - #else - usleep(usecToSleep); - #endif - } else { - std::cout << "No sleep required!"; - } - } -} +AgentList agentList('V', VOXEL_LISTEN_PORT); int randomlyFillVoxelTree(int levelsToGo, VoxelNode *currentRootNode) { // randomly generate children for this node @@ -142,43 +110,9 @@ int main(int argc, const char * argv[]) { setvbuf(stdout, NULL, _IOLBF, 0); -#ifndef _WIN32 - // get the local address of the voxel server - struct ifaddrs * ifAddrStruct=NULL; - struct ifaddrs * ifa=NULL; - - getifaddrs(&ifAddrStruct); - - for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4 - // is a valid IP4 Address - localAddress = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; - } - } -#endif - - // Lookup the IP address of things we have hostnames - if (atoi(DOMAIN_IP) == 0) { - struct hostent* pHostInfo; - if ((pHostInfo = gethostbyname(DOMAIN_HOSTNAME)) != NULL) { - sockaddr_in tempAddress; - memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); - strcpy(DOMAIN_IP, inet_ntoa(tempAddress.sin_addr)); - printf("Domain server %s: %s\n", DOMAIN_HOSTNAME, DOMAIN_IP); - - } else { - printf("Failed lookup domainserver\n"); - } - } else { - printf("Using static domainserver IP: %s\n", DOMAIN_IP); - } - - // setup the agentSocket to report to domain server - pthread_t reportAliveThread; - pthread_create(&reportAliveThread, NULL, reportAliveToDS, NULL); - agentList.linkedDataCreateCallback = &attachVoxelAgentDataToAgent; agentList.startSilentAgentRemovalThread(); + agentList.startDomainServerCheckInThread(); srand((unsigned)time(0)); @@ -245,8 +179,5 @@ int main(int argc, const char * argv[]) } } - pthread_join(reportAliveThread, NULL); - agentList.stopSilentAgentRemovalThread(); - return 0; } \ No newline at end of file