]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/fragmenter/class_PackageFragmenter.php
Refactured EOP chunk, now it is formatted as any regular chunk. This makes the code...
[hub.git] / application / hub / main / package / fragmenter / class_PackageFragmenter.php
index 8799f131cef5fe3eddaf1118ba674e1f573aac0a..09da5b728beb60055053cab35fa31ad76e4ba0e7 100644 (file)
@@ -305,7 +305,8 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
 
        /**
         * Appends an end-of-package chunk to the chunk list for given chunk and
-        * final hash.
+        * final hash. As of 23-March-2012 the format of this chunk will be as any
+        * regular one to keep things easy (KISS) in ChunkHandler class.
         *
         * @param       $chunkHash      Last chunk's hash
         * @param       $finalHash      Final hash for raw (unencoded) data
@@ -313,17 +314,13 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
         */
        private function appendEndOfPackageChunk ($chunkHash, $finalHash) {
                // Generate end-of-package marker
-               $rawData =
+               $chunkData =
                        self::END_OF_PACKAGE_IDENTIFIER .
                        $finalHash . self::CHUNK_HASH_SEPARATOR .
                        $chunkHash . self::CHUNK_SEPARATOR;
 
-               // Also get a hash from it
-               $eopHash = $this->generateHashFromRawData($rawData);
-
-               // Append it to the eop's data and hash array
-               $this->chunkHashes[$finalHash][] = $eopHash;
-               $this->chunks[$finalHash][]      = $rawData;
+               // Add it as regular chunk
+               $this->addChunkData($chunkData);
        }
 
        /**
@@ -349,25 +346,10 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
                // Now split it up
                for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) {
                        // Get the next chunk
-                       $chunk = substr($rawData, $idx, $dataChunkSize);
-
-                       // Hash it and remember it in seperate array
-                       $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false);
-                       $this->chunkHashes[$finalHash][] = $chunkHash;
+                       $chunkData = substr($rawData, $idx, $dataChunkSize);
 
-                       // Prepend the hash to the chunk
-                       $chunk = (
-                               $chunkHash . self::CHUNK_DATA_HASH_SEPARATOR .
-                               $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR .
-                               $chunk . self::CHUNK_SEPARATOR
-                       );
-
-                       // Make sure the chunk is not larger than a TCP package can hold
-                       assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE);
-
-                       // Add it to the array
-                       //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($chunk) . ' bytes of a chunk.');
-                       $this->chunks[$finalHash][] = $chunk;
+                       // Add the chunk to the propper array and do all the stuff there
+                       $this->addChunkData($chunkData);
                } // END - for
 
                // Debug output
@@ -377,6 +359,33 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
                $this->appendEndOfPackageChunk($chunkHash, $finalHash);
        }
 
+       /**
+        * Adds the given chunk (raw data) to the proper array and hashes it for
+        * later verfication.
+        *
+        * @param       $chunkData      Raw chunk data
+        * @return      void
+        */
+       private function addChunkData ($chunkData) {
+               // Hash it
+               $rawDataHash = $this->getCryptoInstance()->hashString($chunkData, '', false);
+
+               // Prepend the hash to the chunk
+               $rawData = (
+                       $rawDataHash . self::CHUNK_DATA_HASH_SEPARATOR .
+                       $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR .
+                       $rawData . self::CHUNK_SEPARATOR
+               );
+
+               // Make sure the chunk is not larger than a TCP package can hold
+               assert(strlen($rawData) <= NetworkPackage::TCP_PACKAGE_SIZE);
+
+               // Add it to the array
+               //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($rawData) . ' bytes of a chunk.');
+               $this->chunks[$finalHash][]      = $rawData;
+               $this->chunkHashes[$finalHash][] = $rawDataHash;
+       }
+
        /**
         * Prepends a chunk (or more) with all hashes from all chunks + final chunk.
         *