3 * A general message handler, this class must be abstract to make the template
4 * method pattern working.
6 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
25 abstract class BaseMessageHandler extends BaseHandler {
27 * Array with search criteria elements
29 private $searchData = array();
32 * Array with all data XML nodes (which hold the actual data) and their values
34 protected $messageDataElements = array();
37 * Array for translating message data elements (other node's data mostly)
38 * into configuration elements.
40 protected $messageToConfig = array();
43 * Array for copying configuration entries
45 protected $configCopy = array();
48 * Last exception instance from database layer or NULL (default)
50 private $lastException = NULL;
53 * Protected constructor
55 * @param $className Name of the class
58 protected function __construct ($className) {
59 // Call parent constructor
60 parent::__construct($className);
63 $this->searchData = array(
64 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
65 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP,
66 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT
71 * Getter for last exception
73 * @return $lastException Last thrown exception
75 protected final function getLastException () {
76 return $this->lastException;
80 * "Getter" for a translated last exception as a status code
82 * @return $statusCode Translated status code from last exception
84 protected function getTranslatedStatusFromLastException () {
85 // Default is all fine
86 $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
88 // Is the last exception not NULL?
89 if ($this->lastException instanceof FrameworkException) {
90 // "Determine" the right status code (may differ from exception to exception)
91 $this->debugInstance('lastException=' . $this->lastException->__toString() . ',message=' . $this->lastException->getMessage() . ' is not finished!');
94 // Return the status code
99 * Registers an other node with this node by given message data. The
100 * following data must always be present:
102 * - session-id (for finding the node's record together with below data)
103 * - external-ip (hostname or IP number)
104 * - tcp-port (TCP port for inbound connections)
106 * @param $messageArray An array with all minimum message data
108 * @todo Add something more, e.g. spreading information over DHT
110 protected function registerNodeByMessageData (array $messageData) {
111 // Get a wrapper instance
112 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
114 // Get a search criteria class
115 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
117 // Search for the node's session id and external IP/hostname + TCP port
118 foreach ($this->searchData as $key) {
120 $searchInstance->addCriteria('node_' . $key, $messageData[$key]);
123 // Only one entry is fine
124 $searchInstance->setLimit(1);
127 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
129 // Is there already an entry?
130 if ($resultInstance->next()) {
131 // Entry found, so update it
132 $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
134 // Nothing found, so register it
135 $wrapperInstance->registerNodeByMessageData($messageData, $this);
138 // Save last exception
139 $this->lastException = $wrapperInstance->getLastException();
143 * Prepares a message as answer for given message data for delivery.
145 * @param $messageData An array with all message data
146 * @param $packageInstance An instance of a Deliverable instance
149 protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
151 /* NOISY-DEBUG: */ $this->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Going to send an answer message ...');
153 // Get a helper instance based on this handler's name
154 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
156 // Load descriptor XML
157 $helperInstance->loadDescriptorXml();
160 * Set missing (temporary) configuration data, mostly it needs to be
161 * copied from message data array.
163 $this->initMessageConfigurationData($messageData);
165 // Compile any configuration variables
166 $helperInstance->getTemplateInstance()->compileConfigInVariables();
169 $nodeInstance = Registry::getRegistry()->getInstance('node');
171 // Deliver the package
172 $helperInstance->sendPackage($nodeInstance);
175 * Remove temporary configuration
177 $this->removeMessageConfigurationData($messageData);
180 /* NOISY-DEBUG: */ $this->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Answer message has been prepared.');
184 * Initializes configuration data from given message data array
186 * @param $messageData An array with all message data
189 abstract protected function initMessageConfigurationData (array $messageData);
192 * Removes configuration data with given message data array from global
195 * @param $messageData An array with all message data
198 abstract protected function removeMessageConfigurationData (array $messageData);