]> git.mxchange.org Git - hub.git/commitdiff
Added methods, added marking of completed arrays:
authorRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 22:25:55 +0000 (22:25 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 22:25:55 +0000 (22:25 +0000)
- Added methods for assembling chunks to the original package data from a
  completed final array
- Added private method to mark the final array in ChunkHandler as completed
- Resorted method calls on the chunk handkler in HubChunkAssemblerTask
- Other minor rewrites

application/hub/interfaces/handler/chunks/class_HandleableChunks.php
application/hub/main/handler/chunks/class_ChunkHandler.php
application/hub/main/tasks/hub/chunks/class_HubChunkAssemblerTask.php

index ab9c59318313140a7bd852dd6ae2499087e70ac4..ecef5d4258981ef7d32b6bad49a0335e82c8d93d 100644 (file)
@@ -46,6 +46,28 @@ interface HandleableChunks extends Handleable {
         * @return      void
         */
        function handleAvailableChunksWithFinal ();
+
+       /**
+        * Checks whether unassembled chunks are available (ready) in final array
+        *
+        * @return      $unassembledChunksAvailable             Whether unassembled chunks are available
+        */
+       function ifUnassembledChunksAvailable ();
+
+       /**
+        * Assembles all chunks (except EOP and "hash chunk") back together to the original package data.
+        *
+        * This is done by the following steps:
+        *
+        * 1) Sort the final array with ksort(). This will bring the "hash
+        *    chunk" up to the last array index and the EOP chunk to the
+        *    pre-last array index
+        * 2) Assemble all chunks except two last (see above step)
+        * 3) While so, do the final check on all hashes
+        * 4) If the package is assembled back together, hash it again for
+        *    the very final verification.
+        */
+       function assembleChunksFromFinalArray ();
 }
 
 // [EOF]
index 43731ad106fd054fb37da0d0a27a28d5249532a5..7090c425e72d83ead644ae76ba9047fe61e10456 100644 (file)
@@ -32,9 +32,11 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private $finalPackageChunks = array(
                // Array for package content
-               'content' => array(),
+               'content'     => array(),
                // ... and for the hashes
-               'hashes'  => array()
+               'hashes'      => array(),
+               // ... marker for that the final array is complete for assembling all chunks
+               'is_complete' => false
        );
 
        /**
@@ -137,6 +139,20 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                $this->finalPackageChunks['hashes'][$chunkSplits[1]] = $chunkSplits[0];
        }
 
+       /**
+        * Marks the final array as completed, do only this if you really have all
+        * chunks together including EOP and "hash chunk".
+        *
+        * @return      void
+        */
+       private function markFinalArrayAsCompleted () {
+               /*
+                * As for now, just set the array element. If any further steps are
+                * being added, this should always be the last step.
+                */
+               $this->finalPackageChunks['is_complete'] = true;
+       }
+
        /**
         * Adds all chunks if the last one verifies as a 'final chunk'.
         *
@@ -230,6 +246,42 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                 * Now the chunk can be added to the final array
                 */
                $this->addChunkToFinalArray($chunkSplits);
+
+               // Is the stack now empty?
+               if ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP)) {
+                       // Then mark the final array as complete
+                       $this->markFinalArrayAsCompleted();
+               } // END - if
+       }
+
+       /**
+        * Checks whether unassembled chunks are available (ready) in final array
+        *
+        * @return      $unassembledChunksAvailable             Whether unassembled chunks are available
+        */
+       public function ifUnassembledChunksAvailable () {
+               // For now do only check the array element 'is_complete'
+               $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === true);
+
+               // Return status
+               return $unassembledChunksAvailable;
+       }
+
+       /**
+        * Assembles all chunks (except EOP and "hash chunk") back together to the original package data.
+        *
+        * This is done by the following steps:
+        *
+        * 1) Sort the final array with ksort(). This will bring the "hash
+        *    chunk" up to the last array index and the EOP chunk to the
+        *    pre-last array index
+        * 2) Assemble all chunks except two last (see above step)
+        * 3) While so, do the final check on all hashes
+        * 4) If the package is assembled back together, hash it again for
+        *    the very final verification.
+        */
+       public function assembleChunksFromFinalArray () {
+               $this->partialStub('Please implement this method.');
        }
 }
 
index 8c040645b34f6482eb2393686c81ba5ceed1a343..abedc43268b7de4e6c3eab6618075a37e8211a1f 100644 (file)
@@ -69,11 +69,27 @@ class HubChunkAssemblerTask extends BaseTask implements Taskable, Visitable {
         * @return      void
         */
        public function executeTask () {
-               // Are there chunks to handle?
-               if ($this->getHandlerInstance()->ifUnhandledChunksWithFinalAvailable()) {
-                       // Then handle them (not all!)
+               // Are there chunks to handle or a final array to assemble?
+               if ($this->getHandlerInstance()->ifUnassembledChunksAvailable()) {
+                       /*
+                        * Then do the final steps:
+                        *
+                        * 1) Sort the final array with ksort(). This will bring the "hash
+                        *    chunk" up to the last array index and the EOP chunk to the
+                        *    pre-last array index
+                        * 2) Assemble all chunks except two last (see above step)
+                        * 3) While so, do the final check on all hashes
+                        * 4) If the package is assembled back together, hash it again for
+                        *    the very final verification.
+                        */
+                       $this->getHandlerInstance()->assembleChunksFromFinalArray();
+               } elseif ($this->getHandlerInstance()->ifUnhandledChunksWithFinalAvailable()) {
+                       /*
+                        * Then handle them (not all!). This should push all chunks into a
+                        * 'final array' for last verification.
+                        */
                        $this->getHandlerInstance()->handleAvailableChunksWithFinal();
-               } // END - if
+               }
        }
 }