]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/class_NetworkPackage.php
Corrected assert, added debug line
[hub.git] / application / hub / main / package / class_NetworkPackage.php
index b209bd0aac664ae13b9269d7631d195263c072f4..ba893bd89869f120708c79d1d332b42208568940 100644 (file)
@@ -97,6 +97,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        const PACKAGE_DATA_SENDER    = 'sender';
        const PACKAGE_DATA_RECIPIENT = 'recipient';
+       const PACKAGE_DATA_PROTOCOL  = 'protocol';
        const PACKAGE_DATA_CONTENT   = 'content';
        const PACKAGE_DATA_STATUS    = 'status';
        const PACKAGE_DATA_SIGNATURE = 'signature';
@@ -107,6 +108,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const PACKAGE_STATUS_NEW     = 'new';
        const PACKAGE_STATUS_FAILED  = 'failed';
        const PACKAGE_STATUS_DECODED = 'decoded';
+       const PACKAGE_STATUS_FAKED   = 'faked';
 
        /**
         * Constants for message data array
@@ -114,6 +116,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const MESSAGE_ARRAY_DATA = 'message_data';
        const MESSAGE_ARRAY_TYPE = 'message_type';
 
+       /**
+        * Generic answer status field
+        */
+
        /**
         * Tags separator
         */
@@ -125,15 +131,25 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const PACKAGE_DATA_SEPARATOR = '#';
 
        /**
-        * Network target (alias): 'upper hubs'
+        * Separator for more than one recipient
+        */
+       const PACKAGE_RECIPIENT_SEPARATOR = ':';
+
+       /**
+        * Network target (alias): 'upper nodes'
         */
-       const NETWORK_TARGET_UPPER_HUBS = 'upper';
+       const NETWORK_TARGET_UPPER = 'upper';
 
        /**
         * Network target (alias): 'self'
         */
        const NETWORK_TARGET_SELF = 'self';
 
+       /**
+        * Network target (alias): 'dht'
+        */
+       const NETWORK_TARGET_DHT = 'dht';
+
        /**
         * TCP package size in bytes
         */
@@ -200,6 +216,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        const STACKER_NAME_BACK_BUFFER = 'package_backbuffer';
 
+       /**************************************************************************
+        *                            Protocol names                              *
+        **************************************************************************/
+       const PROTOCOL_TCP = 'TCP';
+       const PROTOCOL_UDP = 'UDP';
+
        /**
         * Protected constructor
         *
@@ -236,17 +258,15 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                // Init all stacker
                $packageInstance->initStackers();
 
-               // Get a visitor instance for speeding up things
+               // Get a visitor instance for speeding up things and set it
                $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class', array($packageInstance));
-
-               // Set it in this package
                $packageInstance->setVisitorInstance($visitorInstance);
 
-               // Get crypto instance and set it in this package
+               // Get crypto instance and set it, too
                $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
                $packageInstance->setCryptoInstance($cryptoInstance);
 
-               // Get a singleton package assembler instance from factory and set it here
+               // Get a singleton package assembler instance from factory and set it here, too
                $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance);
                $packageInstance->setAssemblerInstance($assemblerInstance);
 
@@ -260,7 +280,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @param       $forceReInit    Whether to force reinitialization of all stacks
         * @return      void
         */
-       protected function initStackers ($forceReInit = false) {
+       protected function initStackers ($forceReInit = FALSE) {
                // Initialize all
                foreach (
                        array(
@@ -275,6 +295,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                                self::STACKER_NAME_BACK_BUFFER
                        ) as $stackerName) {
                                // Init this stacker
+                               //* DEBUG: */ print(__METHOD__ . ': stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit) . PHP_EOL);
                                $this->getStackerInstance()->initStacker($stackerName, $forceReInit);
                } // END - foreach
        }
@@ -287,7 +308,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        private function getHashFromContent ($content) {
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
 
                // Create the hash
                // @TODO crc32() is very weak, but it needs to be fast
@@ -299,6 +320,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        $this->getCompressorInstance()->getCompressorExtension()
                );
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
+
                // And return it
                return $hash;
        }
