]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/tags/package/class_PackageTags.php
Continued rewrite:
[hub.git] / application / hub / main / tags / package / class_PackageTags.php
index e8b15b68236a5dc9952f6e7a2d5501c8b2cf6f1f..5bef93ac50851eb2a1d7b899c8fa34149ad4382c 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -26,9 +26,9 @@ class PackageTags extends BaseTags implements Tagable {
        const EXCEPTION_INVALID_TAG = 0x160;
 
        /**
-        * Last found protocol
+        * Last found protocol instance
         */
-       private $lastProtocol = 'invalid';
+       private $lastProtocol = NULL;
 
        /**
         * Last found recipient type
@@ -69,7 +69,7 @@ class PackageTags extends BaseTags implements Tagable {
         */
        private function initObjectRegistry () {
                // Output debug message
-               self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Initializing object registry - START');
+               self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - START');
 
                // Get the application instance
                $applicationInstance = Registry::getRegistry()->getInstance('app');
@@ -87,7 +87,7 @@ class PackageTags extends BaseTags implements Tagable {
                $this->getTemplateInstance()->renderXmlContent();
 
                // Output debug message
-               self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Initializing object registry - FINISHED');
+               self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - FINISHED');
        }
 
        /**
@@ -97,6 +97,9 @@ class PackageTags extends BaseTags implements Tagable {
         * @return      void
         */
        private function extractTagsFromPackageData (array $packageData) {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: packageData=' . print_r($packageData, TRUE));
+
                /*
                 * We take a look at the tags (in most cases only one is needed) so
                 * first we need the content data splitted up into all it's parts.
@@ -113,29 +116,33 @@ class PackageTags extends BaseTags implements Tagable {
         * shared over the whole hub-network. So if the "tag" (let's better say
         * object type) isn't found in that XML the package won't be distributed.
         *
+        * @param       $packageData                    Raw package data
         * @return      void
         * @throws      InvalidTagException             If a provided tag from the package data is invalid
         */
-       private function verifyAllTags () {
+       private function verifyAllTags (array $packageData) {
                // Get the registry
                $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance();
 
                // "Walk" over all tags
                foreach ($this->getTags() as $tag) {
                        // Debug output
-                       self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Validating tag ' . $tag . ' ...');
+                       self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Validating tag ' . $tag . ' ...');
 
                        // Get an array from this tag
                        $entry = $objectRegistryInstance->getArrayFromKey(XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_NAME, $tag);
 
-                       // If the array is empty, the entry is invalid!
-                       if ((count($entry) == 0) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_PROTOCOL])) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]))) {
+                       /*
+                        * If it is no array or the array is empty or an entry is missing
+                        * the entry is invalid.
+                        */
+                       if ((!is_array($entry)) || (count($entry) == 0) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_PROTOCOL])) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]))) {
                                // Invalid entry found
                                throw new InvalidTagException(array($this, $tag), self::EXCEPTION_INVALID_TAG);
                        } // END - if
 
                        // Now save the last discovered protocol/recipient type
-                       $this->lastProtocol      = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_PROTOCOL];
+                       $this->lastProtocol      = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData);
                        $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE];
                } // END - foreach
        }
@@ -144,20 +151,17 @@ class PackageTags extends BaseTags implements Tagable {
         * Chooses the right protocol from given package data
         *
         * @param       $packageData    Raw package data
-        * @return      $protocolName   Name of the choosen procotol
+        * @return      $lastProtocol   An instance of the last used HandleableProtocol class
         */
        public function chooseProtocolFromPackageData (array $packageData) {
                // Extract the tags
                $this->extractTagsFromPackageData($packageData);
 
                // Now we need to verify every single tag
-               $this->verifyAllTags();
-
-               // Use the last found protocol for transmission
-               $protocolName = $this->lastProtocol;
+               $this->verifyAllTags($packageData);
 
-               // Return it
-               return $protocolName;
+               // Return the last (and only) found protocol (e.g. 'tcp' is very usual)
+               return $this->lastProtocol;
        }
 
        /**
@@ -172,7 +176,7 @@ class PackageTags extends BaseTags implements Tagable {
                $this->extractTagsFromPackageData($packageData);
 
                // Now we need to verify every single tag
-               $this->verifyAllTags();
+               $this->verifyAllTags($packageData);
 
                // Now simply check it out
                $accepts = (($this->lastRecipientType == $listenerInstance->getListenerType()) && ($listenerInstance->getListenerType() != 'invalid'));