3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Announcement;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseFrontend;
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\Message\DeliverableMessage;
12 use Org\Shipsimu\Hub\Network\Receive\Receivable;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
14 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
16 // Import framework stuff
17 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
18 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
22 * A NodeMessageAnnouncement handler
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
54 $this->setHandlerName('message_announcement');
56 // Init message data array
57 $this->messageDataElements = array(
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
59 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
60 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
61 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
62 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
63 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
64 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
67 // Init message-data->configuration translation array
68 $this->messageToConfig = array(
69 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
72 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
73 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
76 // Init config-copy array
77 $this->configCopy = array(
78 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
79 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
80 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
81 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
82 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
86 $this->searchData = array(
87 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
92 * Creates an instance of this class
94 * @return $handlerInstance An instance of a HandleableMessage class
96 public final static function createNodeMessageAnnouncementHandler () {
98 $handlerInstance = new NodeMessageAnnouncementHandler();
100 // Return the prepared instance
101 return $handlerInstance;
105 * Handles data array of the message
107 * @param $messageInstance An instance of a DeliverableMessage class
108 * @param $packageInstance An instance of a Receivable class
110 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
112 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
114 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
115 $nodeInstance = NodeObjectFactory::createNodeInstance();
117 // Is this node accepting announcements?
118 if (!$nodeInstance->isAcceptingAnnouncements()) {
120 * This node is not accepting announcements, then someone wants to
121 * announce his node to a non-bootstrap and non-master node.
123 throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageInstance), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
126 // Register the announcing node with this node
127 $this->registerNodeByMessageInstance($messageInstance);
129 // Prepare answer message to be delivered back to the other node
130 $this->prepareAnswerMessage($messageInstance, $packageInstance);
134 * Adds all required elements from given array into data set instance
136 * @param $dataSetInstance An instance of a StoreableCriteria class
137 * @param $messageInstance An instance of a DeliverableMessage class
140 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
142 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
145 foreach ($this->messageDataElements as $key) {
147 assert(isset($messageData[$key]));
150 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
151 $dataSetInstance->addCriteria($key, $messageData[$key]);
156 * Initializes configuration data from given message data array
158 * @param $messageInstance An instance of a DeliverableMessage class
161 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
162 // "Walk" throught the translation array
163 /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageInstance=' . print_r($messageInstance, TRUE));
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 FrameworkBootstrap::getConfigurationInstance()->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 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry($targetKey, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($sourceKey));
181 // Translate last exception into a status code
182 $statusCode = $this->getTranslatedStatusFromLastException();
184 // Set it in configuration (temporarily)
185 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry(NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_ANSWER_STATUS, $statusCode);
189 * Removes configuration data with given message data array from global
192 * @param $messageData An instance of a DeliverableMessage class
195 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
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 FrameworkBootstrap::getConfigurationInstance()->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 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry($configKey);
208 // Remove NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_ANSWER_STATUS as well
209 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_ANSWER_STATUS);