@@ -352,13 +376,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        /**
         * "Getter" for hash from given content and sender's session id
         *
-        * @param       $decodedContent         Decoded package content
+        * @param       $decodedContent         Raw package content
         * @param       $sessionId                      Session id of the sender
         * @return      $hash                           Hash for given package content
         */
        public function getHashFromContentSessionId (array $decodedContent, $sessionId) {
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]);
 
                // Create the hash
                // @TODO crc32() is very weak, but it needs to be fast
@@ -385,6 +409,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @return      void
         */
        private function declareRawPackageData (array $packageData) {
+               // Make sure the required field is there
+               assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
+
                /*
                 * We need to disover every recipient, just in case we have a
                 * multi-recipient entry like 'upper' is. 'all' may be a not so good
@@ -407,6 +434,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        // Get current entry
                        $currentRecipient = $iteratorInstance->current();
 
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]);
+
                        // Set the recipient
                        $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
 
@@ -414,7 +444,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
 
                        // Debug message
-                       $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package declared for recipient ' . $currentRecipient);
 
                        // Skip to next entry
                        $iteratorInstance->next();
@@ -453,50 +483,50 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
 
                // Now discover the right protocol
-               $socketResource = $discoveryInstance->discoverSocket($packageData);
+               $socketResource = $discoveryInstance->discoverSocket($packageData, BaseConnectionHelper::CONNECTION_TYPE_OUTGOING);
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
 
                // We have to put this socket in our registry, so get an instance
-               $registryInstance = SocketRegistry::createSocketRegistry();
+               $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
 
                // Get the listener from registry
                $helperInstance = Registry::getRegistry()->getInstance('connection');
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: stateInstance=' . $helperInstance->getStateInstance());
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: stateInstance=' . $helperInstance->getStateInstance());
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
 
                // Is it not there?
                if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
                        // Debug message
-                       $this->debugOutput('PACKAGE: Registering socket ' . $socketResource . ' ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Registering socket ' . $socketResource . ' ...');
 
                        // Then register it
                        $registryInstance->registerSocket($helperInstance, $socketResource, $packageData);
                } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) {
                        // Is not connected, then we cannot send
-                       $this->debugOutput('PACKAGE: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.');
 
                        // Shutdown the socket
                        $this->shutdownSocket($socketResource);
                }
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
 
                // Make sure the connection is up
                $helperInstance->getStateInstance()->validatePeerStateConnected();
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
 
                // Enqueue it again on the out-going queue, the connection is up and working at this point
                $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
        }
 
        /**
@@ -510,7 +540,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $sentBytes = 0;
 
                // Get the right connection instance
-               $helperInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
+               $helperInstance = SocketRegistryFactory::createSocketRegistryInstance()->getHandlerInstanceFromPackageData($packageData);
 
                // Is this connection still alive?
                if ($helperInstance->isShuttedDown()) {
@@ -534,8 +564,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @return      $signature      Signature as BASE64-encoded string
         */
        private function generatePackageSignature ($content, $senderId) {
-               // ash content and sender id together, use md5() as last algo
-               $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getNodeId(), false));
+               // Hash content and sender id together, use md5() as last algo
+               $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getPrivateKey(), false));
 
                // Encrypt the content again with the hash as a key
                $encryptedContent = $this->getCryptoInstance()->encryptString($content, $hash);
@@ -561,7 +591,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
 
                // Is it the same?
                //$isSignatureValid = 
-               die(__METHOD__.': signature='.$signature.chr(10).',decodedArray='.print_r($decodedArray,true));
+               exit(__METHOD__.': signature='.$signature.chr(10).',decodedArray='.print_r($decodedArray,true));
        }
 
        /**
@@ -569,18 +599,20 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * from given helper's template instance and pushing it on the 'undeclared'
         * stack.
         *
-        * @param       $helperInstance         An instance of a HelpableHub class
+        * @param       $helperInstance         An instance of a Helper class
+        * @param       $protocol                       Name of used protocol (TCP/UDP)
         * @return      void
         */
