]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/chunks/class_ChunkHandler.php
91fa097bbf811724f7697a211686fe4d2e53a36b
[hub.git] / application / hub / main / handler / chunks / class_ChunkHandler.php
1 <?php
2 /**
3  * A Chunk handler
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable {
25         /**
26          * Stacker for chunks with final EOP
27          */
28         const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks';
29         const STACKER_NAME_CHUNKS_WITHOUT_FINAL  = 'pending_chunks';
30         const STACKER_NAME_ASSEMBLED_RAW_DATA    = 'chunk_raw_data';
31
32         /**
33          * Chunk splits:
34          * 0 = Hash
35          * 1 = Serial number
36          * 2 = Raw data
37          */
38         const CHUNK_SPLITS_INDEX_HASH     = 0;
39         const CHUNK_SPLITS_INDEX_SERIAL   = 1;
40         const CHUNK_SPLITS_INDEX_RAW_DATA = 2;
41
42         /**
43          * The final array for assembling the original package back together
44          */
45         private $finalPackageChunks = array();
46
47         /**
48          * Array of chunk hashes
49          */
50         private $chunkHashes = array();
51
52         /**
53          * Raw EOP chunk data in an array:
54          *
55          * 0 = Final hash,
56          * 1 = Hash of last chunk
57          */
58         private $eopChunk = array();
59
60         /**
61          * Raw package data
62          */
63         private $rawPackageData = '';
64
65         /**
66          * Protected constructor
67          *
68          * @return      void
69          */
70         protected function __construct () {
71                 // Call parent constructor
72                 parent::__construct(__CLASS__);
73
74                 // Set handler name
75                 $this->setHandlerName('chunk');
76
77                 // Initialize handler
78                 $this->initHandler();
79         }
80
81         /**
82          * Creates an instance of this class
83          *
84          * @return      $handlerInstance        An instance of a chunk Handler class
85          */
86         public final static function createChunkHandler () {
87                 // Get new instance
88                 $handlerInstance = new ChunkHandler();
89
90                 // Get a FIFO stacker
91                 $stackInstance = ObjectFactory::createObjectByConfiguredName('chunk_handler_stacker_class');
92
93                 // Init all stacker
94                 $stackInstance->initStacks(array(
95                         self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP,
96                         self::STACKER_NAME_CHUNKS_WITHOUT_FINAL,
97                         self::STACKER_NAME_ASSEMBLED_RAW_DATA
98                 ));
99
100                 // Set the stacker in this handler
101                 $handlerInstance->setStackInstance($stackInstance);
102
103                 // Get a crypto instance ...
104                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
105
106                 // ... and set it in this handler
107                 $handlerInstance->setCryptoInstance($cryptoInstance);
108
109                 // Get a fragmenter instance for later verification of serial numbers (e.g. if all are received)
110                 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
111
112                 // Set it in this handler
113                 $handlerInstance->setFragmenterInstance($fragmenterInstance);
114
115                 // Return the prepared instance
116                 return $handlerInstance;
117         }
118
119         /**
120          * Initializes the handler
121          *
122          * @return      void
123          */
124         private function initHandler () {
125                 // Noisy debug line:
126                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing handler ...');
127
128                 // Init finalPackageChunks
129                 $this->finalPackageChunks = array(
130                         // Array for package content
131                         'content'        => array(),
132                         // ... and for the hashes
133                         'hashes'         => array(),
134                         // ... marker for that the final array is complete for assembling all chunks
135                         'is_complete'    => FALSE,
136                         // ... steps done to assemble all chunks
137                         'assemble_steps' => 0,
138                 );
139
140                 // ... chunkHashes:
141                 $this->chunkHashes = array();
142
143                 // ... eopChunk:
144                 $this->eopChunk = array(
145                         0 => 'INVALID',
146                         1 => 'INVALID',
147                 );
148         }
149
150         /**
151          * Checks whether the hash generated from package content is the same ("valid") as given
152          *
153          * @param       $chunkSplits    An array from a splitted chunk
154          * @return      $isValid                Whether the hash is "valid"
155          */
156         private function isChunkHashValid (array $chunkSplits) {
157                 // Noisy debug line:
158                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkSplits=' . print_r($chunkSplits, TRUE));
159
160                 // Assert on some elements
161                 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]));
162                 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]));
163
164                 // Now hash the raw data again
165                 $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE);
166
167                 // Check it
168                 $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
169
170                 // Debug output
171                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHash=' . $chunkHash . ',isValid=' . intval($isValid));
172
173                 // ... and return it
174                 return $isValid;
175         }
176
177         /**
178          * Checks whether the given serial number is valid
179          *
180          * @param       $serialNumber   A serial number from a chunk
181          * @return      $isValid                Whether the serial number is valid
182          */
183         private function isSerialNumberValid ($serialNumber) {
184                 // Check it
185                 $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
186
187                 // Return result
188                 return $isValid;
189         }
190
191         /**
192          * Adds the chunk to the final array which will be used for the final step
193          * which will be to assemble all chunks back to the original package content
194          * and for the final hash check.
195          *
196          * This method may throw an exception if a chunk with the same serial number
197          * has already been added to avoid mixing chunks from different packages.
198          *
199          * @param       $chunkSplits    An array from a splitted chunk
200          * @return      void
201          */
202         private function addChunkToFinalArray (array $chunkSplits) {
203                 // Is the serial number (index 1) already been added?
204                 if (isset($this->finalPackageChunks[$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]])) {
205                         // Then throw an exception
206                         throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED);
207                 } // END - if
208
209                 // Debug message
210                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]);
211
212                 // Add the chunk data (index 2) to the final array and use the serial number as index
213                 $this->finalPackageChunks['content'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA];
214
215                 // ... and the hash as well
216                 $this->finalPackageChunks['hashes'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH];
217         }
218
219         /**
220          * Marks the final array as completed, do only this if you really have all
221          * chunks together including EOP and "hash chunk".
222          *
223          * @return      void
224          */
225         private function markFinalArrayAsCompleted () {
226                 /*
227                  * As for now, just set the array element. If any further steps are
228                  * being added, this should always be the last step.
229                  */
230                 $this->finalPackageChunks['is_complete'] = TRUE;
231         }
232
233         /**
234          * Sorts the chunks array by using the serial number as a sorting key. In
235          * most situations a call of ksort() is enough to accomblish this. So this
236          * method may only call ksort() on the chunks array.
237          *
238          * This method sorts 'content' and 'hashes' so both must have used the
239          * serial numbers as array indexes.
240          *
241          * @return      void
242          */
243         private function sortChunksArray () {
244                 // Sort 'content' first
245                 ksort($this->finalPackageChunks['content']);
246
247                 // ... then 'hashes'
248                 ksort($this->finalPackageChunks['hashes']);
249         }
250
251         /**
252          * Prepares the package assemble by removing last chunks (last shall be
253          * hash chunk, pre-last shall be EOP chunk) and verify that all serial
254          * numbers are valid (same as PackageFragmenter class would generate).
255          *
256          * @return      void
257          */
258         private function preparePackageAssmble () {
259                 // Make sure both arrays have same count (this however should always be TRUE)
260                 assert(count($this->finalPackageChunks['hashes']) == count($this->finalPackageChunks['content']));
261                 //* DIE: */ exit(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'], TRUE));
262
263                 /*
264                  * Remove last element (hash chunk) from 'hashes'. This hash will never
265                  * be needed, so ignore it.
266                  */
267                 array_pop($this->finalPackageChunks['hashes']);
268
269                 // ... and from 'content' as well but save it for later use
270                 $this->chunkHashes = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::HASH_CHUNK_IDENTIFIER)));
271
272                 // Remove EOP chunk and keep a copy of it
273                 array_pop($this->finalPackageChunks['hashes']);
274                 $this->eopChunk = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)));
275
276                 // Verify all serial numbers
277                 $this->verifyChunkSerialNumbers();
278         }
279
280         /**
281          * Verifies all chunk serial numbers by using a freshly initialized
282          * fragmenter instance. Do ALWAYS sort the array and array_pop() the hash
283          * chunk before calling this method to avoid re-requests of many chunks.
284          *
285          * @return      void
286          */
287         private function verifyChunkSerialNumbers () {
288                 // Debug message
289                 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE));
290
291                 // Get final hash
292                 $finalHash = $this->generateFinalHash(implode('', $this->finalPackageChunks['content']));
293
294                 // Reset the serial number generator
295                 $this->getFragmenterInstance()->resetSerialNumber($finalHash);
296
297                 // "Walk" through all (content) chunks
298                 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
299                         // Get next serial number
300                         $nextSerial = $this->getFragmenterInstance()->getNextHexSerialNumber($finalHash);
301
302                         // Debug output
303                         //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
304
305                         // Is it not the same? Then re-request it
306                         if ($serialNumber != $nextSerial) {
307                                 // This is invalid, so remove it
308                                 unset($this->finalPackageChunks['content'][$serialNumber]);
309                                 unset($this->finalPackageChunks['hashes'][$serialNumber]);
310
311                                 // And re-request it with valid serial number (and hash chunk)
312                                 $this->rerequestChunkBySerialNumber($nextSerial);
313                         } // END - if
314                 } // END - foreach
315         }
316
317         /**
318          * Assembles and verifies ("final check") chunks back together to the
319          * original package (raw data for the start). This method should only be
320          * called AFTER the EOP and final-chunk chunk have been removed.
321          *
322          * @return      void
323          */
324         private function assembleAllChunksToPackage () {
325                 // If chunkHashes is not filled, don't continue
326                 assert(count($this->chunkHashes) > 0);
327
328                 // Init raw package data string
329                 $this->rawPackageData = '';
330
331                 // That went well, so start assembling all chunks
332                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Handling ' . count($this->finalPackageChunks['content']) . ' entries ...');
333                 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
334                         // Assert on 'hash' entry (must always be set)
335                         assert(isset($this->finalPackageChunks['hashes'][$serialNumber]));
336
337                         // Debug message
338                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',hashes=' . $this->finalPackageChunks['hashes'][$serialNumber] . ' - validating ...');
339                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE));
340
341                         // Is this chunk valid? This should be the case
342                         assert($this->isChunkHashValid(array(
343                                 self::CHUNK_SPLITS_INDEX_HASH     => $this->finalPackageChunks['hashes'][$serialNumber],
344                                 self::CHUNK_SPLITS_INDEX_RAW_DATA => $content
345                         )));
346
347                         // ... and is also in the hash chunk?
348                         assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
349
350                         // Verification okay, add it to the raw data
351                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($content) . ' bytes as raw package data ...');
352                         $this->rawPackageData .= $content;
353                 } // END - foreach
354
355                 // Debug output
356                 //* 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));
357
358                 // The last chunk hash must match with the one from eopChunk[1]
359                 assert($this->eopChunk[1] == $this->chunkHashes[count($this->chunkHashes) - 2]);
360         }
361
362         /**
363          * Generate final hash if EOP chunk is found, else an assert will happen.
364          *
365          * @param       $rawPackageData         Raw package data
366          * @return      $finalHash                      Final hash if EOP chunk is found
367          */
368         private function generateFinalHash ($rawPackageData) {
369                 // Make sure the raw package data is given
370                 assert((is_string($rawPackageData)) && (!empty($rawPackageData)));
371
372                 // Make sure the EOP chunk is set
373                 assert((isset($this->eopChunk[0])) && (isset($this->eopChunk[1])));
374                 assert((is_string($this->eopChunk[0])) && (!empty($this->eopChunk[0])));
375
376                 // Hash the raw data
377                 $finalHash = $this->getCryptoInstance()->hashString($rawPackageData, $this->eopChunk[0], FALSE);
378
379                 // Return it
380                 return $finalHash;
381         }
382
383         /**
384          * Verifies the finally assembled raw package data by comparing it against
385          * the final hash.
386          *
387          * @return      void
388          */
389         private function verifyRawPackageData () {
390                 // Generate final hash
391                 $finalHash = $this->generateFinalHash($this->rawPackageData);
392
393                 // Is it the same?
394                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',finalHash=' . $finalHash);
395                 assert($finalHash == $this->eopChunk[0]);
396         }
397
398         /**
399          * Adds all chunks if the last one verifies as a 'final chunk'.
400          *
401          * @param       $chunks         An array with chunks, the last one should be a 'final'
402          * @return      void
403          * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
404          */
405         public function addAllChunksWithFinal (array $chunks) {
406                 // Try to validate the final chunk
407                 try {
408                         // Validate final chunk
409                         $this->isValidFinalChunk($chunks);
410                 } catch (AssertionException $e) {
411                         // Last chunk is not valid
412                         throw new FinalChunkVerificationException(array($this, $chunks, $e), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
413                 }
414
415                 // Do we have some pending chunks (no final)?
416                 while (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)) {
417                         // Then get it first and add it before the EOP chunks
418                         array_unshift($chunks, $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL));
419                 } // END - while
420
421                 // Add all chunks to the FIFO stacker
422                 foreach ($chunks as $chunk) {
423                         // Add the chunk
424                         $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP, $chunk);
425                 } // END - foreach
426         }
427
428         /**
429          * Adds all chunks and wait for more (e.g. incomplete transmission)
430          *
431          * @param       $chunks         An array with chunks, the last one should be a 'final'
432          * @return      void
433          */
434         public function addAllChunksWait (array $chunks) {
435                 // Add all chunks to the FIFO stacker
436                 foreach ($chunks as $chunk) {
437                         // Add the chunk
438                         $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL, $chunk);
439                 } // END - foreach
440         }
441
442         /**
443          * Checks whether unhandled chunks are available
444          *
445          * @return      $unhandledChunks        Whether unhandled chunks are left
446          */
447         public function ifUnhandledChunksWithFinalAvailable () {
448                 // Simply check if the stacker is not empty
449                 $unhandledChunks = $this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP) === FALSE;
450
451                 // Return result
452                 return $unhandledChunks;
453         }
454
455         /**
456          * Handles available chunks by processing one-by-one (not all together,
457          * this would slow-down the whole application) with the help of an
458          * iterator.
459          *
460          * @return      void
461          */
462         public function handleAvailableChunksWithFinal () {
463                 // First check if there are undhandled chunks available
464                 assert($this->ifUnhandledChunksWithFinalAvailable());
465
466                 // Get an entry from the stacker
467                 $chunk = $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
468
469                 // Split the string with proper separator character
470                 $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk);
471
472                 /*
473                  * Make sure three elements are always found:
474                  * 0 = Hash
475                  * 1 = Serial number
476                  * 2 = Raw data
477                  */
478                 assert(count($chunkSplits) == 3);
479
480                 // Is the generated hash from data same ("valid") as given hash?
481                 if (!$this->isChunkHashValid($chunkSplits)) {
482                         // Do some logging
483                         self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Chunk content is not validating against given hash.');
484
485                         // Re-request this chunk (trust the hash in index # 0)
486                         $this->rerequestChunkBySplitsArray($chunkSplits);
487
488                         // Don't process this chunk
489                         return;
490                 } // END - if
491
492                 // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)?
493                 if (!$this->isSerialNumberValid($chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL])) {
494                         // Do some logging
495                         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.');
496
497                         // Re-request this chunk
498                         $this->rerequestChunkBySplitsArray($chunkSplits);
499
500                         // Don't process this chunk
501                         return;
502                 } // END - if
503
504                 /*
505                  * It is now known that (as long as the hash algorithm has no
506                  * collisions) the content is the same as the sender sends it to this
507                  * peer.
508                  *
509                  * And also the serial number is valid (basicly) at this point. Now the
510                  * chunk can be added to the final array.
511                  */
512                 $this->addChunkToFinalArray($chunkSplits);
513
514                 // Is the stack now empty?
515                 if ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP)) {
516                         // Then mark the final array as complete
517                         $this->markFinalArrayAsCompleted();
518                 } // END - if
519         }
520
521         /**
522          * Checks whether unassembled chunks are available (ready) in final array
523          *
524          * @return      $unassembledChunksAvailable             Whether unassembled chunks are available
525          */
526         public function ifUnassembledChunksAvailable () {
527                 // For now do only check the array element 'is_complete'
528                 $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === TRUE);
529
530                 // Return status
531                 return $unassembledChunksAvailable;
532         }
533
534         /**
535          * Assembles all chunks (except EOP and "hash chunk") back together to the original package data.
536          *
537          * This is done by the following steps:
538          *
539          * 1) Sort the final array with ksort(). This will bring the "hash
540          *    chunk" up to the last array index and the EOP chunk to the
541          *    pre-last array index
542          * 2) Assemble all chunks except two last (see above step)
543          * 3) While so, do the final check on all hashes
544          * 4) If the package is assembled back together, hash it again for
545          *    the very final verification.
546          *
547          * @return      void
548          */
549         public function assembleChunksFromFinalArray () {
550                 // Make sure the final array is really completed
551                 assert($this->ifUnassembledChunksAvailable());
552
553                 // Count up stepping
554                 $this->finalPackageChunks['assemble_steps']++;
555
556                 // Do the next step
557                 switch ($this->finalPackageChunks['assemble_steps']) {
558                         case 1: // Sort the chunks array (the serial number shall act as a sorting key)
559                                 $this->sortChunksArray();
560                                 break;
561
562                         case 2: // Prepare the assemble by removing last two indexes
563                                 $this->preparePackageAssmble();
564                                 break;
565
566                         case 3: // Assemble all chunks back together to the original package
567                                 $this->assembleAllChunksToPackage();
568                                 break;
569
570                         case 4: // Verify the raw data by hashing it again
571                                 $this->verifyRawPackageData();
572                                 break;
573
574                         case 5: // Re-initialize handler to reset it to the old state
575                                 $this->initHandler();
576                                 break;
577
578                         default: // Invalid step found
579                                 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
580                                 break;
581                 } // END - switch
582         }
583
584         /**
585          * Checks whether the raw package data has been assembled back together.
586          * This can be safely assumed when rawPackageData is not empty and the
587          * collection of all chunks is FALSE (because initHandler() will reset it).
588          *
589          * @return      $isRawPackageDataAvailable      Whether raw package data is available
590          */
591         public function ifRawPackageDataIsAvailable () {
592                 // Check it
593                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: this->rawPackageData()=' . strlen($this->rawPackageData) . ',ifUnassembledChunksAvailable()=' . intval($this->ifUnassembledChunksAvailable()));
594                 $isRawPackageDataAvailable = ((!empty($this->rawPackageData)) && (!$this->ifUnassembledChunksAvailable()));
595
596                 // Return it
597                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: isRawPackageDataAvailable=' . intval($isRawPackageDataAvailable));
598                 return $isRawPackageDataAvailable;
599         }
600
601         /**
602          * Handles the finally assembled raw package data by feeding it into another
603          * stacker for further decoding/processing.
604          *
605          * @return      void
606          */
607         public function handledAssembledRawPackageData () {
608                 // Assert to make sure that there is raw package data available
609                 assert($this->ifRawPackageDataIsAvailable());
610
611                 // Then feed it into the next stacker
612                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($this->rawPackageData) . ' bytes to stack ' . self::STACKER_NAME_ASSEMBLED_RAW_DATA . ' ...');
613                 $this->getStackInstance()->pushNamed(self::STACKER_NAME_ASSEMBLED_RAW_DATA, $this->rawPackageData);
614
615                 // ... and reset it
616                 $this->rawPackageData = '';
617         }
618 }
619
620 // [EOF]
621 ?>