* 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
*/
// 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;
}
// 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),
);
// 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__ . ': 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]