-       public function enqueueRawDataFromTemplate (HelpableHub $helperInstance) {
+       public function enqueueRawDataFromTemplate (Helper $helperInstance, $protocolName) {
                // Get the raw content ...
                $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('content(' . strlen($content) . ')=' . $content);
 
                // ... and compress it
                $content = $this->getCompressorInstance()->compressStream($content);
 
                // Add magic in front of it and hash behind it, including BASE64 encoding
-               $content = sprintf(self::PACKAGE_MASK,
+               $packageContent = sprintf(self::PACKAGE_MASK,
                        // 1.) Compressor's extension
                        $this->getCompressorInstance()->getCompressorExtension(),
                        // - separator
@@ -601,9 +633,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
                        self::PACKAGE_DATA_SENDER    => $this->getSessionId(),
                        self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
-                       self::PACKAGE_DATA_CONTENT   => $content,
+                       self::PACKAGE_DATA_PROTOCOL  => $protocolName,
+                       self::PACKAGE_DATA_CONTENT   => $packageContent,
                        self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_NEW,
-                       self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($content, $this->getSessionId())
+                       self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($packageContent, $this->getSessionId())
                ));
        }
 
@@ -668,13 +701,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * Now there are for sure packages to deliver, so start with the first
                 * one.
                 */
-               $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED);
+               $packageData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
 
                // Declare the raw package data for delivery
                $this->declareRawPackageData($packageData);
-
-               // And remove it finally
-               $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
        }
 
        /**
@@ -688,13 +718,17 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                // Sanity check if we have packages declared
                if (!$this->isPackageDeclared()) {
                        // This is not fatal but should be avoided
-                       $this->debugOutput('PACKAGE: No package has been declared, but ' . __METHOD__ . ' has been called!');
+                       self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package has been declared, but ' . __METHOD__ . ' has been called!');
                        return;
                } // END - if
 
                // Get the package
                $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
 
+               // Assert on it
+               assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
+
+               // Try to deliver the package
                try {
                        // And try to send it
                        $this->deliverRawPackageData($packageData);
@@ -703,7 +737,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
                } catch (InvalidStateException $e) {
                        // The state is not excepected (shall be 'connected')
-                       $this->debugOutput('PACKAGE: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
+                       self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
 
                        // Mark the package with status failed
                        $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED);
@@ -722,7 +756,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                // Sanity check if we have packages waiting for delivery
                if (!$this->isPackageWaitingForDelivery()) {
                        // This is not fatal but should be avoided
-                       $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
                        return;
                } // END - if
 
@@ -737,7 +771,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
                } catch (InvalidSocketException $e) {
                        // Output exception message
-                       $this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());
+                       self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package was not delivered: ' . $e->getMessage());
 
                        // Mark package as failed
                        $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED);
@@ -753,7 +787,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         *
         * @return      $isPending      Whether decoded raw data is pending
         */
-       private function isDecodedDataPending () {
+       private function isRawDataPending () {
                // Just return whether the stack is not empty
                $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING));
 
@@ -772,7 +806,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $poolInstance->accept($this->getVisitorInstance());
 
                // Check for new data arrival
-               $hasArrived = $this->isDecodedDataPending();
+               $hasArrived = $this->isRawDataPending();
 
                // Return the status
                return $hasArrived;
@@ -790,14 +824,14 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * This method should only be called if decoded raw data is pending,
                 * so check it again.
                 */
-               if (!$this->isDecodedDataPending()) {
+               if (!$this->isRawDataPending()) {
                        // This is not fatal but should be avoided
                        // @TODO Add some logging here
                        return;
                } // END - if
 
                // Very noisy debug message:
-               /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
 
                // "Pop" the next entry (the same array again) from the stack
                $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING);
