From: Roland Haeder Date: Fri, 14 Mar 2014 21:13:52 +0000 (+0100) Subject: Added incomplete handling of multiple messages. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9f1e8be8d1766c91ff9f26cfb45349dadb4e3f98;p=hub.git Added incomplete handling of multiple messages. Signed-off-by: Roland Haeder --- diff --git a/application/hub/config.php b/application/hub/config.php index 40e700cfd..30d4c6246 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -294,6 +294,9 @@ $cfg->setConfigEntry('dht_stacker_class', 'FiLoStacker'); // CFG: RAW-DATA-STACKER-CLASS $cfg->setConfigEntry('raw_data_stacker_class', 'FiLoStacker'); +// CFG: MULTIPLE-MESSAGE-STACKER-CLASS +$cfg->setConfigEntry('multiple_message_stacker_class', 'FiFoStacker'); + // CFG: NODE-ANNOUNCEMENT-ANSWER-TEMPLATE-TYPE $cfg->setConfigEntry('node_announcement_answer_template_type', 'xml/answer/announcement'); diff --git a/application/hub/exceptions/stream/.htaccess b/application/hub/exceptions/stream/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/exceptions/stream/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/exceptions/stream/class_MultipleMessageSentException.php b/application/hub/exceptions/stream/class_MultipleMessageSentException.php new file mode 100644 index 000000000..e0b9d6f30 --- /dev/null +++ b/application/hub/exceptions/stream/class_MultipleMessageSentException.php @@ -0,0 +1,45 @@ + + * @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.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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class MultipleMessageSentException extends FrameworkException { + /** + * The super constructor for all exceptions + * + * @param $streamInstance An instance of a InputStreamable class + * @param $data The actual data + * @return void + */ + public function __construct (InputStreamable $streamInstance, $data) { + // Construct the message + $message = sprintf('[%s:%d] Cannot handle multiple messages. Please split them before calling this method.', + $streamInstance->__toString(), + $this->getLine() + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index 590f439f9..15b97beee 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -32,6 +32,7 @@ class BaseHubSystem extends BaseFrameworkSystem { const EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING = 0x906; const EXCEPTION_REQUEST_NOT_ACCEPTED = 0x907; const EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED = 0x908; + const EXCEPTION_MULTIPLE_MESSAGE_SENT = 0x909; // Message status codes const MESSAGE_STATUS_CODE_OKAY = 'OKAY'; diff --git a/application/hub/main/package/assembler/class_PackageAssembler.php b/application/hub/main/package/assembler/class_PackageAssembler.php index 0eec7aff0..99e1a0c29 100644 --- a/application/hub/main/package/assembler/class_PackageAssembler.php +++ b/application/hub/main/package/assembler/class_PackageAssembler.php @@ -68,6 +68,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable // Set handler instance $assemblerInstance->setHandlerInstance($handlerInstance); + // Get stacker instance + $stackerInstance = ObjectFactory::createObjectByConfiguredName('multiple_message_stacker_class'); + + // And add it + $assemblerInstance->setStackerInstance($stackerInstance); + // Return the prepared instance return $assemblerInstance; } @@ -197,7 +203,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable // 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 + } 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. + */ + } // Init fake array $packageContent = array( diff --git a/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php index dfce170c4..df633482e 100644 --- a/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php +++ b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php @@ -58,6 +58,7 @@ class RawDataInputStream extends BaseStream implements InputStreamable { * @todo Do we need to do something more here? * @throws Base64EncodingModuloException If the data's length modulo 4 is not zero * @throws Base64EncodingBadException If the data contains characters which are not in the "alphabet" of BASE64 messages. + * @throws MultipleMessageSentException If the sender has sent two messages and both end up here */ public function streamData ($data) { // Debug message @@ -66,6 +67,15 @@ class RawDataInputStream extends BaseStream implements InputStreamable { // Do we have start and end marker again? assert($this->ifStartEndMarkersSet($data)); + // Count of start and end markers must be the same + assert(substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) == substr_count($data, BaseRawDataHandler::STREAM_END_MARKER)); + + // Check if more than two start markers exist and if so, split it. + if (substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) > 1) { + // Please do it outside this method + throw new MultipleMessageSentException(array($this, $data), BaseHubSystem::EXCEPTION_MULTIPLE_MESSAGE_SENT); + } // END - if + // Remove both $data = substr($data, strlen(BaseRawDataHandler::STREAM_START_MARKER), -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER));