From 8322c290930d803aa9fbe3be57171c34bb8a279f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 16:29:33 -0800 Subject: [PATCH] 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)); } } }