]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/assembler/class_PackageAssembler.php
Noisy debug line added + check if start and end markers are set.
[hub.git] / application / hub / main / package / assembler / class_PackageAssembler.php
index 2aed4c154ba000cc895913ec15fbdcd53b22c9cc..0eec7aff05545a31b66cf614aed90c188b0d73c1 100644 (file)
@@ -3,11 +3,11 @@
  * A PackageAssembler class to assemble a package content stream fragemented
  * by PackageFragmenter back to a raw package data array.
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
  * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,6 +28,11 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
         */
        private $pendingData = '';
 
+       /**
+        * Private call-back methods
+        */
+       private $callbacks = array();
+
        /**
         * Protected constructor
         *
@@ -57,6 +62,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                // And set it
                $assemblerInstance->setInputStreamInstance($streamInstance);
 
+               // Now get a chunk handler instance
+               $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
+
+               // Set handler instance
+               $assemblerInstance->setHandlerInstance($handlerInstance);
+
                // Return the prepared instance
                return $assemblerInstance;
        }
@@ -71,7 +82,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                $isInputBufferEmpty = $this->getPackageInstance()->getStackerInstance()->isStackEmpty(NetworkPackage::STACKER_NAME_DECODED_HANDLED);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: isInputBufferEmpty=' . intval($isInputBufferEmpty));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': isInputBufferEmpty=' . intval($isInputBufferEmpty));
 
                // Return it
                return $isInputBufferEmpty;
@@ -113,16 +124,16 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                );
 
                // Construct call-back name from package error code
-               $methodName = 'handlePackageBy' . $this->convertToClassName($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]);
+               $this->callbacks[$packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]] = 'handlePackageBy' . $this->convertToClassName($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]);
 
                // Abort if the call-back method is not there
-               if (!method_exists($this, $methodName)) {
+               if (!method_exists($this, $this->callbacks[$packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]])) {
                        // Throw an exception
-                       throw new UnsupportedPackageCodeHandlerException(array($this, $methodName, $packageContent), BaseListener::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
+                       throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]], $packageContent), BaseListener::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
                } // END - if
 
                // Call it back
-               call_user_func(array($this, $methodName), $packageContent);
+               call_user_func(array($this, $this->callbacks[$packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]]), $packageContent);
        }
 
        /**************************************************************************
@@ -130,18 +141,17 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
         **************************************************************************/
 
        /**
-        * Call-back handler to handle unhandled packages. This method "explodes"
-        * the string with the chunk separator from PackageFragmenter class, does
-        * some low checks on it and feeds it into another queue for verification
-        * and re-request for bad chunks.
+        * Call-back handler to handle unhandled package data. This method
+        * "explodes" the string with the chunk separator from PackageFragmenter
+        * class, does some low checks on it and feeds it into another queue for
+        * verification and re-request for bad chunks.
         *
         * @param       $packageContent         An array with two elements: 'raw_data' and 'error_code'
         * @return      void
-        * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
         */
        private function handlePackageByUnhandledPackage (array $packageContent) {
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: packageData[' . BaseRawDataHandler::PACKAGE_RAW_DATA . ']=' . $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ']: packageData[' . BaseRawDataHandler::PACKAGE_RAW_DATA . ']=' . $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]);
 
                // Check for some conditions
                if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageContent))) {
@@ -149,23 +159,10 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                        $this->pendingData .= $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA];
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes)');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Partial data received. Waiting for more ... ( ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes)');
                } else {
                        // Debug message
-                       //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('packageContent=' . print_r($packageContent,true) . ',chunks='.print_r($chunks,true));
-
-                       /*
-                        * "explode" the string from 'raw_data' with chunk separator to
-                        * get an array of chunks. These chunks must then be verified by
-                        * their checksums. Also the final chunk must be handled.
-                        */
-                       $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]);
-
-                       // Now get a chunk handler instance
-                       $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
-
-                       // Add all chunks because the last final chunk is found
-                       $handlerInstance->addAllChunksWithFinal($chunks);
+                       /* DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': packageContent=' . print_r($packageContent, TRUE) . ',chunks='.print_r($chunks, TRUE));
                }
        }
 
@@ -189,11 +186,18 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
         * @return      void
         */
        public function handlePendingData () {
+               // Debug output
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Going to decode ' . strlen($this->pendingData) . ' Bytes of pending data. pendingData=' . $this->pendingData);
+
                // Assert on condition
                assert(!$this->isPendingDataEmpty());
 
-               // Debug output
-               /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE-ASSEMBLER: Going to decode ' . strlen($this->pendingData) . ' Bytes of pending data. pendingData=' . $this->pendingData);
+               // No markers set?
+               if (!$this->ifStartEndMarkersSet($this->pendingData)) {
+                       // This will cause an assertition in next call, so simply wait for more data
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Pending data of ' . strlen($this->pendingData) . ' Bytes are incomplete, waiting for more ...');
+                       return;
+               } // END - if
 
                // Init fake array
                $packageContent = array(
@@ -205,10 +209,24 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                $this->pendingData = '';
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes has been received.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Last block of partial data received. A total of ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes has been received.');
+
+               // Make sure last CHUNK_SEPARATOR is not there
+               if (substr($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
+                       // Remove it
+                       $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA] = substr($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA], 0, -1);
+               } // END - if
+
+               /*
+                * "explode" the string from 'raw_data' with chunk separator to
+                * get an array of chunks. These chunks must then be verified by
+                * their checksums. Also the final chunk must be handled.
+                */
+               $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]);
 
-               // Call the real handler method
-               $this->handlePackageByUnhandledPackage($packageContent);
+               // Add all chunks because the last final chunk is found
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Going to add ' . count($chunks) . ' to chunk handler ...');
+               $this->getHandlerInstance()->addAllChunksWithFinal($chunks);
        }
 }