5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
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.
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.
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/>.
24 class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable {
26 * Stacker for chunks with final EOP
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';
38 const CHUNK_SPLITS_INDEX_HASH = 0;
39 const CHUNK_SPLITS_INDEX_SERIAL = 1;
40 const CHUNK_SPLITS_INDEX_RAW_DATA = 2;
43 * The final array for assembling the original package back together
45 private $finalPackageChunks = array();
48 * Array of chunk hashes
50 private $chunkHashes = array();
53 * Raw EOP chunk data in an array:
56 * 1 = Hash of last chunk
58 private $eopChunk = array();
63 private $rawPackageData = '';
66 * Protected constructor
70 protected function __construct () {
71 // Call parent constructor
72 parent::__construct(__CLASS__);
75 $this->setHandlerName('chunk');
82 * Creates an instance of this class
84 * @return $handlerInstance An instance of a chunk Handler class
86 public final static function createChunkHandler () {
88 $handlerInstance = new ChunkHandler();
91 $stackerInstance = ObjectFactory::createObjectByConfiguredName('chunk_handler_stacker_class');
94 $stackerInstance->initStacks(array(
95 self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP,
96 self::STACKER_NAME_CHUNKS_WITHOUT_FINAL,
97 self::STACKER_NAME_ASSEMBLED_RAW_DATA
100 // Set the stacker in this handler
101 $handlerInstance->setStackerInstance($stackerInstance);
103 // Get a crypto instance ...
104 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
106 // ... and set it in this handler
107 $handlerInstance->setCryptoInstance($cryptoInstance);
109 // Get a fragmenter instance for later verification of serial numbers (e.g. if all are received)
110 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
112 // Set it in this handler
113 $handlerInstance->setFragmenterInstance($fragmenterInstance);
115 // Return the prepared instance
116 return $handlerInstance;
120 * Initializes the handler
124 private function initHandler () {
126 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing handler ...');
128 // Init finalPackageChunks
129 $this->finalPackageChunks = array(
130 // Array for package content
131 'content' => array(),
132 // ... and for the hashes
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,
141 $this->chunkHashes = array();
144 $this->eopChunk = array(
151 * Checks whether the hash generated from package content is the same ("valid") as given
153 * @param $chunkSplits An array from a splitted chunk
154 * @return $isValid Whether the hash is "valid"
156 private function isChunkHashValid (array $chunkSplits) {
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkSplits=' . print_r($chunkSplits, TRUE));
160 // Assert on some elements
161 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]));
162 assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]));
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);
168 $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHash=' . $chunkHash . ',isValid=' . intval($isValid));
178 * Checks whether the given serial number is valid
180 * @param $serialNumber A serial number from a chunk
181 * @return $isValid Whether the serial number is valid
183 private function isSerialNumberValid ($serialNumber) {
185 $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
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.
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.
199 * @param $chunkSplits An array from a splitted chunk
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);
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]);
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];
215 // ... and the hash as well
216 $this->finalPackageChunks['hashes'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH];
220 * Marks the final array as completed, do only this if you really have all
221 * chunks together including EOP and "hash chunk".
225 private function markFinalArrayAsCompleted () {
227 * As for now, just set the array element. If any further steps are
228 * being added, this should always be the last step.
230 $this->finalPackageChunks['is_complete'] = TRUE;
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.
238 * This method sorts 'content' and 'hashes' so both must have used the
239 * serial numbers as array indexes.
243 private function sortChunksArray () {
244 // Sort 'content' first
245 ksort($this->finalPackageChunks['content']);
248 ksort($this->finalPackageChunks['hashes']);
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).
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));
264 * Remove last element (hash chunk) from 'hashes'. This hash will never
265 * be needed, so ignore it.
267 array_pop($this->finalPackageChunks['hashes']);
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)));
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)));
276 // Verify all serial numbers
277 $this->verifyChunkSerialNumbers();
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.
287 private function verifyChunkSerialNumbers () {
288 // Reset the serial number generator
289 $this->getFragmenterInstance()->resetSerialNumber();
291 // "Walk" through all (content) chunks
292 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
293 // Get next serial number
294 $nextSerial = $this->getFragmenterInstance()->getNextHexSerialNumber();
297 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
299 // Is it not the same? Then re-request it
300 if ($serialNumber != $nextSerial) {
301 // This is invalid, so remove it
302 unset($this->finalPackageChunks['content'][$serialNumber]);
303 unset($this->finalPackageChunks['hashes'][$serialNumber]);
305 // And re-request it with valid serial number (and hash chunk)
306 $this->rerequestChunkBySerialNumber($nextSerial);
312 * Assembles and verifies ("final check") chunks back together to the
313 * original package (raw data for the start). This method should only be
314 * called AFTER the EOP and final-chunk chunk have been removed.
318 private function assembleAllChunksToPackage () {
319 // If chunkHashes is not filled, don't continue
320 assert(count($this->chunkHashes) > 0);
322 // Init raw package data string
323 $this->rawPackageData = '';
325 // That went well, so start assembling all chunks
326 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Handling ' . count($this->finalPackageChunks['content']) . ' entries ...');
327 foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
329 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ' - validating ...');
330 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE));
332 // Is this chunk valid? This should be the case
333 assert($this->isChunkHashValid(array(
334 self::CHUNK_SPLITS_INDEX_HASH => $this->finalPackageChunks['hashes'][$serialNumber],
335 self::CHUNK_SPLITS_INDEX_RAW_DATA => $content
338 // ... and is also in the hash chunk?
339 assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
341 // Verification okay, add it to the raw data
342 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($content) . ' bytes as raw package data ...');
343 $this->rawPackageData .= $content;
347 //* 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));
349 // The last chunk hash must match with the one from eopChunk[1]
350 assert($this->eopChunk[1] == $this->chunkHashes[count($this->chunkHashes) - 2]);
354 * Verifies the finally assembled raw package data by comparing it against
359 private function verifyRawPackageData () {
360 // Hash the raw package data for final verification
361 $finalHash = $this->getCryptoInstance()->hashString($this->rawPackageData, $this->eopChunk[0], FALSE);
364 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',finalHash=' . $finalHash);
365 assert($finalHash == $this->eopChunk[0]);
369 * Adds all chunks if the last one verifies as a 'final chunk'.
371 * @param $chunks An array with chunks, the last one should be a 'final'
373 * @throws FinalChunkVerificationException If the final chunk does not start with 'EOP:'
375 public function addAllChunksWithFinal (array $chunks) {
376 // Try to validate the final chunk
378 // Validate final chunk
379 $this->isValidFinalChunk($chunks);
380 } catch (AssertionException $e) {
381 // Last chunk is not valid
382 throw new FinalChunkVerificationException(array($this, $chunks, $e), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
385 // Do we have some pending chunks (no final)?
386 while (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)) {
387 // Then get it first and add it before the EOP chunks
388 array_unshift($chunks, $this->getStackerInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL));
391 // Add all chunks to the FIFO stacker
392 foreach ($chunks as $chunk) {
394 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP, $chunk);
399 * Adds all chunks and wait for more (e.g. incomplete transmission)
401 * @param $chunks An array with chunks, the last one should be a 'final'
404 public function addAllChunksWait (array $chunks) {
405 // Add all chunks to the FIFO stacker
406 foreach ($chunks as $chunk) {
408 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL, $chunk);
413 * Checks whether unhandled chunks are available
415 * @return $unhandledChunks Whether unhandled chunks are left
417 public function ifUnhandledChunksWithFinalAvailable () {
418 // Simply check if the stacker is not empty
419 $unhandledChunks = $this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP) === FALSE;
422 return $unhandledChunks;
426 * Handles available chunks by processing one-by-one (not all together,
427 * this would slow-down the whole application) with the help of an
432 public function handleAvailableChunksWithFinal () {
433 // First check if there are undhandled chunks available
434 assert($this->ifUnhandledChunksWithFinalAvailable());
436 // Get an entry from the stacker
437 $chunk = $this->getStackerInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP);
439 // Split the string with proper separator character
440 $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk);
443 * Make sure three elements are always found:
448 assert(count($chunkSplits) == 3);
450 // Is the generated hash from data same ("valid") as given hash?
451 if (!$this->isChunkHashValid($chunkSplits)) {
453 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Chunk content is not validating against given hash.');
455 // Re-request this chunk (trust the hash in index # 0)
456 $this->rerequestChunkBySplitsArray($chunkSplits);
458 // Don't process this chunk
462 // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)?
463 if (!$this->isSerialNumberValid($chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL])) {
465 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.');
467 // Re-request this chunk
468 $this->rerequestChunkBySplitsArray($chunkSplits);
470 // Don't process this chunk
475 * It is now known that (as long as the hash algorithm has no
476 * collisions) the content is the same as the sender sends it to this
479 * And also the serial number is valid (basicly) at this point. Now the
480 * chunk can be added to the final array.
482 $this->addChunkToFinalArray($chunkSplits);
484 // Is the stack now empty?
485 if ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP)) {
486 // Then mark the final array as complete
487 $this->markFinalArrayAsCompleted();
492 * Checks whether unassembled chunks are available (ready) in final array
494 * @return $unassembledChunksAvailable Whether unassembled chunks are available
496 public function ifUnassembledChunksAvailable () {
497 // For now do only check the array element 'is_complete'
498 $unassembledChunksAvailable = ($this->finalPackageChunks['is_complete'] === TRUE);
501 return $unassembledChunksAvailable;
505 * Assembles all chunks (except EOP and "hash chunk") back together to the original package data.
507 * This is done by the following steps:
509 * 1) Sort the final array with ksort(). This will bring the "hash
510 * chunk" up to the last array index and the EOP chunk to the
511 * pre-last array index
512 * 2) Assemble all chunks except two last (see above step)
513 * 3) While so, do the final check on all hashes
514 * 4) If the package is assembled back together, hash it again for
515 * the very final verification.
519 public function assembleChunksFromFinalArray () {
520 // Make sure the final array is really completed
521 assert($this->ifUnassembledChunksAvailable());
524 $this->finalPackageChunks['assemble_steps']++;
527 switch ($this->finalPackageChunks['assemble_steps']) {
528 case 1: // Sort the chunks array (the serial number shall act as a sorting key)
529 $this->sortChunksArray();
532 case 2: // Prepare the assemble by removing last two indexes
533 $this->preparePackageAssmble();
536 case 3: // Assemble all chunks back together to the original package
537 $this->assembleAllChunksToPackage();
540 case 4: // Verify the raw data by hashing it again
541 $this->verifyRawPackageData();
544 case 5: // Re-initialize handler to reset it to the old state
545 $this->initHandler();
548 default: // Invalid step found
549 self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Invalid step ' . $this->finalPackageChunks['assemble_steps'] . ' detected.');
555 * Checks whether the raw package data has been assembled back together.
556 * This can be safely assumed when rawPackageData is not empty and the
557 * collection of all chunks is FALSE (because initHandler() will reset it).
559 * @return $isRawPackageDataAvailable Whether raw package data is available
561 public function ifRawPackageDataIsAvailable () {
563 $isRawPackageDataAvailable = ((!empty($this->rawPackageData)) && (!$this->ifUnassembledChunksAvailable()));
566 return $isRawPackageDataAvailable;
570 * Handles the finally assembled raw package data by feeding it into another
571 * stacker for further decoding/processing.
575 public function handledAssembledRawPackageData () {
576 // Assert to make sure that there is raw package data available
577 assert($this->ifRawPackageDataIsAvailable());
579 // Then feed it into the next stacker
580 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_ASSEMBLED_RAW_DATA, $this->rawPackageData);
583 $this->rawPackageData = '';