]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/chunks/class_ChunkHandler.php
Renamed 'stacker' -> 'stack'
[hub.git] / application / hub / main / handler / chunks / class_ChunkHandler.php
index 7090c425e72d83ead644ae76ba9047fe61e10456..d0a7feafb6d12de0d417a5a0f5b7746ee0416b46 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -26,18 +26,41 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * Stacker for chunks with final EOP
         */
        const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks';
+       const STACKER_NAME_CHUNKS_WITHOUT_FINAL  = 'pending_chunks';
+       const STACKER_NAME_ASSEMBLED_RAW_DATA    = 'chunk_raw_data';
+
+       /**
+        * Chunk splits:
+        * 0 = Hash
+        * 1 = Serial number
+        * 2 = Raw data
+        */
+       const CHUNK_SPLITS_INDEX_HASH     = 0;
+       const CHUNK_SPLITS_INDEX_SERIAL   = 1;
+       const CHUNK_SPLITS_INDEX_RAW_DATA = 2;
 
        /**
         * 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(),
-               // ... marker for that the final array is complete for assembling all chunks
-               'is_complete' => false
-       );
+       private $finalPackageChunks = array();
+
+       /**
+        * Array of chunk hashes
+        */
+       private $chunkHashes = array();
+
+       /**
+        * Raw EOP chunk data in an array:
+        *
+        * 0 = Final hash,
+        * 1 = Hash of last chunk
+        */
+       private $eopChunk = array();
+
+       /**
+        * Raw package data
+        */
+       private $rawPackageData = '';
 
        /**
         * Protected constructor
@@ -50,6 +73,9 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
 
                // Set handler name
                $this->setHandlerName('chunk');
+
+               // Initialize handler
+               $this->initHandler();
        }
 
        /**
@@ -65,7 +91,11 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                $stackerInstance = ObjectFactory::createObjectByConfiguredName('chunk_handler_stacker_class');
 
                // Init all stacker
-               $stackerInstance->initStacker(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
+               $stackerInstance->initStacks(array(
+                       self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP,
+                       self::STACKER_NAME_CHUNKS_WITHOUT_FINAL,
+                       self::STACKER_NAME_ASSEMBLED_RAW_DATA
+               ));
 
                // Set the stacker in this handler
                $handlerInstance->setStackerInstance($stackerInstance);
@@ -76,10 +106,44 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                // ... and set it in this handler
                $handlerInstance->setCryptoInstance($cryptoInstance);
 
+               // Get a fragmenter instance for later verification of serial numbers (e.g. if all are received)
+               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
+
+               // Set it in this handler
+               $handlerInstance->setFragmenterInstance($fragmenterInstance);
+
                // Return the prepared instance
                return $handlerInstance;
        }
 
+       /**
+        * Initializes the handler
+        *
+        * @return      void
+        */
+       private function initHandler () {
+               // Init finalPackageChunks
+               $this->finalPackageChunks = array(
+                       // Array for package content
+                       'content'        => array(),
+                       // ... and for the hashes
+                       'hashes'         => array(),
+                       // ... marker for that the final array is complete for assembling all chunks
+                       'is_complete'    => FALSE,
+                       // ... steps done to assemble all chunks
+                       'assemble_steps' => 0,
+               );
+
+               // ... chunkHashes:
+               $this->chunkHashes = array();
+
+               // ... eopChunk:
+               $this->eopChunk = array(
+                       0 => 'INVALID',
+                       1 => 'INVALID',
+               );
+       }
+
        /**
         * Checks whether the hash generated from package content is the same ("valid") as given
         *
@@ -88,13 +152,13 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private function isChunkHashValid (array $chunkSplits) {
                // Now hash the raw data again
-               $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[2], $chunkSplits[0], false);
+               $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE);
 
                // Debug output
-               //* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: chunkHash=' . $chunkHash . ',chunkSplits[0]=' . $chunkSplits[0] . ',chunkSplits[1]=' . $chunkSplits[1]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: chunkHash=' . $chunkHash . ',chunkSplits[chunk_hash]=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ',chunkSplits[serial]=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',chunkSplits[raw_data]=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]);
 
                // Check it
-               $isValid = ($chunkSplits[0] === $chunkHash);
+               $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
 
                // ... and return it
                return $isValid;
@@ -108,7 +172,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private function isSerialNumberValid ($serialNumber) {
                // Check it
-               $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->bigintval($serialNumber, false) === $serialNumber));
+               $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
 
                // Return result
                return $isValid;
@@ -127,16 +191,19 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private function addChunkToFinalArray (array $chunkSplits) {
                // Is the serial number (index 1) already been added?
-               if (isset($this->finalPackageChunks[$chunkSplits[1]])) {
+               if (isset($this->finalPackageChunks[$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]])) {
                        // Then throw an exception
                        throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED);
                } // END - if
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]);
+
                // Add the chunk data (index 2) to the final array and use the serial number as index
-               $this->finalPackageChunks['content'][$chunkSplits[1]] = $chunkSplits[2];
+               $this->finalPackageChunks['content'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA];
 
                // ... and the hash as well
-               $this->finalPackageChunks['hashes'][$chunkSplits[1]] = $chunkSplits[0];
+               $this->finalPackageChunks['hashes'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH];
        }
 
        /**
@@ -150,7 +217,139 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                 * 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;
+               $this->finalPackageChunks['is_complete'] = TRUE;
+       }
+
+       /**
+        * Sorts the chunks array by using the serial number as a sorting key. In
+        * most situations a call of ksort() is enough to accomblish this. So this
+        * method may only call ksort() on the chunks array.
+        *
+        * This method sorts 'content' and 'hashes' so both must have used the
+        * serial numbers as array indexes.
+        *
+        * @return      void
+        */
+       private function sortChunksArray () {
+               // Sort 'content' first
+               ksort($this->finalPackageChunks['content']);
+
+               // ... then 'hashes'
+               ksort($this->finalPackageChunks['hashes']);
+       }
+
+       /**
+        * Prepares the package assemble by removing last chunks (last shall be
+        * hash chunk, pre-last shall be EOP chunk) and verify that all serial
+        * numbers are valid (same as PackageFragmenter class would generate).
+        *
+        * @return      void
+        */
+       private function preparePackageAssmble () {
+               // Make sure both arrays have same count (this however should always be TRUE)
+               assert(count($this->finalPackageChunks['hashes']) == count($this->finalPackageChunks['content']));
+               //* DIE: */ exit(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'], TRUE));
+
+               /*
+                * Remove last element (hash chunk) from 'hashes'. This hash will never
+                * be needed, so ignore it.
+                */
+               array_pop($this->finalPackageChunks['hashes']);
+
+               // ... and from 'content' as well but save it for later use
+               $this->chunkHashes = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::HASH_CHUNK_IDENTIFIER)));
+
+               // Remove EOP chunk and keep a copy of it
+               array_pop($this->finalPackageChunks['hashes']);
+               $this->eopChunk = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)));
+
+               // Verify all serial numbers
+               $this->verifyChunkSerialNumbers();
+       }
+
+       /**
+        * Verifies all chunk serial numbers by using a freshly initialized
+        * fragmenter instance. Do ALWAYS sort the array and array_pop() the hash
+        * chunk before calling this method to avoid re-requests of many chunks.
+        *
+        * @return      void
+        */
+       private function verifyChunkSerialNumbers () {
+               // Reset the serial number generator
+               $this->getFragmenterInstance()->resetSerialNumber();
+
+               // "Walk" through all (content) chunks
+               foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
+                       // Get next serial number
+                       $nextSerial = $this->getFragmenterInstance()->getNextHexSerialNumber();
+
+                       // Debug output
+                       //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
+
+                       // Is it not the same? Then re-request it
+                       if ($serialNumber != $nextSerial) {
+                               // This is invalid, so remove it
+                               unset($this->finalPackageChunks['content'][$serialNumber]);
+                               unset($this->finalPackageChunks['hashes'][$serialNumber]);
+
+                               // And re-request it with valid serial number (and hash chunk)
+                               $this->rerequestChunkBySerialNumber($nextSerial);
+                       } // END - if
+               } // END - foreach
+       }
+
+       /**
+        * Assembles and verifies ("final check") chunks back together to the
+        * original package (raw data for the start). This method should only be
+        * called AFTER the EOP and final-chunk chunk have been removed.
+        *
+        * @return      void
+        */
+       private function assembleAllChunksToPackage () {
+               // If chunkHashes is not filled, don't continue
+               assert(count($this->chunkHashes) > 0);
+
+               // Init raw package data string
+               $this->rawPackageData = '';
+
+               // That went well, so start assembling all chunks
+               foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: serialNumber=' . $serialNumber . ' - validating ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE));
+
+                       // Is this chunk valid? This should be the case
+                       assert($this->isChunkHashValid(array(
+                               self::CHUNK_SPLITS_INDEX_HASH     => $this->finalPackageChunks['hashes'][$serialNumber],
+                               self::CHUNK_SPLITS_INDEX_RAW_DATA => $content
+                       )));
+
+                       // ... and is also in the hash chunk?
+                       assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
+
+                       // Verification okay, add it to the raw data
+                       $this->rawPackageData .= $content;
+               } // END - foreach
+
+               // Debug output
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',' . chr(10) . 'index=' . (count($this->chunkHashes) - 2) . ',' . chr(10) . 'chunkHashes='.print_r($this->chunkHashes, TRUE));
+
+               // The last chunk hash must match with the one from eopChunk[1]
+               assert($this->eopChunk[1] == $this->chunkHashes[count($this->chunkHashes) - 2]);
+       }
+
+       /**
+        * Verifies the finally assembled raw package data by comparing it against
+        * the final hash.
+        *
+        * @return      void
+        */
+       private function verifyRawPackageData () {
+               // Hash the raw package data for final verification
+               $finalHash = $this->getCryptoInstance()->hashString($this->rawPackageData, $this->eopChunk[0], FALSE);
+
+               // Is it the same?
+               assert($finalHash == $this->eopChunk[0]);
        }
 
        /**
@@ -161,11 +360,20 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
         */
        public function addAllChunksWithFinal (array $chunks) {
-               // Validate final chunk
-               if (!$this->isValidFinalChunk($chunks)) {
+               // Try to validate the final chunk
+               try {
+                       // Validate final chunk
+                       $this->isValidFinalChunk($chunks);
+               } catch (AssertionException $e) {
                        // Last chunk is not valid
-                       throw new FinalChunkVerificationException(array($this, $chunks), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
-               } // END - if
+                       throw new FinalChunkVerificationException(array($this, $chunks, $e), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
+               }
+
+               // Do we have some pending chunks (no final)?
+               while (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)) {
+                       // Then get it first and add it before the EOP chunks
+                       array_unshift($chunks, $this->getStackerInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL));
+               } // END - while
 
                // Add all chunks to the FIFO stacker
                foreach ($chunks as $chunk) {
@@ -174,6 +382,20 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                } // END - foreach
        }
 
