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