X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Ftags%2Fpackage%2Fclass_PackageTags.php;h=b2985f7664d2dfb2b10e92efd96f09e0aef64d1c;hb=280fca59f8ed35ddde36a794c7a9f4991911e46a;hp=e630c4928bb2858bfd8b4608e976375ae12d056c;hpb=f33fd598a1a77034720172ea80353e4d4c0f9662;p=hub.git diff --git a/application/hub/main/tags/package/class_PackageTags.php b/application/hub/main/tags/package/class_PackageTags.php index e630c4928..b2985f766 100644 --- a/application/hub/main/tags/package/class_PackageTags.php +++ b/application/hub/main/tags/package/class_PackageTags.php @@ -2,11 +2,11 @@ /** * A Package tags class * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @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 @@ -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 @@ -62,40 +62,32 @@ class PackageTags extends BaseTags implements Tagable { } /** - * Loads the XML file (our "object registry") and saves an instance for faster re-use + * Loads the XML file (our "object registry") and saves an instance for + * faster re-use. * * @return void */ private function initObjectRegistry () { // Output debug message - $this->debugOutput('TAGS: Initializing object registry - START'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - CALLED!'); // Get the application instance - $appInstance = Registry::getRegistry()->getInstance('app'); + $applicationInstance = Registry::getRegistry()->getInstance('app'); // Get a XML template instance - $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($appInstance)); - - // Disable language support - $templateInstance->enableLanguageSupport(false); - - /* - * Enable compacting/rewriting of the XML to save bandwidth from XML - * comments. This is expensive and should be avoided in general. - */ - $templateInstance->enableXmlCompacting(); + $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class'); // Set it for later use $this->setTemplateInstance($templateInstance); // Read the XML file - $this->getTemplateInstance()->loadObjectRegistryTemplate('object_registry'); + $this->getTemplateInstance()->loadXmlTemplate(); // Render the XML content $this->getTemplateInstance()->renderXmlContent(); // Output debug message - $this->debugOutput('TAGS: Initializing object registry - FINISHED'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - EXIT!'); } /** @@ -105,14 +97,17 @@ 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. */ - $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPERATOR, $packageData['content']); + $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); // Get the tags and store them locally - $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPERATOR, $contentData[NetworkPackage::INDEX_TAGS])); + $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackage::INDEX_TAGS])); } /** @@ -121,30 +116,34 @@ 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 = ObjectFactory::createObjectByConfiguredName('object_type_registry_class'); + $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance(); // "Walk" over all tags foreach ($this->getTags() as $tag) { // Debug output - $this->debugOutput('TAGS: Validating tag ' . $tag . ' ...'); + self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Validating tag ' . $tag . ' ...'); // Get an array from this tag - $entry = $objectRegistryInstance->getArrayFromKey($tag); + $entry = $objectRegistryInstance->getArrayFromKey(XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_NAME, $tag); - // If the array is empty, the entry is invalid! - if (count($entry) == 0) { + /* + * 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['object-protocol']; - $this->lastRecipientType = $entry['object-recipient-type']; + $this->lastProtocol = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); + $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]; } // END - foreach } @@ -152,35 +151,32 @@ 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; } /** - * Checks wether the given package data is accepted by the listener + * Checks whether the given package data is accepted by the listener * * @param $packageData Raw package data * @param $listenerInstance A Listenable instance - * @return $accepts Wether it is accepted + * @return $accepts Whether it is accepted */ public function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance) { // Extract the tags $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'));