+       /**
+        * Adds all chunks and wait for more (e.g. incomplete transmission)
+        *
+        * @param       $chunks         An array with chunks, the last one should be a 'final'
+        * @return      void
+        */
+       public function addAllChunksWait (array $chunks) {
+               // Add all chunks to the FIFO stacker
+               foreach ($chunks as $chunk) {
+                       // Add the chunk
+                       $this->getStackerInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL, $chunk);
+               } // END - foreach
+       }
+
        /**
         * Checks whether unhandled chunks are available
         *
@@ -181,7 +403,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        public function ifUnhandledChunksWithFinalAvailable () {
                // Simply check if the stacker is not empty
-               $unhandledChunks = $this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP) === false;
+               $unhandledChunks = $this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP) === FALSE;
 
                // Return result
                return $unhandledChunks;
@@ -215,7 +437,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                // Is the generated hash from data same ("valid") as given hash?
                if (!$this->isChunkHashValid($chunkSplits)) {
                        // Do some logging
-                       $this->debugOutput('CHUNK-HANDLER: Chunk content is not validating against given hash.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: Chunk content is not validating against given hash.');
 
                        // Re-request this chunk (trust the hash in index # 0)
                        $this->rerequestChunkBySplitsArray($chunkSplits);
@@ -225,9 +447,9 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                } // END - if
 
                // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)?
-               if (!$this->isSerialNumberValid($chunkSplits[1])) {
+               if (!$this->isSerialNumberValid($chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL])) {
                        // Do some logging
-                       $this->debugOutput('CHUNK-HANDLER: Chunk serial numberĀ for hash ' . $chunkSplits[0] . ' is invalid.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: Chunk serial number ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ' for hash ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ' is invalid.');
 
                        // Re-request this chunk
                        $this->rerequestChunkBySplitsArray($chunkSplits);
@@ -241,9 +463,8 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                 * collisions) the content is the same as the sender sends it to this
                 * peer.
                 *
-                * And also the serial number is valid (basicly) at this point.
-                *
-                * Now the chunk can be added to the final array
+                * And also the serial number is valid (basicly) at this point. Now the
+                * chunk can be added to the final array.
                 */
                $this->addChunkToFinalArray($chunkSplits);
 
