5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class PackageTags extends BaseTags implements Tagable {
26 const EXCEPTION_INVALID_TAG = 0x160;
31 private $lastProtocol = 'invalid';
34 * Last found recipient type
36 private $lastRecipientType = 'invalid';
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
47 // Init the object registry
48 $this->initObjectRegistry();
52 * Creates an instance of this class
54 * @return $tagsInstance An instance of a Tagable class
56 public static final function createPackageTags () {
58 $tagsInstance = new PackageTags();
60 // Return the prepared instance
65 * Loads the XML file (our "object registry") and saves an instance for
70 private function initObjectRegistry () {
71 // Output debug message
72 self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Initializing object registry - START');
74 // Get the application instance
75 $applicationInstance = Registry::getRegistry()->getInstance('app');
77 // Get a XML template instance
78 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class');
80 // Set it for later use
81 $this->setTemplateInstance($templateInstance);
84 $this->getTemplateInstance()->loadXmlTemplate();
86 // Render the XML content
87 $this->getTemplateInstance()->renderXmlContent();
89 // Output debug message
90 self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Initializing object registry - FINISHED');
94 * Extracts the tags from given package data
96 * @param $packageData Raw package data
99 private function extractTagsFromPackageData (array $packageData) {
101 * We take a look at the tags (in most cases only one is needed) so
102 * first we need the content data splitted up into all it's parts.
104 $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
106 // Get the tags and store them locally
107 $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackage::INDEX_TAGS]));
111 * Verifies all tags by looking them up in an XML file. This method is
112 * the key method to make sure only known objects are being distributed and
113 * shared over the whole hub-network. So if the "tag" (let's better say
114 * object type) isn't found in that XML the package won't be distributed.
117 * @throws InvalidTagException If a provided tag from the package data is invalid
119 private function verifyAllTags () {
121 $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance();
123 // "Walk" over all tags
124 foreach ($this->getTags() as $tag) {
126 self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __LINE__ . ']: Validating tag ' . $tag . ' ...');
128 // Get an array from this tag
129 $entry = $objectRegistryInstance->getArrayFromKey(XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_NAME, $tag);
131 // If the array is empty, the entry is invalid!
132 if (count($entry) == 0) {
133 // Invalid entry found
134 throw new InvalidTagException(array($this, $tag), self::EXCEPTION_INVALID_TAG);
137 // Now save the last discovered protocol/recipient type
138 $this->lastProtocol = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_PROTOCOL];
139 $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE];
144 * Chooses the right protocol from given package data
146 * @param $packageData Raw package data
147 * @return $protocolName Name of the choosen procotol
149 public function chooseProtocolFromPackageData (array $packageData) {
151 $this->extractTagsFromPackageData($packageData);
153 // Now we need to verify every single tag
154 $this->verifyAllTags();
156 // Use the last found protocol for transmission
157 $protocolName = $this->lastProtocol;
160 return $protocolName;
164 * Checks whether the given package data is accepted by the listener
166 * @param $packageData Raw package data
167 * @param $listenerInstance A Listenable instance
168 * @return $accepts Whether it is accepted
170 public function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance) {
172 $this->extractTagsFromPackageData($packageData);
174 // Now we need to verify every single tag
175 $this->verifyAllTags();
177 // Now simply check it out
178 $accepts = (($this->lastRecipientType == $listenerInstance->getListenerType()) && ($listenerInstance->getListenerType() != 'invalid'));
180 // And return the result