From fc45bd6bbfedaa7beb1a12da805377ef7711a984 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 23 Mar 2012 22:25:55 +0000 Subject: [PATCH] Added methods, added marking of completed arrays: - 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 --- .../handler/chunks/class_HandleableChunks.php | 22 ++++++++ .../handler/chunks/class_ChunkHandler.php | 56 ++++++++++++++++++- .../chunks/class_HubChunkAssemblerTask.php | 24 ++++++-- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/application/hub/interfaces/handler/chunks/class_HandleableChunks.php b/application/hub/interfaces/handler/chunks/class_HandleableChunks.php index ab9c59318..ecef5d425 100644 --- a/application/hub/interfaces/handler/chunks/class_HandleableChunks.php +++ b/application/hub/interfaces/handler/chunks/class_HandleableChunks.php @@ -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] diff --git a/application/hub/main/handler/chunks/class_ChunkHandler.php b/application/hub/main/handler/chunks/class_ChunkHandler.php index 43731ad10..7090c425e 100644 --- a/application/hub/main/handler/chunks/class_ChunkHandler.php +++ b/application/hub/main/handler/chunks/class_ChunkHandler.php @@ -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.'); } } diff --git a/application/hub/main/tasks/hub/chunks/class_HubChunkAssemblerTask.php b/application/hub/main/tasks/hub/chunks/class_HubChunkAssemblerTask.php index 8c040645b..abedc4326 100644 --- a/application/hub/main/tasks/hub/chunks/class_HubChunkAssemblerTask.php +++ b/application/hub/main/tasks/hub/chunks/class_HubChunkAssemblerTask.php @@ -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 + } } } -- 2.39.5