]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/message-types/class_BaseMessageHandler.php
4763b03cdf7e14f3a54df533047745cd62e0919e
[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         private $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          * Last exception instance from database layer or NULL (default)
44          */
45         private $lastException = NULL;
46
47         /**
48          * Protected constructor
49          *
50          * @param       $className      Name of the class
51          * @return      void
52          */
53         protected function __construct ($className) {
54                 // Call parent constructor
55                 parent::__construct($className);
56
57                 // Init array
58                 $this->searchData = array(
59                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
60                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP,
61                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT
62                 );
63         }
64
65         /**
66          * Getter for last exception
67          *
68          * @return      $lastException  Last thrown exception
69          */
70         protected final function getLastException () {
71                 return $this->lastException;
72         }
73
74         /**
75          * "Getter" for a translated last exception as a status code
76          *
77          * @return      $statusCode             Translated status code from last exception
78          */
79         protected function getTranslatedStatusFromLastException () {
80                 // Default is all fine
81                 $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
82
83                 // Is the last exception not NULL?
84                 if ($this->lastException instanceof FrameworkException) {
85                         // "Determine" the right status code (may differ from exception to exception)
86                         $this->debugInstance('lastException=' . $this->lastException->__toString() . ',message=' . $this->lastException->getMessage() . ' is not finished!');
87                 } // END - if
88
89                 // Return the status code
90                 return $statusCode;
91         }
92
93         /**
94          * Registers an other node with this node by given message data. The
95          * following data must always be present:
96          *
97          * - session-id  (for finding the node's record together with below data)
98          * - external-ip (hostname or IP number)
99          * - tcp-port    (TCP port for inbound connections)
100          *
101          * @param       $messageArray   An array with all minimum message data
102          * @return      void
103          */
104         protected function registerNodeByMessageData (array $messageData) {
105                 // Get a wrapper instance
106                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
107
108                 // Get a search criteria class
109                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
110
111                 // Search for the node's session id and external IP/hostname + TCP port
112                 foreach ($this->searchData as $key) {
113                         // Add criteria
114                         $searchInstance->addCriteria('node_' . $key, $messageData[$key]);
115                 } // END - foreach
116
117                 // Only one entry is fine
118                 $searchInstance->setLimit(1);
119
120                 // Run the query
121                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
122
123                 // Is there already an entry?
124                 if ($resultInstance->next()) {
125                         // Entry found
126                         $resultInstance->debugBackTrace('Entry found!');
127                 } else {
128                         // Nothing found, so register it
129                         $wrapperInstance->registerNodeByMessageData($messageData, $this);
130                 }
131
132                 // Save last exception
133                 $this->lastException = $wrapperInstance->getLastException();
134         }
135
136         /**
137          * Prepares a message as answer for given message data for delivery.
138          *
139          * @param       $messageData            An array with all message data
140          * @param       $packageInstance        An instance of a Deliverable instance
141          * @return      void
142          */
143         protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
144                 // Get a helper instance based on this handler's name
145                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
146
147                 // Load descriptor XML
148                 $helperInstance->loadDescriptorXml();
149
150                 /*
151                  * Set missing (temporary) configuration data, mostly it needs to be
152                  * copied from message data array.
153                  */
154                 $this->initMessageConfigurationData($messageData);
155
156                 // Compile any configuration variables
157                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
158
159                 // Get node instance
160                 $nodeInstance = Registry::getRegistry()->getInstance('node');
161
162                 // Deliver the package
163                 $helperInstance->sendPackage($nodeInstance);
164
165                 /*
166                  * Remove temporary configuration
167                  */
168                 $this->removeMessageConfigurationData($messageData);
169         }
170
171         /**
172          * Initializes configuration data from given message data array
173          *
174          * @param       $messageData    An array with all message data
175          * @return      void
176          */
177         abstract protected function initMessageConfigurationData (array $messageData);
178
179         /**
180          * Removes configuration data with given message data array from global
181          * configuration
182          *
183          * @param       $messageData    An array with all message data
184          * @return      void
185          */
186         abstract protected function removeMessageConfigurationData (array $messageData);
187 }
188
189 // [EOF]
190 ?>