From d85141c636802a0314983aa8f610542ca9c58218 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 14 Mar 2014 22:35:29 +0100 Subject: [PATCH] Added support for visiting package assembler which will e.g. clear pending data on shut down stockets or handle multiple messages. Signed-off-by: Roland Haeder --- .../class_MultipleMessageSentException.php | 8 ++-- .../assembler/class_PackageAssembler.php | 46 ++++++++++++++++++- .../hub/main/package/class_NetworkPackage.php | 7 ++- .../socket/class_ShutdownSocketVisitor.php | 11 +++++ 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/application/hub/exceptions/stream/class_MultipleMessageSentException.php b/application/hub/exceptions/stream/class_MultipleMessageSentException.php index e0b9d6f30..bebe02b07 100644 --- a/application/hub/exceptions/stream/class_MultipleMessageSentException.php +++ b/application/hub/exceptions/stream/class_MultipleMessageSentException.php @@ -25,14 +25,14 @@ class MultipleMessageSentException extends FrameworkException { /** * The super constructor for all exceptions * - * @param $streamInstance An instance of a InputStreamable class - * @param $data The actual data + * @param $messageArray Error message array + * @param $code Error code * @return void */ - public function __construct (InputStreamable $streamInstance, $data) { + public function __construct (array $messageArray, $code) { // Construct the message $message = sprintf('[%s:%d] Cannot handle multiple messages. Please split them before calling this method.', - $streamInstance->__toString(), + $messageArray[0]->__toString(), $this->getLine() ); diff --git a/application/hub/main/package/assembler/class_PackageAssembler.php b/application/hub/main/package/assembler/class_PackageAssembler.php index 99e1a0c29..2325b9f99 100644 --- a/application/hub/main/package/assembler/class_PackageAssembler.php +++ b/application/hub/main/package/assembler/class_PackageAssembler.php @@ -22,7 +22,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -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 */ @@ -71,6 +76,9 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable // 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); @@ -208,6 +216,19 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable * 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 @@ -217,7 +238,7 @@ 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.'); @@ -239,6 +260,27 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable //* 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] diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index b1b345928..faf9f1703 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -1060,13 +1060,16 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ public function accept (Visitor $visitorInstance) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START'); // Visit the package $visitorInstance->visitNetworkPackage($this); + // Then visit the assembler to handle multiple packages + $this->getAssemblerInstance()->accept($visitorInstance); + // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); } /** diff --git a/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php b/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php index 9882d6899..fbcd19c49 100644 --- a/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php +++ b/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php @@ -80,6 +80,17 @@ class ShutdownSocketVisitor extends BaseVisitor implements Visitor { // Just call it back $packageInstance->clearAllStacks(); } + + /** + * "Visit" the assembler instance. This shall clear any pending data + * + * @param $assemblerInstance An instance of a Assembler class + * @return void + */ + public function visitAssembler (Assembler $assemblerInstance) { + // Clear pending data + $assemblerInstance->clearPendingData(); + } } // [EOF] -- 2.39.5