From 81a363f053de0335c1b0bbcb5260fee6aeba28fb Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 1 Aug 2013 22:05:13 -0700 Subject: [PATCH] added reading/writing to jurisdiction files --- libraries/voxels/src/JurisdictionMap.cpp | 80 +++++++++++++++++++++--- libraries/voxels/src/JurisdictionMap.h | 8 ++- voxel-server/src/main.cpp | 21 ++++++- 3 files changed, 96 insertions(+), 13 deletions(-) diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index 1a2b57965b..b3ea5f21f5 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -7,6 +7,8 @@ // #include +#include +#include #include "JurisdictionMap.h" #include "VoxelNode.h" @@ -16,16 +18,20 @@ JurisdictionMap::~JurisdictionMap() { } void JurisdictionMap::clear() { - delete[] _rootOctalCode; - _rootOctalCode = NULL; + if (_rootOctalCode) { + delete[] _rootOctalCode; + _rootOctalCode = NULL; + } for (int i = 0; i < _endNodes.size(); i++) { - delete[] _endNodes[i]; + if (_endNodes[i]) { + delete[] _endNodes[i]; + } } _endNodes.clear(); } -JurisdictionMap::JurisdictionMap() { +JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { unsigned char* rootCode = new unsigned char[1]; *rootCode = 0; @@ -33,13 +39,14 @@ JurisdictionMap::JurisdictionMap() { init(rootCode, emptyEndNodes); } -JurisdictionMap::JurisdictionMap(const char* filename) { +JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); } -JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) { +JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) + : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); } @@ -55,10 +62,65 @@ bool JurisdictionMap::isMyJurisdiction(VoxelNode* node, int childIndex) const { bool JurisdictionMap::readFromFile(const char* filename) { - QSettings settings(QString(filename), QSettings::IniFormat); - QString rootCode = settings->value("root","").toString(); - qDebug("rootCode=%s\n",rootCode); + QString settingsFile(filename); + QSettings settings(settingsFile, QSettings::IniFormat); + QString rootCode = settings.value("root","").toString(); + qDebug() << "rootCode=" << rootCode << "\n"; + + unsigned char* rootOctCode = hexStringToOctalCode(rootCode); + printOctalCode(rootOctCode); + + settings.beginGroup("endNodes"); + const QStringList childKeys = settings.childKeys(); + QHash values; + foreach (const QString &childKey, childKeys) { + QString childValue = settings.value(childKey).toString(); + values.insert(childKey, childValue); + qDebug() << childKey << "=" << childValue << "\n"; + + unsigned char* octcode = hexStringToOctalCode(childValue); + printOctalCode(octcode); + + _endNodes.push_back(octcode); + } + settings.endGroup(); + return true; } bool JurisdictionMap::writeToFile(const char* filename) { + QString settingsFile(filename); + QSettings settings(settingsFile, QSettings::IniFormat); + + settings.setValue("root", "rootNodeValue"); + + settings.beginGroup("endNodes"); + for (int i = 0; i < _endNodes.size(); i++) { + QString key = QString("endnode%1").arg(i); + QString value = QString("valuenode%1").arg(i); + settings.setValue(key, value); + } + settings.endGroup(); + return true; } + + +unsigned char* JurisdictionMap::hexStringToOctalCode(const QString& input) { + // i variable used to hold position in string + int i = 0; + // x variable used to hold byte array element position + int x = 0; + // allocate byte array based on half of string length + unsigned char* bytes = new unsigned char[(input.length()) / 2]; + // loop through the string - 2 bytes at a time converting + // it to decimal equivalent and store in byte array + while (input.length() > i + 1) { + + bool ok; + uint value = input.mid(i, 2).toUInt(&ok,16); + bytes[x] = (unsigned char)value; + i += 2; + x += 1; + } + // return the finished byte array of decimal values + return bytes; +} \ No newline at end of file diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index 9c9ed9ec0a..dd0160e7a1 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -10,6 +10,7 @@ #define __hifi__JurisdictionMap__ #include +#include class VoxelNode; // forward declaration @@ -20,15 +21,16 @@ public: JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes); ~JurisdictionMap(); - bool isMyJurisdiction(VoxelNode* node, int childIndex) const; + + bool writeToFile(const char* filename); + bool readFromFile(const char* filename); private: void clear(); void init(unsigned char* rootOctalCode, const std::vector& endNodes); - bool writeToFile(const char* filename); - bool readFromFile(const char* filename); + unsigned char* hexStringToOctalCode(const QString& input); unsigned char* _rootOctalCode; diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 75eac7353b..ef0a785fc8 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -430,6 +430,7 @@ void attachVoxelNodeDataToNode(Node* newNode) { } int receivedPacketCount = 0; +JurisdictionMap* jurisdiction = NULL; int main(int argc, const char * argv[]) { pthread_mutex_init(&::treeLock, NULL); @@ -447,7 +448,21 @@ int main(int argc, const char * argv[]) { } printf("portParameter=%s listenPort=%d\n", portParameter, listenPort); } - + + const char* JURISDICTION_FILE = "--jurisdictionFile"; + const char* jurisdictionFile = getCmdOption(argc, argv, JURISDICTION_FILE); + if (jurisdictionFile) { + printf("jurisdictionFile=%s\n", jurisdictionFile); + + printf("about to readFromFile().... jurisdictionFile=%s\n", jurisdictionFile); + jurisdiction = new JurisdictionMap(jurisdictionFile); + printf("after readFromFile().... jurisdictionFile=%s\n", jurisdictionFile); + + // test writing the file... + //printf("about to writeToFile().... jurisdictionFile=%s\n", jurisdictionFile); + //jurisdiction->writeToFile(jurisdictionFile); + //printf("after writeToFile().... jurisdictionFile=%s\n", jurisdictionFile); + } NodeList* nodeList = NodeList::createInstance(NODE_TYPE_VOXEL_SERVER, listenPort); setvbuf(stdout, NULL, _IOLBF, 0); @@ -733,6 +748,10 @@ int main(int argc, const char * argv[]) { pthread_join(sendVoxelThread, NULL); pthread_mutex_destroy(&::treeLock); + + if (jurisdiction) { + delete jurisdiction; + } return 0; }