@@ -805,7 +839,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                // Make sure both array elements are there
                assert(
                        (is_array($decodedData)) &&
-                       (isset($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA])) &&
+                       (isset($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA])) &&
                        (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
                );
 
@@ -813,13 +847,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we
                 * only want to handle unhandled packages here.
                 */
-               /* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . BaseRawDataHandler::SOCKET_ERROR_UNHANDLED . ')');
                assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED);
 
                // Remove the last chunk SEPARATOR (because it is being added and we don't need it)
-               if (substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
+               if (substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
                        // It is there and should be removed
-                       $decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], 0, -1);
+                       $decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], 0, -1);
                } // END - if
 
                // This package is "handled" and can be pushed on the next stack
@@ -832,15 +866,15 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @param       $handlerInstance        An instance of a Networkable class
         * @return      void
         */
-       public function addDecodedDataToIncomingStack (Networkable $handlerInstance) {
+       public function addRawDataToIncomingStack (Networkable $handlerInstance) {
                /*
                 * Get the decoded data from the handler, this is an array with
-                * 'decoded_data' and 'error_code' as elements.
+                * 'raw_data' and 'error_code' as elements.
                 */
-               $decodedData = $handlerInstance->getNextDecodedData();
+               $decodedData = $handlerInstance->getNextRawData();
 
                // Very noisy debug message:
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
 
                // And push it on our stack
                $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData);
@@ -851,7 +885,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         *
         * @return      $isHandled      Whether incoming decoded data is handled
         */
-       public function isIncomingDecodedDataHandled () {
+       public function isIncomingRawDataHandled () {
                // Determine if the stack is not empty
                $isHandled = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED));
 
@@ -892,7 +926,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        public function assembleDecodedDataToPackage () {
                // Make sure the raw decoded package data is handled
-               assert($this->isIncomingDecodedDataHandled());
+               assert($this->isIncomingRawDataHandled());
 
                // Get current package content (an array with two elements; see handleIncomingDecodedData() for details)
                $packageContent = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED);
@@ -915,13 +949,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        public function accept (Visitor $visitorInstance) {
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - START');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START');
 
                // Visit the package
                $visitorInstance->visitNetworkPackage($this);
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
        }
 
        /**
@@ -934,7 +968,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $this->initStackers(true);
 
                // Debug message
-               /* DEBUG: */ $this->debugOutput('PACKAGE: All stacker have been re-initialized.');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: All stacker have been re-initialized.');
        }
 
        /**
@@ -977,7 +1011,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * @todo Unsupported feature of "signed" messages commented out
                if (!$this->isPackageSignatureValid($decodedArray)) {
                        // Is not valid, so throw an exception here
-                       die('INVALID SIG! UNDER CONSTRUCTION!' . chr(10));
+                       exit(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10));
                } // END - if
                */
 
@@ -1004,7 +1038,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @return      void
         * @throws      InvalidDataChecksumException    If the checksum doesn't match
         */
-       public function handleDecodedData (array $decodedData) {
+       public function handleRawData (array $decodedData) {
                /*
                 * "Decode" the package's content by a simple explode() call, for
                 * details of the array elements, see comments for constant
@@ -1078,6 +1112,41 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 */
                $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this);
        }
+
+       /**
+        * Checks whether a processed message is pending for "interpretation"
+        *
+        * @return      $isPending      Whether a processed message is pending
+        */
+       public function isProcessedMessagePending () {
+               // Check it
+               $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE));
+
+               // Return it
+               return $isPending;
+       }
+
+       /**
+        * Handle processed messages by "interpreting" the 'message_type' element
+        *
+        * @return      void
+        */
+       public function handleProcessedMessage () {
+               // Get it from the stacker, it is the full array with the processed message
+               $messageArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE);
+
+               // Add type for later easier handling
+               $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE];
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: messageArray=' . print_r($messageArray, true));
+
+               // Create a handler instance from given message type
+               $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]);
+
+               // Handle message data
+               $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this);
+       }
 }
 
 // [EOF]