]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/assembler/class_PackageAssembler.php
Added support for visiting package assembler which will e.g. clear pending data on...
[hub.git] / application / hub / main / package / assembler / class_PackageAssembler.php
index e737c8e42adb26bc4d0393613d69ac8fc6ae9fce..2325b9f99d06e0cbf5c1261d44eeb072585764ce 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class PackageAssembler extends BaseHubSystem implements Assembler, Registerable {
+class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, Visitable {
+       /**
+        * Name for stacker holding raw data of multiple messages
+        */
+       const STACKER_NAME_MULTIPLE_MESSAGE = 'multiple_message';
+
        /**
         * Pending data
         */
@@ -68,6 +73,15 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                // Set handler instance
                $assemblerInstance->setHandlerInstance($handlerInstance);
 
+               // Get stacker instance
+               $stackerInstance = ObjectFactory::createObjectByConfiguredName('multiple_message_stacker_class');
+
+               // Initialize the only one stack
+               $stackerInstance->initStack(self::STACKER_NAME_MULTIPLE_MESSAGE);
+
+               // And add it
+               $assemblerInstance->setStackerInstance($stackerInstance);
+
                // Return the prepared instance
                return $assemblerInstance;
        }
@@ -159,7 +173,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                        $this->pendingData .= $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA];
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': 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
                        /* DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': packageContent=' . print_r($packageContent, TRUE) . ',chunks='.print_r($chunks, TRUE));
@@ -187,11 +201,36 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
         */
        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);
+               //* 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());
 
+               // 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;
+               } elseif (substr_count($this->pendingData, BaseRawDataHandler::STREAM_START_MARKER) > 1) {
+                       /*
+                        * Multiple messages found, so split off first message as the input
+                        * stream can only handle one message per time.
+                        */
+                       foreach (explode(BaseRawDataHandler::STREAM_START_MARKER, $this->pendingData) as $message) {
+                               // Prepend start marker again as it is needed to decode the message.
+                               $message = BaseRawDataHandler::STREAM_START_MARKER . $message;
+
+                               // Push it on stack
+                               $this->getStackerInstance()->pushNamed(self::STACKER_NAME_MULTIPLE_MESSAGE, $message);
+                       } // END - foreach
+
+                       // Clear pending data
+                       $this->clearPendingData();
+
+                       // ... and exit here
+                       return;
+               }
+
                // Init fake array
                $packageContent = array(
                        BaseRawDataHandler::PACKAGE_RAW_DATA   => $this->getInputStreamInstance()->streamData($this->pendingData),
@@ -199,10 +238,10 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                );
 
                // Clear pending data
-               $this->pendingData = '';
+               $this->clearPendingData();
 
                // Debug message
-               /* 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.');
+               //* 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) {
@@ -218,9 +257,30 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable
                $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]);
 
                // 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 ...');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER[' . __METHOD__ . ':' . __LINE__ . ': Going to add ' . count($chunks) . ' to chunk handler ...');
                $this->getHandlerInstance()->addAllChunksWithFinal($chunks);
        }
+
+       /**
+        * Accepts the visitor to process the visit "request"
+        *
+        * @param       $visitorInstance        An instance of a Visitor class
+        * @return      void
+        */
+       public function accept (Visitor $visitorInstance) {
+               // Visit the assembler
+               $visitorInstance->visitAssembler($this);
+       }
+
+       /**
+        * Clears pending data
+        *
+        * @return      void
+        */
+       public function clearPendingData () {
+               // Clear it
+               $this->pendingData = '';
+       }
 }
 
 // [EOF]