From 0cea1e80fff4f24d1dd0f7f8624bd2b232684827 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 09:30:50 -0800 Subject: [PATCH 01/11] fix memory overwrite in domain server --- domain/src/main.cpp | 2 +- interface/src/Agent.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 7a395c0f74..7323ff86e0 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -78,7 +78,7 @@ int addAgent(uint32_t ip, in_port_t port, char *private_ip, unsigned short priva agents[i].active = true; agents[i].public_sin_addr.s_addr = ip; agents[i].public_port = port; - agents[i].private_addr = private_ip; + strcpy(agents[i].private_addr, private_ip); agents[i].private_port = private_port; agents[i].agentType = agentType; gettimeofday(&agents[i].time, NULL); diff --git a/interface/src/Agent.cpp b/interface/src/Agent.cpp index c062f8d9d4..385eba623b 100644 --- a/interface/src/Agent.cpp +++ b/interface/src/Agent.cpp @@ -62,6 +62,7 @@ int update_agents(char * data, int length) { spot = packet.find_first_of (",", 0); while (spot != std::string::npos) { std::string thisAgent = packet.substr(start_spot, spot-start_spot); + std::cout << "RAW: " << thisAgent << "\n"; sscanf(thisAgent.c_str(), "%c %s %hd %s %hd", &agentType, public_address, &public_port, private_address, &private_port); add_agent(public_address, public_port, private_address, private_port, agentType); numAgents++; From 612662c475485b9e5d912bc038dab65b4bd2ca46 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 09:36:47 -0800 Subject: [PATCH 02/11] actually allocate memory for the agent private address --- domain/src/main.cpp | 7 ++++++- interface/src/main.cpp | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 7323ff86e0..eb23eac51f 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -70,7 +70,12 @@ int addAgent(uint32_t ip, in_port_t port, char *private_ip, unsigned short priva while ((ip != agents[i].ip || port != agents[i].public_port) && (i < num_agents)) { i++; } - if ((i == num_agents) || (agents[i].active == false)) is_new = 1; + + if ((i == num_agents) || (agents[i].active == false)) { + is_new = 1; + agents[i].private_addr = new char[255]; + } + agents[i].ip = ip; agents[i].x = x; agents[i].y = y; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index a42c06d6c1..51c2558ed8 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -60,7 +60,7 @@ int simulate_on = 1; const int MAX_PACKET_SIZE = 1500; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string +char DOMAIN_IP[100] = "192.168.1.47"; // IP Address will be used first if not empty string const int DOMAINSERVER_PORT = 40102; UDPSocket agentSocket(AGENT_UDP_PORT); From c3c0793ddc26a53cde20dfaa869925d26f7ab8b2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 09:39:52 -0800 Subject: [PATCH 03/11] mixer needs to send port as unsigned short --- interface/src/main.cpp | 2 +- mixer/src/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 51c2558ed8..a42c06d6c1 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -60,7 +60,7 @@ int simulate_on = 1; const int MAX_PACKET_SIZE = 1500; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = "192.168.1.47"; // IP Address will be used first if not empty string +char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string const int DOMAINSERVER_PORT = 40102; UDPSocket agentSocket(AGENT_UDP_PORT); diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index 26fca7fe78..011e27f82b 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -229,7 +229,7 @@ void *reportAliveToDS(void *args) { while (true) { gettimeofday(&lastSend, NULL); - sprintf(output, "%c %f,%f,%f,54.241.92.53 %hd", 'M', 0.f, 0.f, 0.f, MIXER_LISTEN_PORT); + sprintf(output, "%c %f,%f,%f,54.241.92.53 %hd", 'M', 0.f, 0.f, 0.f, (unsigned short)MIXER_LISTEN_PORT); int packetSize = strlen(output); audioSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, output, packetSize); From fd73b8de82572a81707f20c2bd86be81fb7c3bc9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 09:50:15 -0800 Subject: [PATCH 04/11] always store ports as unsigned short ports --- domain/src/main.cpp | 4 ++-- interface/src/Agent.h | 2 +- interface/src/main.cpp | 4 ++-- mixer/src/main.cpp | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index eb23eac51f..75eebbb9af 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -43,9 +43,9 @@ struct AgentList { char agentType; uint32_t ip; in_addr public_sin_addr; - in_port_t public_port; + unsigned short public_port; char *private_addr; - in_port_t private_port; + unsigned short private_port; float x, y, z; bool active; timeval time, connectTime; diff --git a/interface/src/Agent.h b/interface/src/Agent.h index 15a363bb12..460491fde7 100644 --- a/interface/src/Agent.h +++ b/interface/src/Agent.h @@ -19,7 +19,7 @@ #include "UDPSocket.h" #include "Audio.h" -const int AGENT_UDP_PORT = 40103; +const unsigned short AGENT_UDP_PORT = 40103; int update_agents(char * data, int length); int add_agent(char * address, unsigned short port, char *private_address, unsigned short private_port, char agentType); diff --git a/interface/src/main.cpp b/interface/src/main.cpp index a42c06d6c1..ef875a3d5a 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -60,7 +60,7 @@ int simulate_on = 1; const int MAX_PACKET_SIZE = 1500; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string +char DOMAIN_IP[100] = "192.168.1.47"; // IP Address will be used first if not empty string const int DOMAINSERVER_PORT = 40102; UDPSocket agentSocket(AGENT_UDP_PORT); @@ -231,7 +231,7 @@ void Timer(int extra) // Send a message to the domainserver telling it we are ALIVE // char output[100]; - sprintf(output, "%c %f,%f,%f,%s %hd", 'I', location[0], location[1], location[2], localAddressBuffer, (unsigned short) AGENT_UDP_PORT); + sprintf(output, "%c %f,%f,%f,%s %hd", 'I', location[0], location[1], location[2], localAddressBuffer, AGENT_UDP_PORT); std::cout << "sending " << output << " to domain server\n"; int packet_size = strlen(output); agentSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, output, packet_size); diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index 011e27f82b..5f4605fac3 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -18,7 +18,7 @@ const int MAX_AGENTS = 1000; const int LOGOFF_CHECK_INTERVAL = 1000; -const int MIXER_LISTEN_PORT = 55443; +const unsigned short MIXER_LISTEN_PORT = 55443; const int BUFFER_LENGTH_BYTES = 1024; const int BUFFER_LENGTH_SAMPLES = BUFFER_LENGTH_BYTES / sizeof(int16_t); @@ -229,7 +229,7 @@ void *reportAliveToDS(void *args) { while (true) { gettimeofday(&lastSend, NULL); - sprintf(output, "%c %f,%f,%f,54.241.92.53 %hd", 'M', 0.f, 0.f, 0.f, (unsigned short)MIXER_LISTEN_PORT); + sprintf(output, "%c %f,%f,%f,54.241.92.53 %hd", 'M', 0.f, 0.f, 0.f, MIXER_LISTEN_PORT); int packetSize = strlen(output); audioSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, output, packetSize); From 9774dd58748bb062428ff11e2d76bf09efc2e518 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 10:37:00 -0800 Subject: [PATCH 05/11] don't kill serial when polling too quickly --- interface/src/SerialInterface.cpp | 27 +++++++++++++-------------- interface/src/SerialInterface.h | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/interface/src/SerialInterface.cpp b/interface/src/SerialInterface.cpp index c53d8dad55..5e6e55fbfd 100644 --- a/interface/src/SerialInterface.cpp +++ b/interface/src/SerialInterface.cpp @@ -27,7 +27,7 @@ char serial_buffer[MAX_BUFFER]; int serial_buffer_pos = 0; const int ZERO_OFFSET = 2048; -const short NO_READ_MAXIMUM = 10; +const short NO_READ_MAXIMUM_MSECS = 3000; const short SAMPLES_TO_DISCARD = 100; void SerialInterface::pair() { @@ -166,14 +166,12 @@ void SerialInterface::readData() { int initialSamples = totalSamples; - while (read(serial_fd, &bufchar, 1) > 0) - { + while (read(serial_fd, &bufchar, 1) > 0) { //std::cout << bufchar[0]; serial_buffer[serial_buffer_pos] = bufchar[0]; serial_buffer_pos++; // Have we reached end of a line of input? - if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) - { + if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) { std::string serialLine(serial_buffer, serial_buffer_pos-1); //std::cout << serialLine << "\n"; int spot; @@ -204,26 +202,27 @@ void SerialInterface::readData() { } } - if (initialSamples == totalSamples) { - noReadCount++; - std::cout << "#" << noReadCount << " blank read from serial.\n"; + if (initialSamples == totalSamples) { + timeval now; + gettimeofday(&now, NULL); - if (noReadCount >= NO_READ_MAXIMUM) { + if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { + std::cout << "No data coming over serial. Shutting down SerialInterface.\n"; resetSerial(); } + } else { + gettimeofday(&lastGoodRead, NULL); } } void SerialInterface::resetSerial() { - std::cout << "Reached maximum blank read count. Shutting down serial.\n"; - active = false; - noReadCount = 0; totalSamples = 0; + gettimeofday(&lastGoodRead, NULL); + // Clear the measured and average channel data - for (int i = 0; i < NUM_CHANNELS; i++) - { + for (int i = 0; i < NUM_CHANNELS; i++) { lastMeasured[i] = 0; trailingAverage[i] = 0.0; } diff --git a/interface/src/SerialInterface.h b/interface/src/SerialInterface.h index 46d47b4911..c2a66ce201 100644 --- a/interface/src/SerialInterface.h +++ b/interface/src/SerialInterface.h @@ -49,8 +49,8 @@ private: float trailingAverage[NUM_CHANNELS]; int samplesAveraged; int LED; - int noReadCount; int totalSamples; + timeval lastGoodRead; }; #endif From 8c8309c1f4df17c52a110936439b353bd821e3fd Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 11:08:12 -0800 Subject: [PATCH 06/11] add some comments to the CMakeLists files --- domain/CMakeLists.txt | 3 +++ interface/CMakeLists.txt | 26 +++++++++++++++++++++----- mixer/CMakeLists.txt | 9 +++++---- shared/CMakeLists.txt | 2 ++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/domain/CMakeLists.txt b/domain/CMakeLists.txt index 59cfa685e3..4a7f77910f 100644 --- a/domain/CMakeLists.txt +++ b/domain/CMakeLists.txt @@ -2,9 +2,12 @@ cmake_minimum_required(VERSION 2.8) project(domain) +# grab the implementation / header files file(GLOB DOMAIN_SRCS src/*.cpp src/*.h) +# add an executable with the source files add_executable(domain ${DOMAIN_SRCS}) +# link the shared hifi library include(../LinkHifiShared.cmake) link_hifi_shared_library(domain) \ No newline at end of file diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 4476555028..c75f77cbec 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.8) +# setup for find modules set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/") set(GLM_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external) set(LODEPNG_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/LodePNG) @@ -8,19 +9,22 @@ set(PORTAUDIO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/portaudio) project(interface) if (APPLE) + # link in required OS X frameworks and include the right GL headers set(CMAKE_EXE_LINKER_FLAGS "-framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework CoreServices -framework Carbon") set(GL_HEADERS "#include \n#include ") else (APPLE) + # include the right GL headers for UNIX set(GL_HEADERS "#include \n#include \n#include ") endif (APPLE) +# create the InterfaceConfig.h file based on GL_HEADERS above configure_file(InterfaceConfig.h.in ${PROJECT_BINARY_DIR}/includes/InterfaceConfig.h) +# grab the implementation and header files from src dir file(GLOB INTERFACE_SRCS src/*.cpp src/*.h) -# For Apple set the icns file containing icons IF(APPLE) - # set how it shows up in the Info.plist file + # set how the icon shows up in the Info.plist file SET(MACOSX_BUNDLE_ICON_FILE interface.icns) # set where in the bundle to put the icns file SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/interface.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources) @@ -28,16 +32,20 @@ IF(APPLE) SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/interface.icns) ENDIF(APPLE) +# create the executable, make it a bundle on OS X add_executable(interface MACOSX_BUNDLE ${INTERFACE_SRCS}) +# link in the hifi shared library include(../LinkHifiShared.cmake) link_hifi_shared_library(interface) +# find required libraries find_package(OpenGL REQUIRED) find_package(GLUT REQUIRED) find_package(GLM REQUIRED) find_package(LodePNG REQUIRED) +# include headers for external libraries and InterfaceConfig. include_directories( ${PROJECT_BINARY_DIR}/includes ${OPENGL_INCLUDE_DIRS} @@ -46,12 +54,14 @@ include_directories( ${LODEPNG_INCLUDE_DIRS} ) +# link target to external libraries target_link_libraries(interface ${OPENGL_LIBRARY} ${GLUT_LIBRARY} ${LODEPNG_LIBRARY} ) +# include PortAudio as external project include(ExternalProject) set(PORTAUDIO_PROJ_DIR external/portaudio) ExternalProject_Add( @@ -63,13 +73,18 @@ ExternalProject_Add( BUILD_COMMAND make ) -ExternalProject_Get_Property(portaudio binary_dir) -ExternalProject_Get_Property(portaudio source_dir) -include_directories(${source_dir}/include) +# make PortAudio a dependency of the interface executable add_dependencies(interface portaudio) +# include the PortAudio headers +ExternalProject_Get_Property(portaudio source_dir) +include_directories(${source_dir}/include) + +# link the PortAudio library +ExternalProject_Get_Property(portaudio binary_dir) target_link_libraries(interface ${binary_dir}/lib/.libs/libportaudio.a) +# link required libraries on UNIX if (UNIX AND NOT APPLE) find_package(Threads REQUIRED) find_package(Librt REQUIRED) @@ -84,6 +99,7 @@ if (UNIX AND NOT APPLE) ) endif (UNIX AND NOT APPLE) +# install command for OS X bundle INSTALL(TARGETS interface BUNDLE DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/install COMPONENT Runtime RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/install COMPONENT Runtime diff --git a/mixer/CMakeLists.txt b/mixer/CMakeLists.txt index 00b2032aba..8c429b35e5 100644 --- a/mixer/CMakeLists.txt +++ b/mixer/CMakeLists.txt @@ -2,15 +2,16 @@ cmake_minimum_required(VERSION 2.8) project(mixer) +# grab the implemenation and header files file(GLOB MIXER_SRCS src/*.cpp src/*.h) +# add the mixer executable add_executable(mixer ${MIXER_SRCS}) +# link the shared hifi library include(../LinkHifiShared.cmake) link_hifi_shared_library(mixer) +# link the threads library find_package(Threads REQUIRED) -target_link_libraries(mixer - ${CMAKE_THREAD_LIBS_INIT} - ${HIFI_SHARED_LIBRARY} -) \ No newline at end of file +target_link_libraries(mixer ${CMAKE_THREAD_LIBS_INIT}) \ No newline at end of file diff --git a/shared/CMakeLists.txt b/shared/CMakeLists.txt index 3fb85b8495..afe855d47e 100644 --- a/shared/CMakeLists.txt +++ b/shared/CMakeLists.txt @@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 2.8) project(shared) +# grab the implemenation and header files file(GLOB HIFI_SHARED_SRCS src/*.h src/*.cpp) +# create a library and set the property so it can be referenced later add_library(HifiShared ${HIFI_SHARED_SRCS}) set(HIFI_SHARED_LIBRARY HifiShared) \ No newline at end of file From dcb0e5add25505e583c4c3a6d6fb5e4404d96e8a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 11:39:29 -0800 Subject: [PATCH 07/11] copy resources to OS X application bundle --- interface/CMakeLists.txt | 17 +++++++++++------ interface/src/main.cpp | 8 ++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index c75f77cbec..30bbfa56ec 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -23,14 +23,19 @@ configure_file(InterfaceConfig.h.in ${PROJECT_BINARY_DIR}/includes/InterfaceConf # grab the implementation and header files from src dir file(GLOB INTERFACE_SRCS src/*.cpp src/*.h) -IF(APPLE) +if (APPLE) # set how the icon shows up in the Info.plist file SET(MACOSX_BUNDLE_ICON_FILE interface.icns) - # set where in the bundle to put the icns file - SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/interface.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources) - # include the icns file in the target - SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/interface.icns) -ENDIF(APPLE) + + # grab the image and audio files + FILE(GLOB INTERFACE_RES_DIRS resources/*) + set(INTERFACE_RSRCS ${INTERFACE_RES_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/interface.icns) + + # set where in the bundle to put the resources file + SET_SOURCE_FILES_PROPERTIES(${INTERFACE_RSRCS} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) + # include the icns file and resource files in the target + SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${INTERFACE_RSRCS}) +endif (APPLE) # create the executable, make it a bundle on OS X add_executable(interface MACOSX_BUNDLE ${INTERFACE_SRCS}) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index ef875a3d5a..7986de9423 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -184,11 +184,7 @@ double elapsedTime; // Particles -// To add a new texture: -// 1. Add to the XCode project in the Resources/images group -// (ensure "Copy file" is checked -// 2. Add to the "Copy files" build phase in the project -char texture_filename[] = "./int-texture256-v4.png"; +char texture_filename[] = "images/int-texture256-v4.png"; unsigned int texture_width = 256; unsigned int texture_height = 256; @@ -311,7 +307,7 @@ void initDisplay(void) glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); - //load_png_as_texture(texture_filename); +// load_png_as_texture(texture_filename); if (fullscreen) glutFullScreen(); } From 8322c290930d803aa9fbe3be57171c34bb8a279f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 16:29:33 -0800 Subject: [PATCH 08/11] use char * in place of vectors and strings --- space/example.data.txt | 2 + space/src/TreeNode.cpp | 5 +- space/src/TreeNode.h | 4 +- space/src/main.cpp | 188 ++++++++++++++++------------------------- 4 files changed, 80 insertions(+), 119 deletions(-) create mode 100644 space/example.data.txt diff --git a/space/example.data.txt b/space/example.data.txt new file mode 100644 index 0000000000..065017c308 --- /dev/null +++ b/space/example.data.txt @@ -0,0 +1,2 @@ +0 00000100001011101110 domain4.highfidelity.co domain4 +0 00000100110100010001 domain3.highfidelity.co domain3 diff --git a/space/src/TreeNode.cpp b/space/src/TreeNode.cpp index 7240dacebe..7326b2db0d 100644 --- a/space/src/TreeNode.cpp +++ b/space/src/TreeNode.cpp @@ -14,6 +14,7 @@ TreeNode::TreeNode() { for (int i = 0; i < CHILDREN_PER_NODE; ++i) { child[i] = NULL; } - hostname = &EMPTY_STRING; - nickname = &EMPTY_STRING; + + hostname = NULL; + nickname = NULL; } \ No newline at end of file diff --git a/space/src/TreeNode.h b/space/src/TreeNode.h index d2668a2588..ad766d953f 100644 --- a/space/src/TreeNode.h +++ b/space/src/TreeNode.h @@ -18,8 +18,8 @@ public: TreeNode(); TreeNode *child[CHILDREN_PER_NODE]; - std::string *hostname; - std::string *nickname; + char *hostname; + char *nickname; int domain_id; }; diff --git a/space/src/main.cpp b/space/src/main.cpp index 90b897a0b9..a617511d40 100644 --- a/space/src/main.cpp +++ b/space/src/main.cpp @@ -7,51 +7,31 @@ // #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "TreeNode.h" #include "UDPSocket.h" -const char *CONFIG_FILE = "/etc/below92/spaceserver.data.txt"; -const int SPACE_LISTENING_PORT = 55551; -std::vector< std::vector > configData; -sockaddr_in address, destAddress; -socklen_t destLength = sizeof(destAddress); +const char *CONFIG_FILE = "/Users/birarda/code/worklist/checkouts/hifi/space/example.data.txt"; +const unsigned short SPACE_LISTENING_PORT = 55551; +const short MAX_NAME_LENGTH = 63; -std::string ROOT_HOSTNAME = "root.highfidelity.co"; -std::string ROOT_NICKNAME = "root"; -std::string *LAST_KNOWN_HOSTNAME = new std::string(); +const char ROOT_HOSTNAME[] = "root.highfidelity.co"; +const char ROOT_NICKNAME[] = "root"; const size_t PACKET_LENGTH_BYTES = 1024; +sockaddr_in destAddress; +socklen_t destLength = sizeof(destAddress); + +char *lastKnownHostname; + TreeNode rootNode; UDPSocket spaceSocket(SPACE_LISTENING_PORT); -void printBinaryValue(char element) { - std::bitset<8> x(element); - std::cout << "Printing binary value: " << x << std::endl; -} - -TreeNode *findOrCreateNode(unsigned long lengthInBits, +TreeNode *findOrCreateNode(int lengthInBits, unsigned char *addressBytes, - std::string *hostname, - std::string *nickname, - int domain_id) { + char *hostname, + char *nickname, + int domainID) { TreeNode *currentNode = &rootNode; @@ -75,14 +55,14 @@ TreeNode *findOrCreateNode(unsigned long lengthInBits, if (currentNode->child[octet] == NULL) { currentNode->child[octet] = new TreeNode; - } else if (!currentNode->child[octet]->hostname->empty()) { - LAST_KNOWN_HOSTNAME = currentNode->child[octet]->hostname; + } else if (currentNode->child[octet]->hostname != NULL) { + lastKnownHostname = currentNode->child[octet]->hostname; } currentNode = currentNode->child[octet]; } - if (currentNode->hostname->empty()) { + if (currentNode->hostname == NULL) { currentNode->hostname = hostname; currentNode->nickname = nickname; } @@ -91,73 +71,59 @@ TreeNode *findOrCreateNode(unsigned long lengthInBits, }; bool loadSpaceData(void) { - std::ifstream configFile(CONFIG_FILE); - std::string line; + FILE *configFile = std::fopen(CONFIG_FILE, "r"); + char formatString[10]; - if (configFile.is_open()) { - while (getline(configFile, line)) { - std::istringstream iss(line); - std::vector thisEntry; - copy(std::istream_iterator(iss), - std::istream_iterator(), - std::back_inserter(thisEntry)); - configData.push_back(thisEntry); - thisEntry.clear(); - } - } else { - std::cout << "Unable to load config file\n"; + if (configFile == NULL) { + std::cout << "Unable to load config file!\n"; return false; - } + } else { + char *lengthBitString = new char[8]; + int itemsRead = 0; + + while ((itemsRead = fscanf(configFile, "0 %8c", lengthBitString)) > 0) { + + // calculate the number of bits in the address and bits required for padding + unsigned long threeBitCodes = strtoul(lengthBitString, NULL, 2); + int bitsInAddress = threeBitCodes * 3; + int paddingBits = 8 - (bitsInAddress % 8); + int addressByteLength = (bitsInAddress + paddingBits) / 8; + + // create an unsigned char * to hold the padded address + unsigned char *paddedAddress = new unsigned char[addressByteLength]; + + char *fullByteBitString = new char[8]; + + for (int c = 0; c < addressByteLength; c++) { + if (c + 1 == addressByteLength && paddingBits > 0) { + // this is the last byte, and we need some padding bits + // pull as many bits as are left + int goodBits = 8 - paddingBits; + sprintf(formatString, "%%%dc", goodBits); + fscanf(configFile, formatString, fullByteBitString); + + // fill out the rest with zeros + memset(fullByteBitString + goodBits, '0', paddingBits); + } else { + // pull 8 bits (which will be one byte) from the file + fscanf(configFile, "%8c", fullByteBitString); + } + + // set the corresponding value in the unsigned char array + *(paddedAddress + c) = strtoul(fullByteBitString, NULL, 2); + } - for (std::vector< std::vector >::iterator it = configData.begin(); it != configData.end(); ++it) { - std::string *thisAddress = &(*it)[1]; - std::string *thisHostname = &(*it)[2]; - std::string *thisNickname = &(*it)[3]; - - char lengthByteString[8]; - unsigned long lengthByte; - unsigned long bitsInAddress; - std::size_t bytesForAddress; - - std::size_t lengthByteSlice = (*thisAddress).copy(lengthByteString, 8, 0); - lengthByteString[lengthByteSlice] = '\0'; - lengthByte = strtoul(lengthByteString, NULL, 2); - - bitsInAddress = lengthByte * 3; - bytesForAddress = (((bitsInAddress + 7) & ~7)); - char *addressBitStream = new char(); - std::size_t addressBitSlice = (*thisAddress).copy(addressBitStream, (*thisAddress).length(), 8); - - if (bitsInAddress != addressBitSlice) { - std::cout << "[FATAL] Mismatching byte length: " << bitsInAddress - << " and address bits: " << sizeof(addressBitStream) << std::endl; - return false; + char *nodeHostname = new char[MAX_NAME_LENGTH]; + char *nodeNickname = new char[MAX_NAME_LENGTH]; + fscanf(configFile, "%s %s\n", nodeHostname, nodeNickname); + + findOrCreateNode(bitsInAddress, paddedAddress, nodeHostname, nodeNickname, 0); } - char paddedBitString[bytesForAddress]; - strcpy(paddedBitString, addressBitStream); - for (unsigned long i = addressBitSlice; i < bytesForAddress; ++i ) { - paddedBitString[i] = '0'; - } - paddedBitString[bytesForAddress] = '\0'; + std::fclose(configFile); - std::string byteBufferHolder = *new std::string(paddedBitString); - unsigned char addressBytes[bytesForAddress / 8]; - addressBytes[bytesForAddress / 8] = '\0'; - int j = 0; - - for (unsigned long i = 0; i < bytesForAddress; i += 8) { - char *byteHolder = new char; - unsigned long thisByte; - byteBufferHolder.copy(byteHolder, 8, i); - thisByte = strtoul(byteHolder, NULL, 2); - addressBytes[j] = thisByte; - ++j; - } - - findOrCreateNode(bitsInAddress, addressBytes, thisHostname, thisNickname, 0); + return true; } - return true; } int main (int argc, const char *argv[]) { @@ -165,8 +131,11 @@ int main (int argc, const char *argv[]) { unsigned char packetData[PACKET_LENGTH_BYTES]; ssize_t receivedBytes = 0; - rootNode.hostname = &ROOT_HOSTNAME; - rootNode.nickname = &ROOT_NICKNAME; + rootNode.hostname = new char[MAX_NAME_LENGTH]; + rootNode.nickname = new char[MAX_NAME_LENGTH]; + + strcpy(rootNode.hostname, ROOT_HOSTNAME); + strcpy(rootNode.nickname, ROOT_NICKNAME); loadSpaceData(); @@ -181,24 +150,13 @@ int main (int argc, const char *argv[]) { for (int i = 0; i < sizeof(packetData)-1; ++i) { addressData[i] = packetData[i+1]; } - std::string thisHostname; - std::string thisNickname; - std::string hostnameHolder; - int domain_id = 0; - TreeNode thisNode = *findOrCreateNode(lengthInBits, addressData, &thisHostname, &thisNickname, domain_id); + TreeNode *thisNode = findOrCreateNode(lengthInBits, addressData, NULL, NULL, 0); + char *hostnameToSend = (thisNode->hostname == NULL) + ? lastKnownHostname + : thisNode->hostname; - if (thisNode.hostname->empty()) { - hostnameHolder = *LAST_KNOWN_HOSTNAME; - } else { - hostnameHolder = *thisNode.hostname; - } - - char hostname[hostnameHolder.size() + 1]; - std::copy(hostnameHolder.begin(), hostnameHolder.end(), hostname); - hostname[hostnameHolder.size()] = '\0'; - - spaceSocket.send(&destAddress, &hostname, PACKET_LENGTH_BYTES); + spaceSocket.send(&destAddress, &hostnameToSend, sizeof(hostnameToSend)); } } } From e6a0f5cd6ceb0e1733a5d42b214c024edcb25a87 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 16:32:39 -0800 Subject: [PATCH 09/11] simplify bitwise operations to single statements --- space/src/main.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/space/src/main.cpp b/space/src/main.cpp index a617511d40..9a6033bdac 100644 --- a/space/src/main.cpp +++ b/space/src/main.cpp @@ -36,21 +36,12 @@ TreeNode *findOrCreateNode(int lengthInBits, TreeNode *currentNode = &rootNode; for (int i = 0; i < lengthInBits; i += 3) { - unsigned char octetA; - unsigned char octetB; unsigned char octet; - /* - * @TODO Put those shifts into a nice single statement, leaving as is for now - */ if (i%8 < 6) { - octetA = addressBytes[i/8] << i%8; - octet = octetA >> (5); + octet = addressBytes[i/8] << i%8 >> (5); } else { - octetA = addressBytes[i/8] << i; - octetA = octetA >> (11 - i); - octetB = addressBytes[i/8 + 1] >> (11 - i + 2); - octet = octetA | octetB; + octet = (addressBytes[i/8] << i >> (11 - i)) | (addressBytes[i/8 + 1] >> (11 - i + 2)); } if (currentNode->child[octet] == NULL) { From 307fa9e5fab053e88bc10e57e7bfe7d74a6a0c42 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 16:33:31 -0800 Subject: [PATCH 10/11] point to EC2 domain server instead of local machine --- interface/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 7986de9423..f799926aad 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -60,7 +60,7 @@ int simulate_on = 1; const int MAX_PACKET_SIZE = 1500; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; -char DOMAIN_IP[100] = "192.168.1.47"; // IP Address will be used first if not empty string +char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string const int DOMAINSERVER_PORT = 40102; UDPSocket agentSocket(AGENT_UDP_PORT); From 741654e13d178993767bec79e91641d8ef76cbd4 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 16:35:45 -0800 Subject: [PATCH 11/11] don't print RAW packet to stdout --- interface/src/Agent.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interface/src/Agent.cpp b/interface/src/Agent.cpp index 385eba623b..c062f8d9d4 100644 --- a/interface/src/Agent.cpp +++ b/interface/src/Agent.cpp @@ -62,7 +62,6 @@ int update_agents(char * data, int length) { spot = packet.find_first_of (",", 0); while (spot != std::string::npos) { std::string thisAgent = packet.substr(start_spot, spot-start_spot); - std::cout << "RAW: " << thisAgent << "\n"; sscanf(thisAgent.c_str(), "%c %s %hd %s %hd", &agentType, public_address, &public_port, private_address, &private_port); add_agent(public_address, public_port, private_address, private_port, agentType); numAgents++;