]> git.mxchange.org Git - hub.git/commitdiff
Continued with hub development: validation of chunk hash added
authorRoland Häder <roland@mxchange.org>
Sun, 26 Feb 2012 00:26:16 +0000 (00:26 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 26 Feb 2012 00:26:16 +0000 (00:26 +0000)
application/hub/main/handler/chunks/class_ChunkHandler.php
application/hub/main/handler/tasks/class_TaskHandler.php
application/hub/main/package/fragmenter/class_PackageFragmenter.php

index 06573bd812863ccb81fa9bdd577852c11feacbd6..14a726563dd477563c8596d6816cc2c27c7b63f4 100644 (file)
@@ -58,6 +58,12 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                // Set the stacker in this handler
                $handlerInstance->setStackerInstance($stackerInstance);
 
+               // Get a crypto instance ...
+               $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
+
+               // ... and set it in this handler
+               $handlerInstance->setCryptoInstance($cryptoInstance);
+
                // Return the prepared instance
                return $handlerInstance;
        }
@@ -108,7 +114,33 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable
                assert($this->ifUnhandledChunksAvailable());
 
                // Get an entry from the stacker
-               $chunk = $this->getStackerInstance()->getNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
+               $chunk = $this->getStackerInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
+
+               // Split the string with proper separator character
+               $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk);
+
+               /*
+                * Make sure three elements are always found:
+                * 0 = Hash
+                * 1 = Serial number
+                * 2 = Raw data
+                */
+               assert(count($chunkSplits) == 3);
+
+               // Now hash the raw data again
+               $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[2], $chunkSplits[0], false);
+
+               // Debug output
+               /* NOISY-DEBUG: */ $this->debugOutput('CHUNK-HANDLER: chunkHash=' . $chunkHash . ',chunkSplits[0]=' . $chunkSplits[0] . ',chunkSplits[1]=' . $chunkSplits[1]);
+
+               // Is it the same?
+               if ($chunkSplits[0] != $chunkHash) {
+                       // Re-request this chunk (trust the hash in index # 0)
+                       $this->rerequestChunkBySplitsArray($chunkSplits);
+
+                       // Don't process this chunk
+                       return;
+               } // END - if
                die('chunk=' . $chunk . chr(10));
        }
 }
index b9502d91e2debd90482a206d6a970fba853b106e..c936419f8d2fe24ad35790d716c3532d19a8af6d 100644 (file)
@@ -213,11 +213,11 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                $this->getListInstance()->addEntry('tasks', $taskEntry);
 
                // Debug message
-               $this->debugOutput('TASK-HANDLER: Task ' . $taskName .
+               $this->debugOutput('TASK-HANDLER: Task registered: taskName=' . $taskName .
                        ' (taskInstance=' . $taskInstance->__toString() . ')' .
                        ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
                        ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
-                       ', maxRuns=' . $taskEntry['task_max_runs'] . ' times registered.'
+                       ', maxRuns=' . $taskEntry['task_max_runs'] . ' ...'
                );
        }
 
@@ -263,11 +263,8 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
         * @return      void
         */
        public function doShutdown () {
-               // Should we rewind?
-               if (!$this->getIteratorInstance()->valid()) {
-                       // Rewind to the beginning for next loop
-                       $this->getIteratorInstance()->rewind();
-               } // END - if
+               // Always rewind to the beginning for next loop
+               $this->getIteratorInstance()->rewind();
 
                // Debug message
                $this->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
index ef6798126019981d1fb235ffff53687b265eee38..a254b3d18db7c4575a0d9dcef7a4409c7cf56982 100644 (file)
@@ -273,8 +273,11 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
         * @return      $hash           Hash from the raw data
         */
        private function generateHashFromRawData ($rawData) {
-               // Get the crypto instance and hash the data
-               $hash = $this->getCryptoInstance()->hashString($rawData);
+               /*
+                * Get the crypto instance and hash the data with no extra salt because
+                * the other peer doesn't have *this* peer's salt.
+                */
+               $hash = $this->getCryptoInstance()->hashString($rawData, '', false);
 
                // Return it
                return $hash;
@@ -348,7 +351,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
                        $chunk = substr($rawData, $idx, $dataChunkSize);
 
                        // Hash it and remember it in seperate array
-                       $chunkHash = $this->getCryptoInstance()->hashString($chunk);
+                       $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false);
                        $this->chunkHashes[$finalHash][] = $chunkHash;
 
                        // Prepend the hash to the chunk
@@ -396,7 +399,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg
                        $chunk = substr($rawData, $idx, $dataChunkSize);
 
                        // Hash it and remember it in seperate array
-                       $chunkHash = $this->getCryptoInstance()->hashString($chunk);
+                       $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false);
                        array_unshift($this->chunkHashes[$finalHash], $chunkHash);
 
                        // Prepend the hash to the chunk