From: Roland Häder Date: Fri, 23 Mar 2012 20:30:33 +0000 (+0000) Subject: Basicly finished adding verfied chunks to the final arrays (chunk data and hashes... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f72d9b4e3b1d3359a18e9b8e9c2ed609c4cfb1f4;p=hub.git Basicly finished adding verfied chunks to the final arrays (chunk data and hashes in separate arrays) --- diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index fd0d4ad7b..4de321119 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -24,6 +24,7 @@ class BaseHubSystem extends BaseFrameworkSystem { // Exception codes const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900; + const EXCEPTION_CHUNK_ALREADY_ASSEMBLED = 0x901; /** * Separator for all bootstrap node entries diff --git a/application/hub/main/handler/chunks/class_ChunkHandler.php b/application/hub/main/handler/chunks/class_ChunkHandler.php index 6d4d94918..2e5649834 100644 --- a/application/hub/main/handler/chunks/class_ChunkHandler.php +++ b/application/hub/main/handler/chunks/class_ChunkHandler.php @@ -27,6 +27,16 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable */ const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks'; + /** + * The final array for assembling the original package back together + */ + private $finalPackageChunks = array( + // Array for package content + 'content' => array(), + // ... and for the hashes + 'hashes' => array() + ); + /** * Protected constructor * @@ -102,6 +112,31 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable return $isValid; } + /** + * Adds the chunk to the final array which will be used for the final step + * which will be to assemble all chunks back to the original package content + * and for the final hash check. + * + * This method may throw an exception if a chunk with the same serial number + * has already been added to avoid mixing chunks from different packages. + * + * @param $chunkSplits An array from a splitted chunk + * @return void + */ + private function addChunkToFinalArray (array $chunkSplits) { + // Is the serial number (index 1) already been added? + if (isset($this->finalPackageChunks[$chunkSplits[1])) { + // Then throw an exception + throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED); + } // END - if + + // Add the chunk data (index 2) to the final array and use the serial number as index + $this->finalPackageChunks['content'][$chunkSplits[1]] = $chunkSplits[2]; + + // ... and the hash as well + $this->finalPackageChunks['hashes'][$chunkSplits[1]] = $chunkSplits[0]; + } + /** * Adds all chunks if the last one verifies as a 'final chunk'. * @@ -176,7 +211,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)? if (!$this->isSerialNumberValid($chunkSplits[1])) { // Do some logging - $this->debugOutput('CHUNK-HANDLER: Chunk serial number is invalid.'); + $this->debugOutput('CHUNK-HANDLER: Chunk serial number for hash ' . $chunkSplits[0] . ' is invalid.'); // Re-request this chunk $this->rerequestChunkBySplitsArray($chunkSplits); @@ -191,8 +226,10 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable * peer. * * And also the serial number is valid (basicly) at this point. + * + * Now the chunk can be added to the final array */ - die('chunk=' . $chunk . chr(10)); + $this->addChunkToFinalArray($chunkSplits); } }