3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Announcement;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
8 use Org\Shipsimu\Hub\Generic\BaseHubSystem;
9 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
10 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
11 use Org\Shipsimu\Hub\Network\Receive\Receivable;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Registry\Registerable;
20 * A NodeMessageAnnouncement handler
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
52 $this->setHandlerName('message_announcement');
54 // Init message data array
55 $this->messageDataElements = array(
56 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
57 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
59 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
60 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
61 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
62 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
65 // Init message-data->configuration translation array
66 $this->messageToConfig = array(
67 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
68 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
69 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
74 // Init config-copy array
75 $this->configCopy = array(
76 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
77 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
78 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
79 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
80 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
84 $this->searchData = array(
85 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
90 * Creates an instance of this class
92 * @return $handlerInstance An instance of a HandleableMessage class
94 public final static function createNodeMessageAnnouncementHandler () {
96 $handlerInstance = new NodeMessageAnnouncementHandler();
98 // Return the prepared instance
99 return $handlerInstance;
103 * Handles data array of the message
105 * @param $messageData An array with message data to handle
106 * @param $packageInstance An instance of a Receivable class
108 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
110 public function handleMessageData (array $messageData, Receivable $packageInstance) {
112 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
113 $nodeInstance = NodeObjectFactory::createNodeInstance();
115 // Is this node accepting announcements?
116 if (!$nodeInstance->isAcceptingAnnouncements()) {
118 * This node is not accepting announcements, then someone wants to
119 * announce his node to a non-bootstrap and non-master node.
121 throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
124 // Register the announcing node with this node
125 $this->registerNodeByMessageData($messageData);
127 // Prepare answer message to be delivered back to the other node
128 $this->prepareAnswerMessage($messageData, $packageInstance);
132 * Adds all required elements from given array into data set instance
134 * @param $dataSetInstance An instance of a StoreableCriteria class
135 * @param $messageData An array with all message data
138 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
140 parent::addArrayToDataSet($dataSetInstance, $messageData);
143 foreach ($this->messageDataElements as $key) {
145 assert(isset($messageData[$key]));
148 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
149 $dataSetInstance->addCriteria($key, $messageData[$key]);
154 * Initializes configuration data from given message data array
156 * @param $messageData An array with all message data
159 protected function initMessageConfigurationData (array $messageData) {
161 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
163 // "Walk" throught the translation array
164 foreach ($this->messageToConfig as $messageKey => $configKey) {
166 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
168 // Set the element in configuration
169 $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
172 // "Walk" throught the config-copy array
173 foreach ($this->configCopy as $targetKey => $sourceKey) {
175 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
177 // Copy from source to targetKey
178 $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
181 // Translate last exception into a status code
182 $statusCode = $this->getTranslatedStatusFromLastException();
184 // Set it in configuration (temporarily)
185 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
189 * Removes configuration data with given message data array from global
192 * @param $messageData An array with all message data
195 protected function removeMessageConfigurationData (array $messageData) {
196 // "Walk" throught the translation array again
197 foreach ($this->messageToConfig as $dummy => $configKey) {
198 // Now unset this configuration entry (to save some memory)
199 $this->getConfigInstance()->unsetConfigEntry($configKey);
202 // "Walk" throught the config-copy array again
203 foreach ($this->configCopy as $configKey => $dummy) {
204 // Now unset this configuration entry (to save some memory again)
205 $this->getConfigInstance()->unsetConfigEntry($configKey);
208 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
209 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);