]> git.mxchange.org Git - hub.git/commitdiff
Added incomplete handling of multiple messages.
authorRoland Haeder <roland@mxchange.org>
Fri, 14 Mar 2014 21:13:52 +0000 (22:13 +0100)
committerRoland Haeder <roland@mxchange.org>
Fri, 14 Mar 2014 21:13:52 +0000 (22:13 +0100)
Signed-off-by: Roland Haeder <roland@mxchange.org>
application/hub/config.php
application/hub/exceptions/stream/.htaccess [new file with mode: 0644]
application/hub/exceptions/stream/class_MultipleMessageSentException.php [new file with mode: 0644]
application/hub/main/class_BaseHubSystem.php
application/hub/main/package/assembler/class_PackageAssembler.php
application/hub/main/streams/raw_data/input/class_RawDataInputStream.php

index 40e700cfd69b36a1e4cf55f181dbca4639344a13..30d4c62467ed87d235d85981c0fec5fc372fa5a4 100644 (file)
@@ -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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..e0b9d6f
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+/**
+ * This exception is thrown when multiple messages are found in in-buffer.
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
index 590f439f9865f0da54cbc39edc7160601d9ae603..15b97beeedd447fd259e57e8daca85352cb0fbde 100644 (file)
@@ -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';
index 0eec7aff05545a31b66cf614aed90c188b0d73c1..99e1a0c29c1e7804f71995f9a2a54f86771e8c50 100644 (file)
@@ -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(
index dfce170c4ab1504abe35b0d7fa7ba5ce42661635..df633482e2a175557199dd3bef9a29b2bb55b220 100644 (file)
@@ -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));