From c157aad775d398f3b2be19e0dbd7063d55f27d38 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 10:44:43 -0700 Subject: [PATCH] starting to add PACKET_TYPE_VOXEL_JURISDICTION --- libraries/shared/src/PacketHeaders.h | 1 + libraries/voxels/src/JurisdictionMap.cpp | 75 ++++++++++++++++++++++++ libraries/voxels/src/JurisdictionMap.h | 3 + 3 files changed, 79 insertions(+) diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 3d49b6b066..720fb69936 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -38,6 +38,7 @@ const PACKET_TYPE PACKET_TYPE_DOMAIN_REPORT_FOR_DUTY = 'C'; const PACKET_TYPE PACKET_TYPE_REQUEST_ASSIGNMENT = 'r'; const PACKET_TYPE PACKET_TYPE_SEND_ASSIGNMENT = 's'; const PACKET_TYPE PACKET_TYPE_VOXEL_STATS = '#'; +const PACKET_TYPE PACKET_TYPE_VOXEL_JURISDICTION = 'J'; typedef char PACKET_VERSION; diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index 377329caf2..5cd44e0030 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include "JurisdictionMap.h" #include "VoxelNode.h" @@ -206,3 +208,76 @@ bool JurisdictionMap::writeToFile(const char* filename) { settings.endGroup(); return true; } + +int JurisdictionMap::packIntoMessage(unsigned char* destinationBuffer, int availableBytes) { + unsigned char* bufferStart = destinationBuffer; + + int headerLength = populateTypeAndVersion(destinationBuffer, PACKET_TYPE_VOXEL_JURISDICTION); + destinationBuffer += headerLength; + + // add the root jurisdiction + if (_rootOctalCode) { + // copy the + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(_rootOctalCode)); + memcpy(destinationBuffer, &bytes, sizeof(bytes)); + destinationBuffer += sizeof(bytes); + memcpy(destinationBuffer, _rootOctalCode, bytes); + destinationBuffer += bytes; + + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = _endNodes.size(); + + memcpy(destinationBuffer, &endNodeCount, sizeof(endNodeCount)); + destinationBuffer += sizeof(endNodeCount); + + for (int i=0; i < endNodeCount; i++) { + unsigned char* endNodeCode = _endNodes[i]; + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode)); + memcpy(destinationBuffer, &bytes, sizeof(bytes)); + destinationBuffer += sizeof(bytes); + memcpy(destinationBuffer, endNodeCode, bytes); + destinationBuffer += bytes; + } + } else { + int bytes = 0; + memcpy(destinationBuffer, &bytes, sizeof(bytes)); + destinationBuffer += sizeof(bytes); + } + + return destinationBuffer - bufferStart; // includes header! +} + +int JurisdictionMap::unpackFromMessage(unsigned char* sourceBuffer, int availableBytes) { + clear(); + unsigned char* startPosition = sourceBuffer; + + // increment to push past the packet header + int numBytesPacketHeader = numBytesForPacketHeader(sourceBuffer); + sourceBuffer += numBytesPacketHeader; + + // read the root jurisdiction + int bytes = 0; + memcpy(&bytes, sourceBuffer, sizeof(bytes)); + sourceBuffer += sizeof(bytes); + + if (bytes > 0) { + _rootOctalCode = new unsigned char[bytes]; + memcpy(_rootOctalCode, sourceBuffer, bytes); + sourceBuffer += bytes; + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = 0; + memcpy(&endNodeCount, sourceBuffer, sizeof(endNodeCount)); + sourceBuffer += sizeof(endNodeCount); + for (int i=0; i < endNodeCount; i++) { + int bytes = 0; + memcpy(&bytes, sourceBuffer, sizeof(bytes)); + sourceBuffer += sizeof(bytes); + unsigned char* endNodeCode = new unsigned char[bytes]; + memcpy(endNodeCode, sourceBuffer, bytes); + sourceBuffer += bytes; + _endNodes.push_back(endNodeCode); + } + } + + return sourceBuffer - startPosition; // includes header! +} diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index 50aabb65f2..5b0f19229a 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -49,6 +49,9 @@ public: int getEndNodeCount() const { return _endNodes.size(); } void copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn); + + int unpackFromMessage(unsigned char* sourceBuffer, int availableBytes); + int packIntoMessage(unsigned char* destinationBuffer, int availableBytes); private: void copyContents(const JurisdictionMap& other); // use assignment instead