* @version 0.0.0 * @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 * * 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 . */ class PackageTags extends BaseTags implements Tagable { // Exception codes const EXCEPTION_INVALID_TAG = 0x160; /** * Last found protocol instance */ private $lastProtocol = NULL; /** * Last found recipient type */ private $lastRecipientType = 'invalid'; /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Init the object registry $this->initObjectRegistry(); } /** * Creates an instance of this class * * @return $tagsInstance An instance of a Tagable class */ public static final function createPackageTags () { // Get new instance $tagsInstance = new PackageTags(); // Return the prepared instance return $tagsInstance; } /** * Loads the XML file (our "object registry") and saves an instance for * faster re-use. * * @return void */ private function initObjectRegistry () { // Output debug message self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - START'); // Get the application instance $applicationInstance = Registry::getRegistry()->getInstance('app'); // Get a XML template instance $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class'); // Set it for later use $this->setTemplateInstance($templateInstance); // Read the XML file $this->getTemplateInstance()->loadXmlTemplate(); // Render the XML content $this->getTemplateInstance()->renderXmlContent(); // Output debug message self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - FINISHED'); } /** * Extracts the tags from given package data * * @param $packageData Raw package data * @return void */ private function extractTagsFromPackageData (array $packageData) { /* * 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_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); // Get the tags and store them locally $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackage::INDEX_TAGS])); } /** * Verifies all tags by looking them up in an XML file. This method is * the key method to make sure only known objects are being distributed and * 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. * * @return void * @throws InvalidTagException If a provided tag from the package data is invalid */ private function verifyAllTags () { // Get the registry $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance(); // "Walk" over all tags foreach ($this->getTags() as $tag) { // Debug output 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]))) { // 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 = ProtocolHandlerFactory::createProtocolHandlerFromArray($entry); $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]; } // END - foreach } /** * Chooses the right protocol from given package data * * @param $packageData Raw package data * @return $lastProtocol An instance of the last used ProtocolHandler class */ public function chooseProtocolFromPackageData (array $packageData) { // Extract the tags $this->extractTagsFromPackageData($packageData); // Now we need to verify every single tag $this->verifyAllTags(); // Return it return $this->lastProtocol; } /** * Checks whether the given package data is accepted by the listener * * @param $packageData Raw package data * @param $listenerInstance A Listenable instance * @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(); // Now simply check it out $accepts = (($this->lastRecipientType == $listenerInstance->getListenerType()) && ($listenerInstance->getListenerType() != 'invalid')); // And return the result return $accepts; } } // [EOF] ?>