]> git.mxchange.org Git - hub.git/commitdiff
Basicly finished adding verfied chunks to the final arrays (chunk data and hashes...
authorRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 20:30:33 +0000 (20:30 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 20:30:33 +0000 (20:30 +0000)
application/hub/main/class_BaseHubSystem.php
application/hub/main/handler/chunks/class_ChunkHandler.php

index fd0d4ad7b9298f2c73090fb61ada247ffa6e2a24..4de321119f6054d8f9ac7294566c5806af064b38 100644 (file)
@@ -24,6 +24,7 @@
 class BaseHubSystem extends BaseFrameworkSystem {
        // Exception codes
        const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
 class BaseHubSystem extends BaseFrameworkSystem {
        // Exception codes
        const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
+       const EXCEPTION_CHUNK_ALREADY_ASSEMBLED   = 0x901;
 
        /**
         * Separator for all bootstrap node entries
 
        /**
         * Separator for all bootstrap node entries
index 6d4d9491831ac5915f30942b2a662fc92c111686..2e56498347221a99c1174291005aa2ab922bf1bc 100644 (file)
@@ -27,6 +27,16 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks';
 
         */
        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
         *
        /**
         * Protected constructor
         *
@@ -102,6 +112,31 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                return $isValid;
        }
 
                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'.
         *
        /**
         * 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
                // 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);
 
                        // 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.
                 * 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);
        }
 }
 
        }
 }