]> git.mxchange.org Git - hub.git/commitdiff
Refactured EOP chunk, now it is formatted as any regular chunk. This makes the code...
authorRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 21:08:20 +0000 (21:08 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 23 Mar 2012 21:08:20 +0000 (21:08 +0000)
application/hub/main/class_BaseHubSystem.php
application/hub/main/package/fragmenter/class_PackageFragmenter.php

index 4de321119f6054d8f9ac7294566c5806af064b38..845daef6b8b50cc9d43c0af62a3287a1f01ca138 100644 (file)
@@ -286,11 +286,17 @@ class BaseHubSystem extends BaseFrameworkSystem {
                // Default is all fine
                $isValid = true;
 
+               // Split the (possible) EOP chunk
+               $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]);
+
+               // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler)
+               assert(count($chunkSplits) == 3);
+
                // Validate final chunk
-               if (substr($chunks[count($chunks) - 1], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) {
+               if (substr($chunkSplits[2], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) {
                        // Not fine
                        $isValid = false;
-               } elseif (substr_count($chunks[count($chunks) - 1], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) {
+               } elseif (substr_count($chunkSplits[2], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) {
                        // CHUNK_HASH_SEPARATOR shall only be found once
                        $isValid = false;
                }
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.
         *