]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/chunks/class_ChunkHandler.php
More debug lines added.
[hub.git] / application / hub / main / handler / chunks / class_ChunkHandler.php
index ca3fa860030992e4a6f73d251502c999a326e187..b4d2fdc4965294d62f729eecbfc548ab336593c6 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A Chunk handler
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
  * @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
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -91,9 +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->initStacker(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL);
-               $stackerInstance->initStacker(self::STACKER_NAME_ASSEMBLED_RAW_DATA);
+               $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);
@@ -120,6 +122,9 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * @return      void
         */
        private function initHandler () {
+               // Noisy debug line:
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing handler ...');
+
                // Init finalPackageChunks
                $this->finalPackageChunks = array(
                        // Array for package content
@@ -127,7 +132,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                        // ... and for the hashes
                        'hashes'         => array(),
                        // ... marker for that the final array is complete for assembling all chunks
-                       'is_complete'    => false,
+                       'is_complete'    => FALSE,
                        // ... steps done to assemble all chunks
                        'assemble_steps' => 0,
                );
@@ -149,15 +154,22 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * @return      $isValid                Whether the hash is "valid"
         */
        private function isChunkHashValid (array $chunkSplits) {
-               // Now hash the raw data again
-               $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], false);
+               // Noisy debug line:
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkSplits=' . print_r($chunkSplits, TRUE));
 
-               // Debug output
-               //* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: 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]);
+               // Assert on some elements
+               assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]));
+               assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]));
+
+               // Now hash the raw data again
+               $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE);
 
                // Check it
                $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
 
+               // Debug output
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHash=' . $chunkHash . ',isValid=' . intval($isValid));
+
                // ... and return it
                return $isValid;
        }
@@ -170,7 +182,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private function isSerialNumberValid ($serialNumber) {
                // Check it
-               $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, false) === $serialNumber));
+               $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
 
                // Return result
                return $isValid;
@@ -195,7 +207,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                } // END - if
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __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[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA];
@@ -215,7 +227,7 @@ 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;
        }
 
        /**
@@ -244,9 +256,9 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         * @return      void
         */
        private function preparePackageAssmble () {
-               // Make sure both arrays have same count (this however should always be true)
+               // Make sure both arrays have same count (this however should always be TRUE)
                assert(count($this->finalPackageChunks['hashes']) == count($this->finalPackageChunks['content']));
-               //* DIE: */ die(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'],true));
+               //* DIE: */ exit(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'], TRUE));
 
                /*
                 * Remove last element (hash chunk) from 'hashes'. This hash will never
@@ -282,7 +294,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                        $nextSerial = $this->getFragmenterInstance()->getNextHexSerialNumber();
 
                        // Debug output
-                       /* NOISY-DEBUG */ $this->debugOutput('CHUNK-HANDLER: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
+                       //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
 
                        // Is it not the same? Then re-request it
                        if ($serialNumber != $nextSerial) {
@@ -311,10 +323,11 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                $this->rawPackageData = '';
 
                // That went well, so start assembling all chunks
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Handling ' . count($this->finalPackageChunks['content']) . ' entries ...');
                foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
                        // Debug message
-                       //* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: serialNumber=' . $serialNumber . ' - validating ...');
-                       //* NOISY-DEBUG: */ $this->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks,true) . 'chunkHashes=' . print_r($this->chunkHashes,true));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __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(
@@ -326,11 +339,12 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                        assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
 
                        // Verification okay, add it to the raw data
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($content) . ' bytes as raw package data ...');
                        $this->rawPackageData .= $content;
                } // END - foreach
 
                // Debug output
-               //* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: eopChunk[1]=' . $this->eopChunk[1] . ',' . chr(10) . 'index=' . (count($this->chunkHashes) - 2) . ',' . chr(10) . 'chunkHashes='.print_r($this->chunkHashes,true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',index=' . (count($this->chunkHashes) - 2) . ',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]);
@@ -344,9 +358,10 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
         */
        private function verifyRawPackageData () {
                // Hash the raw package data for final verification
-               $finalHash = $this->getCryptoInstance()->hashString($this->rawPackageData, $this->eopChunk[0], false);
+               $finalHash = $this->getCryptoInstance()->hashString($this->rawPackageData, $this->eopChunk[0], FALSE);
 
                // Is it the same?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',finalHash=' . $finalHash);
                assert($finalHash == $this->eopChunk[0]);
        }
 
@@ -358,11 +373,14 @@ 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)) {
@@ -398,7 +416,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;
@@ -432,7 +450,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[' . __METHOD__ . ':' . __LINE__ . ']: Chunk content is not validating against given hash.');
 
                        // Re-request this chunk (trust the hash in index # 0)
                        $this->rerequestChunkBySplitsArray($chunkSplits);
@@ -444,7 +462,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[self::CHUNK_SPLITS_INDEX_SERIAL])) {
                        // Do some logging
-                       $this->debugOutput('CHUNK-HANDLER: Chunk serial number ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ' for hash ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ' is invalid.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __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);
@@ -477,7 +495,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;
@@ -528,7 +546,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                                break;
 
                        default: // Invalid step found
-                               $this->debugOutput('CHUNK-HANDLER: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
+                               self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
                                break;
                } // END - switch
        }
@@ -536,15 +554,17 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
        /**
         * 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).
+        * collection of all chunks is FALSE (because initHandler() will reset it).
         *
         * @return      $isRawPackageDataAvailable      Whether raw package data is available
         */
        public function ifRawPackageDataIsAvailable () {
                // Check it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: this->rawPackageData()=' . strlen($this->rawPackageData) . ',ifUnassembledChunksAvailable()=' . intval($this->ifUnassembledChunksAvailable()));
                $isRawPackageDataAvailable = ((!empty($this->rawPackageData)) && (!$this->ifUnassembledChunksAvailable()));
 
                // Return it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: isRawPackageDataAvailable=' . intval($isRawPackageDataAvailable));
                return $isRawPackageDataAvailable;
        }