@@ -261,7 +482,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        public function ifUnassembledChunksAvailable () {
                // For now do only check the array element 'is_complete'
-               $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === true);
+               $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === TRUE);
 
                // Return status
                return $unassembledChunksAvailable;
@@ -279,9 +500,74 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * 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.
+        *
+        * @return      void
         */
        public function assembleChunksFromFinalArray () {
-               $this->partialStub('Please implement this method.');
+               // Make sure the final array is really completed
+               assert($this->ifUnassembledChunksAvailable());
+
+               // Count up stepping
+               $this->finalPackageChunks['assemble_steps']++;
+
+               // Do the next step
+               switch ($this->finalPackageChunks['assemble_steps']) {
+                       case 1: // Sort the chunks array (the serial number shall act as a sorting key)
+                               $this->sortChunksArray();
+                               break;
+
+                       case 2: // Prepare the assemble by removing last two indexes
+                               $this->preparePackageAssmble();
+                               break;
+
+                       case 3: // Assemble all chunks back together to the original package
+                               $this->assembleAllChunksToPackage();
+                               break;
+
+                       case 4: // Verify the raw data by hashing it again
+                               $this->verifyRawPackageData();
+                               break;
+
+                       case 5: // Re-initialize handler to reset it to the old state
+                               $this->initHandler();
+                               break;
+
+                       default: // Invalid step found
+                               self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __LINE__ . ']: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
+                               break;
+               } // END - switch
+       }
+
+       /**
+        * Checks whether the raw package data has been assembled back together.
+        * This can be safely assumed when rawPackageData is not empty and the
+        * collection of all chunks is FALSE (because initHandler() will reset it).
+        *
+        * @return      $isRawPackageDataAvailable      Whether raw package data is available
+        */
+       public function ifRawPackageDataIsAvailable () {
+               // Check it
+               $isRawPackageDataAvailable = ((!empty($this->rawPackageData)) && (!$this->ifUnassembledChunksAvailable()));
+
+               // Return it
+               return $isRawPackageDataAvailable;
+       }
+
+       /**
+        * Handles the finally assembled raw package data by feeding it into another
+        * stacker for further decoding/processing.
+        *
+        * @return      void
+        */
+       public function handledAssembledRawPackageData () {
+               // Assert to make sure that there is raw package data available
+               assert($this->ifRawPackageDataIsAvailable());
+
+               // Then feed it into the next stacker
+               $this->getStackerInstance()->pushNamed(self::STACKER_NAME_ASSEMBLED_RAW_DATA, $this->rawPackageData);
+
+               // ... and reset it
+               $this->rawPackageData = '';
        }
 }