]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/class_NetworkPackage.php
Used factory instead of direct class
[hub.git] / application / hub / main / package / class_NetworkPackage.php
index fad98f9d25609197c68ebb9c2d13df69ce8aac01..3a5b5caeb00f2a085f5150f237ceaf33bfa2cd34 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';
@@ -108,6 +109,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const PACKAGE_STATUS_FAILED  = 'failed';
        const PACKAGE_STATUS_DECODED = 'decoded';
 
+       /**
+        * Constants for message data array
+        */
+       const MESSAGE_ARRAY_DATA = 'message_data';
+       const MESSAGE_ARRAY_TYPE = 'message_type';
+
        /**
         * Tags separator
         */
@@ -118,6 +125,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        const PACKAGE_DATA_SEPARATOR = '#';
 
+       /**
+        * Separator for more than one recipient
+        */
+       const PACKAGE_RECIPIENT_SEPARATOR = ':';
+
        /**
         * Network target (alias): 'upper hubs'
         */
@@ -401,6 +413,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        // Get current entry
                        $currentRecipient = $iteratorInstance->current();
 
+                       // Debug message
+                       $this->debugOutput('NETWORK-PACKAGE: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]);
+
                        // Set the recipient
                        $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
 
@@ -408,7 +423,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);
+                       $this->debugOutput('NETWORK-PACKAGE: Package declared for recipient ' . $currentRecipient);
 
                        // Skip to next entry
                        $iteratorInstance->next();
@@ -450,47 +465,47 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $socketResource = $discoveryInstance->discoverSocket($packageData);
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
+               //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: 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: */ $this->debugOutput('NETWORK-PACKAGE: stateInstance=' . $helperInstance->getStateInstance());
+               //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: 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 . ' ...');
+                       $this->debugOutput('NETWORK-PACKAGE: 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.');
+                       $this->debugOutput('NETWORK-PACKAGE: 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: */ $this->debugOutput('NETWORK-PACKAGE: 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: */ $this->debugOutput('NETWORK-PACKAGE: 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: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
        }
 
        /**
@@ -504,7 +519,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()) {
@@ -564,9 +579,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * stack.
         *
         * @param       $helperInstance         An instance of a HelpableHub class
+        * @param       $protocol                       Name of used protocol (TCP/UDP)
         * @return      void
         */
-       public function enqueueRawDataFromTemplate (HelpableHub $helperInstance) {
+       public function enqueueRawDataFromTemplate (HelpableHub $helperInstance, $protocolName) {
                // Get the raw content ...
                $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
 
@@ -595,6 +611,7 @@ 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_PROTOCOL  => $protocolName,
                        self::PACKAGE_DATA_CONTENT   => $content,
                        self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_NEW,
                        self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($content, $this->getSessionId())
@@ -682,7 +699,7 @@ 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!');
+                       $this->debugOutput('NETWORK-PACKAGE: No package has been declared, but ' . __METHOD__ . ' has been called!');
                        return;
                } // END - if
 
@@ -697,7 +714,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());
+                       $this->debugOutput('NETWORK-PACKAGE: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
 
                        // Mark the package with status failed
                        $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED);
@@ -716,7 +733,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.');
+                       $this->debugOutput('NETWORK-PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
                        return;
                } // END - if
 
@@ -731,7 +748,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());
+                       $this->debugOutput('NETWORK-PACKAGE: Package was not delivered: ' . $e->getMessage());
 
                        // Mark package as failed
                        $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED);
@@ -791,7 +808,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                } // 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: */ $this->debugOutput('NETWORK-PACKAGE: 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);
@@ -807,7 +824,7 @@ 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: */ $this->debugOutput('NETWORK-PACKAGE: 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)
@@ -834,7 +851,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $decodedData = $handlerInstance->getNextDecodedData();
 
                // Very noisy debug message:
-               //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
+               //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
 
                // And push it on our stack
                $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData);
@@ -909,13 +926,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: */ $this->debugOutput('NETWORK-PACKAGE: ' . $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: */ $this->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
        }
 
        /**
@@ -928,7 +945,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                $this->initStackers(true);
 
                // Debug message
-               /* DEBUG: */ $this->debugOutput('PACKAGE: All stacker have been re-initialized.');
+               /* DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: All stacker have been re-initialized.');
        }
 
        /**
@@ -971,7 +988,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));
+                       die(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10));
                } // END - if
                */
 
@@ -1072,6 +1089,35 @@ 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);
+
+               // 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]