Updated 'core'.
[hub.git] / application / hub / main / tags / package / class_PackageTags.php
1 <?php
2 /**
3  * A Package tags class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class PackageTags extends BaseTags implements Tagable {
25         // Exception codes
26         const EXCEPTION_INVALID_TAG = 0x160;
27
28         /**
29          * Last found protocol instance
30          */
31         private $lastProtocol = NULL;
32
33         /**
34          * Last found recipient type
35          */
36         private $lastRecipientType = 'invalid';
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46
47                 // Init the object registry
48                 $this->initObjectRegistry();
49         }
50
51         /**
52          * Creates an instance of this class
53          *
54          * @return      $tagsInstance   An instance of a Tagable class
55          */
56         public static final function createPackageTags () {
57                 // Get new instance
58                 $tagsInstance = new PackageTags();
59
60                 // Return the prepared instance
61                 return $tagsInstance;
62         }
63
64         /**
65          * Loads the XML file (our "object registry") and saves an instance for
66          * faster re-use.
67          *
68          * @return      void
69          */
70         private function initObjectRegistry () {
71                 // Output debug message
72                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - CALLED!');
73
74                 // Get the application instance
75                 $applicationInstance = Registry::getRegistry()->getInstance('app');
76
77                 // Get a XML template instance
78                 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class');
79
80                 // Set it for later use
81                 $this->setTemplateInstance($templateInstance);
82
83                 // Read the XML file
84                 $this->getTemplateInstance()->loadXmlTemplate();
85
86                 // Render the XML content
87                 $this->getTemplateInstance()->renderXmlContent();
88
89                 // Output debug message
90                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Initializing object registry - EXIT!');
91         }
92
93         /**
94          * Extracts the tags from given package data
95          *
96          * @param       $packageData    Raw package data
97          * @return      void
98          */
99         private function extractTagsFromPackageData (array $packageData) {
100                 // Debug message
101                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: packageData=' . print_r($packageData, TRUE));
102
103                 /*
104                  * We take a look at the tags (in most cases only one is needed) so
105                  * first we need the content data splitted up into all it's parts.
106                  */
107                 $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
108
109                 // Get the tags and store them locally
110                 $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackage::INDEX_TAGS]));
111         }
112
113         /**
114          * Verifies all tags by looking them up in an XML file. This method is
115          * the key method to make sure only known objects are being distributed and
116          * shared over the whole hub-network. So if the "tag" (let's better say
117          * object type) isn't found in that XML the package won't be distributed.
118          *
119          * @param       $packageData                    Raw package data
120          * @return      void
121          * @throws      InvalidTagException             If a provided tag from the package data is invalid
122          */
123         private function verifyAllTags (array $packageData) {
124                 // Get the registry
125                 $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance();
126
127                 // "Walk" over all tags
128                 foreach ($this->getTags() as $tag) {
129                         // Debug output
130                         self::createDebugInstance(__CLASS__)->debugOutput('TAGS[' . __METHOD__ . ':' . __LINE__ . ']: Validating tag ' . $tag . ' ...');
131
132                         // Get an array from this tag
133                         $entry = $objectRegistryInstance->getArrayFromKey(XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_NAME, $tag);
134
135                         /*
136                          * If it is no array or the array is empty or an entry is missing
137                          * the entry is invalid.
138                          */
139                         if ((!is_array($entry)) || (count($entry) == 0) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_PROTOCOL])) || (!isset($entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]))) {
140                                 // Invalid entry found
141                                 throw new InvalidTagException(array($this, $tag), self::EXCEPTION_INVALID_TAG);
142                         } // END - if
143
144                         // Now save the last discovered protocol/recipient type
145                         $this->lastProtocol      = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData);
146                         $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE];
147                 } // END - foreach
148         }
149
150         /**
151          * Chooses the right protocol from given package data
152          *
153          * @param       $packageData    Raw package data
154          * @return      $lastProtocol   An instance of the last used HandleableProtocol class
155          */
156         public function chooseProtocolFromPackageData (array $packageData) {
157                 // Extract the tags
158                 $this->extractTagsFromPackageData($packageData);
159
160                 // Now we need to verify every single tag
161                 $this->verifyAllTags($packageData);
162
163                 // Return the last (and only) found protocol (e.g. 'tcp' is very usual)
164                 return $this->lastProtocol;
165         }
166
167         /**
168          * Checks whether the given package data is accepted by the listener
169          *
170          * @param       $packageData            Raw package data
171          * @param       $listenerInstance       A Listenable instance
172          * @return      $accepts                        Whether it is accepted
173          */
174         public function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance) {
175                 // Extract the tags
176                 $this->extractTagsFromPackageData($packageData);
177
178                 // Now we need to verify every single tag
179                 $this->verifyAllTags($packageData);
180
181                 // Now simply check it out
182                 $accepts = (($this->lastRecipientType == $listenerInstance->getListenerType()) && ($listenerInstance->getListenerType() != 'invalid'));
183
184                 // And return the result
185                 return $accepts;
186         }
187 }
188
189 // [EOF]
190 ?>