3 namespace Hub\Handler\Node\Message\Announcement;
5 // Import application-specific stuff
6 use Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Hub\Factory\Node\NodeObjectFactory;
8 use Hub\Generic\BaseHubSystem;
9 use Hub\Network\Receive\Receivable;
11 // Import framework stuff
12 use CoreFramework\Criteria\Storing\StoreableCriteria;
13 use CoreFramework\Registry\Registerable;
16 * A NodeMessageAnnouncement handler
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
48 $this->setHandlerName('message_announcement');
50 // Init message data array
51 $this->messageDataElements = array(
52 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
53 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
54 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
55 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
56 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
57 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
61 // Init message-data->configuration translation array
62 $this->messageToConfig = array(
63 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
64 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
65 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
66 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
67 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
70 // Init config-copy array
71 $this->configCopy = array(
72 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
73 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
74 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
75 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
76 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
80 $this->searchData = array(
81 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
86 * Creates an instance of this class
88 * @return $handlerInstance An instance of a HandleableMessage class
90 public final static function createNodeMessageAnnouncementHandler () {
92 $handlerInstance = new NodeMessageAnnouncementHandler();
94 // Return the prepared instance
95 return $handlerInstance;
99 * Handles data array of the message
101 * @param $messageData An array with message data to handle
102 * @param $packageInstance An instance of a Receivable class
104 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
106 public function handleMessageData (array $messageData, Receivable $packageInstance) {
108 $nodeInstance = NodeObjectFactory::createNodeInstance();
110 // Is this node accepting announcements?
111 if (!$nodeInstance->isAcceptingAnnouncements()) {
113 * This node is not accepting announcements, then someone wants to
114 * announce his node to a non-bootstrap and non-master node.
116 throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
119 // Register the announcing node with this node
120 $this->registerNodeByMessageData($messageData);
122 // Prepare answer message to be delivered back to the other node
123 $this->prepareAnswerMessage($messageData, $packageInstance);
127 * Adds all required elements from given array into data set instance
129 * @param $dataSetInstance An instance of a StoreableCriteria class
130 * @param $messageData An array with all message data
133 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
135 parent::addArrayToDataSet($dataSetInstance, $messageData);
138 foreach ($this->messageDataElements as $key) {
140 assert(isset($messageData[$key]));
143 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
144 $dataSetInstance->addCriteria($key, $messageData[$key]);
149 * Initializes configuration data from given message data array
151 * @param $messageData An array with all message data
154 protected function initMessageConfigurationData (array $messageData) {
156 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
158 // "Walk" throught the translation array
159 foreach ($this->messageToConfig as $messageKey => $configKey) {
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
163 // Set the element in configuration
164 $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
167 // "Walk" throught the config-copy array
168 foreach ($this->configCopy as $targetKey => $sourceKey) {
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
172 // Copy from source to targetKey
173 $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
176 // Translate last exception into a status code
177 $statusCode = $this->getTranslatedStatusFromLastException();
179 // Set it in configuration (temporarily)
180 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
184 * Removes configuration data with given message data array from global
187 * @param $messageData An array with all message data
190 protected function removeMessageConfigurationData (array $messageData) {
191 // "Walk" throught the translation array again
192 foreach ($this->messageToConfig as $dummy => $configKey) {
193 // Now unset this configuration entry (to save some memory)
194 $this->getConfigInstance()->unsetConfigEntry($configKey);
197 // "Walk" throught the config-copy array again
198 foreach ($this->configCopy as $configKey => $dummy) {
199 // Now unset this configuration entry (to save some memory again)
200 $this->getConfigInstance()->unsetConfigEntry($configKey);
203 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
204 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);