]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/assembler/class_PackageAssembler.php
Again contunued on 'hub' project:
[hub.git] / application / hub / main / package / assembler / class_PackageAssembler.php
index f90f81c7a88fc311a109fb88082788640d84afb4..b53cc50ff07f16cc48b4c127b4aadc19af1b19ab 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class PackageAssembler extends BaseHubSystem implements Assembler, Registerable {
+       /**
+        * Pending data
+        */
+       private $pendingData = '';
+
        /**
         * Protected constructor
         *
@@ -36,16 +41,36 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
        /**
         * Creates an instance of this class
         *
+        * @param       $packageInstance        An instance of a Receivable class
         * @return      $assemblerInstance      An instance of an Assembler class
         */
-       public static final function createPackageAssembler () {
+       public static final function createPackageAssembler (Receivable $packageInstance) {
                // Get new instance
                $assemblerInstance = new PackageAssembler();
 
+               // Set package instance here
+               $assemblerInstance->setPackageInstance($packageInstance);
+
                // Return the prepared instance
                return $assemblerInstance;
        }
 
+       /**
+        * Checks whether the input buffer (stacker to be more preceise) is empty.
+        *
+        * @return      $isInputBufferEmpty             Whether the input buffer is empty
+        */
+       private function ifInputBufferIsEmpty () {
+               // Check it
+               $isInputBufferEmpty = $this->getPackageInstance()->getStackerInstance()->isStackEmpty(NetworkPackage::STACKER_NAME_DECODED_HANDLED);
+
+               // Debug message
+               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE-ASSEMBLER: isInputBufferEmpty=' . intval($isInputBufferEmpty));
+
+               // Return it
+               return $isInputBufferEmpty;
+       }
+
        /**
         * Assembles the content from $packageContent. This method does only
         * initialize the whole process by creating a call-back which will then
@@ -80,6 +105,10 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                call_user_func(array($this, $methodName), $packageContent);
        }
 
+       /**************************************************************************
+        *                 Call-back methods for above method                     *
+        **************************************************************************/
+
        /**
         * Call-back handler to handle unhandled packages. This method "explodes"
         * the string with the chunk separator from PackageFragmenter class, does
@@ -91,24 +120,69 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
         * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
         */
        private function handlePackageByUnhandledPackage (array $packageContent) {
-               /*
-                * "explode" the string from 'decoded_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_DECODED_DATA]);
-
-               // Validate final chunk
-               if (!$this->isValidFinalChunk($chunks)) {
-                       // Last chunk is not valid
-                       throw new FinalChunkVerificationException(array($this, $chunks), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
-               } // END - if
+               // Check for some conditions
+               if (!$this->ifInputBufferIsEmpty()) {
+                       // Last chunk is not valid, so wait for more
+                       $this->pendingData .= $packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA];
+
+                       // Debug message
+                       /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]) . ' bytes)');
+               } else {
+                       // Debug message
+                       //* NOISY-DEBUG */ $this->debugOutput('packageContent=' . print_r($packageContent,true) . ',chunks='.print_r($chunks,true));
+
+                       /*
+                        * "explode" the string from 'decoded_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_DECODED_DATA]);
+
+                       // Now get a chunk handler instance
+                       $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
+
+                       // Add all chunks because the last final chunk is found
+                       $handlerInstance->addAllChunksWithFinal($chunks);
+               }
+       }
+
+       /**
+        * Checks whether the assembler's pending data is empty which means it has
+        * no pending data left for handling ... ;-)
+        *
+        * @return      $ifPendingDataIsEmpty   Whether pending data is empty
+        */
+       public function isPendingDataEmpty () {
+               // A simbple check
+               $ifPendingDataIsEmpty = empty($this->pendingData);
+
+               // Return it
+               return $ifPendingDataIsEmpty;
+       }
+
+       /**
+        * Handles the assembler's pending data
+        *
+        * @return      void
+        */
+       public function handlePendingData () {
+               // Assert on condition
+               assert(!$this->isPendingDataEmpty());
+
+               // Init fake array
+               $packageContent = array(
+                       BaseRawDataHandler::PACKAGE_DECODED_DATA => $this->pendingData,
+                       BaseRawDataHandler::PACKAGE_ERROR_CODE   => BaseRawDataHandler::SOCKET_ERROR_UNHANDLED
+               );
+
+               // Clear pending data
+               $this->pendingData = '';
 
-               // Now get a chunk handler instance
-               $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
+               // Debug message
+               /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]) . ' bytes has been received.');
 
-               // Add all chunks because the last final chunk is found
-               $handlerInstance->addAllChunksWithFinal($chunks);
+               // Call the real handler method
+               $this->handlePackageByUnhandledPackage($packageContent);
        }
 }