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\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\Criteria\Storing\StoreableCriteria;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
21 * A NodeMessageAnnouncement handler
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
53 $this->setHandlerName('message_announcement');
55 // Init message data array
56 $this->messageDataElements = array(
57 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
59 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
60 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
61 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
62 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
63 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
66 // Init message-data->configuration translation array
67 $this->messageToConfig = array(
68 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
69 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
72 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
75 // Init config-copy array
76 $this->configCopy = array(
77 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
78 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
79 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
80 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
81 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
85 $this->searchData = array(
86 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
91 * Creates an instance of this class
93 * @return $handlerInstance An instance of a HandleableMessage class
95 public final static function createNodeMessageAnnouncementHandler () {
97 $handlerInstance = new NodeMessageAnnouncementHandler();
99 // Return the prepared instance
100 return $handlerInstance;
104 * Handles data array of the message
106 * @param $messageInstance An instance of a DeliverableMessage class
107 * @param $packageInstance An instance of a Receivable class
109 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
111 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
113 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
114 $nodeInstance = NodeObjectFactory::createNodeInstance();
116 // Is this node accepting announcements?
117 if (!$nodeInstance->isAcceptingAnnouncements()) {
119 * This node is not accepting announcements, then someone wants to
120 * announce his node to a non-bootstrap and non-master node.
122 throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageInstance), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
125 // Register the announcing node with this node
126 $this->registerNodeByMessageInstance($messageInstance);
128 // Prepare answer message to be delivered back to the other node
129 $this->prepareAnswerMessage($messageInstance, $packageInstance);
133 * Adds all required elements from given array into data set instance
135 * @param $dataSetInstance An instance of a StoreableCriteria class
136 * @param $messageInstance An instance of a DeliverableMessage class
139 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
141 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
144 foreach ($this->messageDataElements as $key) {
146 assert(isset($messageData[$key]));
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
150 $dataSetInstance->addCriteria($key, $messageData[$key]);
155 * Initializes configuration data from given message data array
157 * @param $messageInstance An instance of a DeliverableMessage class
160 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
161 // "Walk" throught the translation array
162 /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageInstance=' . print_r($messageInstance, TRUE));
163 foreach ($this->messageToConfig as $messageKey => $configKey) {
165 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
167 // Set the element in configuration
168 $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
171 // "Walk" throught the config-copy array
172 foreach ($this->configCopy as $targetKey => $sourceKey) {
174 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
176 // Copy from source to targetKey
177 $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
180 // Translate last exception into a status code
181 $statusCode = $this->getTranslatedStatusFromLastException();
183 // Set it in configuration (temporarily)
184 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
188 * Removes configuration data with given message data array from global
191 * @param $messageData An instance of a DeliverableMessage class
194 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
195 // "Walk" throught the translation array again
196 foreach ($this->messageToConfig as $dummy => $configKey) {
197 // Now unset this configuration entry (to save some memory)
198 $this->getConfigInstance()->unsetConfigEntry($configKey);
201 // "Walk" throught the config-copy array again
202 foreach ($this->configCopy as $configKey => $dummy) {
203 // Now unset this configuration entry (to save some memory again)
204 $this->getConfigInstance()->unsetConfigEntry($configKey);
207 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
208 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);