added reading/writing to jurisdiction files

This commit is contained in:
ZappoMan 2013-08-01 22:05:13 -07:00
parent 983db5f70d
commit 81a363f053
3 changed files with 96 additions and 13 deletions
libraries/voxels/src
voxel-server/src

View file

@ -7,6 +7,8 @@
//
#include <QSettings>
#include <QString>
#include <QStringList>
#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<unsigned char*>& endNodes) {
JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& 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<QString, QString> 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;
}

View file

@ -10,6 +10,7 @@
#define __hifi__JurisdictionMap__
#include <vector>
#include <QString>
class VoxelNode; // forward declaration
@ -20,15 +21,16 @@ public:
JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& 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<unsigned char*>& endNodes);
bool writeToFile(const char* filename);
bool readFromFile(const char* filename);
unsigned char* hexStringToOctalCode(const QString& input);
unsigned char* _rootOctalCode;

View file

@ -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;
}