]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/message-types/class_BaseMessageHandler.php
a9ec5e79d898192b9a179b290ab2f3af61d6ddca
[hub.git] / application / hub / main / handler / message-types / class_BaseMessageHandler.php
1 <?php
2 /**
3  * A general message handler, this class must be abstract to make the template
4  * method pattern working.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 abstract class BaseMessageHandler extends BaseHandler {
26         /**
27          * Array with search criteria elements
28          */
29         protected $searchData = array();
30
31         /**
32          * Array with all data XML nodes (which hold the actual data) and their values
33          */
34         protected $messageDataElements = array();
35
36         /**
37          * Array for translating message data elements (other node's data mostly)
38          * into configuration elements.
39          */
40         protected $messageToConfig = array();
41
42         /**
43          * Array for copying configuration entries
44          */
45         protected $configCopy = array();
46
47         /**
48          * Last exception instance from database layer or NULL (default)
49          */
50         private $lastException = NULL;
51
52         /**
53          * Protected constructor
54          *
55          * @param       $className      Name of the class
56          * @return      void
57          */
58         protected function __construct ($className) {
59                 // Call parent constructor
60                 parent::__construct($className);
61         }
62
63         /**
64          * Getter for last exception
65          *
66          * @return      $lastException  Last thrown exception
67          */
68         protected final function getLastException () {
69                 return $this->lastException;
70         }
71
72         /**
73          * "Getter" for a translated last exception as a status code
74          *
75          * @return      $statusCode             Translated status code from last exception
76          */
77         protected function getTranslatedStatusFromLastException () {
78                 // Default is all fine
79                 $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
80
81                 // Is the last exception not NULL?
82                 if ($this->lastException instanceof FrameworkException) {
83                         // "Determine" the right status code (may differ from exception to exception)
84                         $this->debugInstance('lastException=' . $this->lastException->__toString() . ',message=' . $this->lastException->getMessage() . ' is not finished!');
85                 } // END - if
86
87                 // Return the status code
88                 return $statusCode;
89         }
90
91         /**
92          * Registers an other node with this node by given message data. The
93          * following data must always be present:
94          *
95          * - session-id  (for finding the node's record together with below data)
96          * - external-ip (hostname or IP number)
97          * - tcp-port    (TCP port for inbound connections)
98          *
99          * @param       $messageArray   An array with all minimum message data
100          * @return      void
101          * @todo        Add something more, e.g. spreading information over DHT
102          */
103         protected function registerNodeByMessageData (array $messageData) {
104                 // Check if searchData has entries
105                 assert(count($this->searchData) > 0);
106
107                 // Get a wrapper instance
108                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
109
110                 // Get a search criteria class
111                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
112
113                 // Search for the node's session id and external IP/hostname + TCP port
114                 foreach ($this->searchData as $key) {
115                         // Add criteria
116                         $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
117                 } // END - foreach
118
119                 // Only one entry is fine
120                 $searchInstance->setLimit(1);
121
122                 // Run the query
123                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
124
125                 // Is there already an entry?
126                 if ($resultInstance->next()) {
127                         // Entry found, so update it
128                         $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
129                 } else {
130                         // Nothing found, so register it
131                         $wrapperInstance->registerNodeByMessageData($messageData, $this);
132                 }
133
134                 // Save last exception
135                 $this->lastException = $wrapperInstance->getLastException();
136         }
137
138         /**
139          * Prepares a message as answer for given message data for delivery.
140          *
141          * @param       $messageData            An array with all message data
142          * @param       $packageInstance        An instance of a Deliverable instance
143          * @return      void
144          */
145         protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
146                 // Debug message
147                 /* NOISY-DEBUG: */ $this->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Going to send an answer message ...');
148
149                 // Get a helper instance based on this handler's name
150                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
151
152                 // Load descriptor XML
153                 $helperInstance->loadDescriptorXml();
154
155                 /*
156                  * Set missing (temporary) configuration data, mostly it needs to be
157                  * copied from message data array.
158                  */
159                 $this->initMessageConfigurationData($messageData);
160
161                 // Compile any configuration variables
162                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
163
164                 // Get node instance
165                 $nodeInstance = Registry::getRegistry()->getInstance('node');
166
167                 // Deliver the package
168                 $helperInstance->sendPackage($nodeInstance);
169
170                 /*
171                  * Remove temporary configuration
172                  */
173                 $this->removeMessageConfigurationData($messageData);
174
175                 // Debug message
176                 /* NOISY-DEBUG: */ $this->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Answer message has been prepared.');
177         }
178
179         /**
180          * Initializes configuration data from given message data array
181          *
182          * @param       $messageData    An array with all message data
183          * @return      void
184          */
185         abstract protected function initMessageConfigurationData (array $messageData);
186
187         /**
188          * Removes configuration data with given message data array from global
189          * configuration
190          *
191          * @param       $messageData    An array with all message data
192          * @return      void
193          */
194         abstract protected function removeMessageConfigurationData (array $messageData);
195 }
196
197 // [EOF]
198 ?>