From 3d874357d74e7bcb0a69161c167c6addb2f5a96c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 13 Feb 2013 13:03:03 -0800 Subject: [PATCH] bust TreeNode out into seperate class --- space/src/TreeNode.cpp | 19 ++++++++++++++++++ space/src/TreeNode.h | 26 ++++++++++++++++++++++++ space/src/main.cpp | 45 ++++++++++++++---------------------------- 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 space/src/TreeNode.cpp create mode 100644 space/src/TreeNode.h diff --git a/space/src/TreeNode.cpp b/space/src/TreeNode.cpp new file mode 100644 index 0000000000..7240dacebe --- /dev/null +++ b/space/src/TreeNode.cpp @@ -0,0 +1,19 @@ +// +// TreeNode.cpp +// hifi +// +// Created by Stephen Birarda on 2/13/13. +// +// + +#include "TreeNode.h" + +std::string EMPTY_STRING = ""; + +TreeNode::TreeNode() { + for (int i = 0; i < CHILDREN_PER_NODE; ++i) { + child[i] = NULL; + } + hostname = &EMPTY_STRING; + nickname = &EMPTY_STRING; +} \ No newline at end of file diff --git a/space/src/TreeNode.h b/space/src/TreeNode.h new file mode 100644 index 0000000000..d2668a2588 --- /dev/null +++ b/space/src/TreeNode.h @@ -0,0 +1,26 @@ +// +// TreeNode.h +// hifi +// +// Created by Stephen Birarda on 2/13/13. +// +// + +#ifndef __hifi__TreeNode__ +#define __hifi__TreeNode__ + +#include + +const int CHILDREN_PER_NODE = 8; + +class TreeNode { +public: + TreeNode(); + + TreeNode *child[CHILDREN_PER_NODE]; + std::string *hostname; + std::string *nickname; + int domain_id; +}; + +#endif /* defined(__hifi__TreeNode__) */ diff --git a/space/src/main.cpp b/space/src/main.cpp index 9710385263..1e71592105 100644 --- a/space/src/main.cpp +++ b/space/src/main.cpp @@ -24,36 +24,21 @@ #include #include #include +#include "TreeNode.h" -const int CHILDREN_PER_NODE = 8; const char *CONFIG_FILE = "/etc/below92/spaceserver.data.txt"; const int UDP_PORT = 55551; std::vector< std::vector > configData; sockaddr_in address, destAddress; socklen_t destLength = sizeof(destAddress); -std::string EMPTY_STRING = ""; - std::string ROOT_HOSTNAME = "root.highfidelity.co"; std::string ROOT_NICKNAME = "root"; std::string *LAST_KNOWN_HOSTNAME = new std::string(); -class treeNode { -public: - treeNode *child[CHILDREN_PER_NODE]; - std::string *hostname; - std::string *nickname; - int domain_id; - treeNode() { - for (int i = 0; i < CHILDREN_PER_NODE; ++i) { - child[i] = NULL; - } - hostname = &EMPTY_STRING; - nickname = &EMPTY_STRING; - } -}; +const short PACKET_LENGTH_BYTES = 1024; -treeNode rootNode; +TreeNode rootNode; void printBinaryValue(char element) { std::bitset<8> x(element); @@ -90,13 +75,13 @@ int network_init() return handle; } -treeNode *findOrCreateNode(unsigned long lengthInBits, +TreeNode *findOrCreateNode(unsigned long lengthInBits, unsigned char *addressBytes, std::string *hostname, std::string *nickname, int domain_id) { - treeNode *currentNode = &rootNode; + TreeNode *currentNode = &rootNode; for (int i = 0; i < lengthInBits; i += 3) { unsigned char octetA; @@ -117,7 +102,7 @@ treeNode *findOrCreateNode(unsigned long lengthInBits, } if (currentNode->child[octet] == NULL) { - currentNode->child[octet] = new treeNode; + currentNode->child[octet] = new TreeNode; } else if (!currentNode->child[octet]->hostname->empty()) { LAST_KNOWN_HOSTNAME = currentNode->child[octet]->hostname; } @@ -205,7 +190,7 @@ bool loadSpaceData(void) { int main (int argc, const char *argv[]) { int handle = network_init(); - unsigned char packet_data[BUFFER_LENGTH_SAMPLES]; + unsigned char packetData[PACKET_LENGTH_BYTES]; long received_bytes = 0; if (!handle) { @@ -223,14 +208,14 @@ int main (int argc, const char *argv[]) { std::cout << "[DEBUG] Listening for Datagrams" << std::endl; while (true) { - received_bytes = recvfrom(handle, (unsigned char*)packet_data, BUFFER_LENGTH_BYTES, 0, (sockaddr*)&dest_address, &destLength); + received_bytes = recvfrom(handle, (unsigned char*)packetData, PACKET_LENGTH_BYTES, 0, (sockaddr*)&destAddress, &destLength); if (received_bytes > 0) { unsigned long lengthInBits; - lengthInBits = packet_data[0] * 3; + lengthInBits = packetData[0] * 3; - unsigned char addressData[sizeof(packet_data)-1]; - for (int i = 0; i < sizeof(packet_data)-1; ++i) { - addressData[i] = packet_data[i+1]; + unsigned char addressData[sizeof(packetData)-1]; + for (int i = 0; i < sizeof(packetData)-1; ++i) { + addressData[i] = packetData[i+1]; } std::string thisHostname; std::string thisNickname; @@ -238,7 +223,7 @@ int main (int argc, const char *argv[]) { int domain_id = 0; long sentBytes; - treeNode thisNode = *findOrCreateNode(lengthInBits, addressData, &thisHostname, &thisNickname, domain_id); + TreeNode thisNode = *findOrCreateNode(lengthInBits, addressData, &thisHostname, &thisNickname, domain_id); if (thisNode.hostname->empty()) { hostnameHolder = *LAST_KNOWN_HOSTNAME; @@ -250,8 +235,8 @@ int main (int argc, const char *argv[]) { std::copy(hostnameHolder.begin(), hostnameHolder.end(), hostname); hostname[hostnameHolder.size()] = '\0'; - sentBytes = sendto(handle, &hostname, BUFFER_LENGTH_BYTES, - 0, (sockaddr*)&dest_address, sizeof(dest_address)); + sentBytes = sendto(handle, &hostname, PACKET_LENGTH_BYTES, + 0, (sockaddr*)&destAddress, sizeof(destAddress)); } } }