]> git.mxchange.org Git - hub.git/blob
d583bfd6cc4879d3154a76356b4af478bf93d14c
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Announcement;
4
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;
15
16 // Import framework stuff
17 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
19
20 /**
21  * A NodeMessageAnnouncement handler
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
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
28  *
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.
33  *
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.
38  *
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/>.
41  */
42 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set handler name
53                 $this->setHandlerName('message_announcement');
54
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,
64                 );
65
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',
73                 );
74
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',
82                 );
83
84                 // Init array
85                 $this->searchData = array(
86                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
87                 );
88         }
89
90         /**
91          * Creates an instance of this class
92          *
93          * @return      $handlerInstance        An instance of a HandleableMessage class
94          */
95         public final static function createNodeMessageAnnouncementHandler () {
96                 // Get new instance
97                 $handlerInstance = new NodeMessageAnnouncementHandler();
98
99                 // Return the prepared instance
100                 return $handlerInstance;
101         }
102
103         /**
104          * Handles data array of the message
105          *
106          * @param       $messageInstance        An instance of a DeliverableMessage class
107          * @param       $packageInstance        An instance of a Receivable class
108          * @return      void
109          * @throws      AnnouncementNotAcceptedException        If this node does not accept announcements
110          */
111         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
112                 // Get node instance
113                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
114                 $nodeInstance = NodeObjectFactory::createNodeInstance();
115
116                 // Is this node accepting announcements?
117                 if (!$nodeInstance->isAcceptingAnnouncements()) {
118                         /*
119                          * This node is not accepting announcements, then someone wants to
120                          * announce his node to a non-bootstrap and non-master node.
121                          */
122                         throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageInstance), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
123                 } // END - if
124
125                 // Register the announcing node with this node
126                 $this->registerNodeByMessageInstance($messageInstance);
127
128                 // Prepare answer message to be delivered back to the other node
129                 $this->prepareAnswerMessage($messageInstance, $packageInstance);
130         }
131
132         /**
133          * Adds all required elements from given array into data set instance
134          *
135          * @param       $dataSetInstance        An instance of a StoreableCriteria class
136          * @param       $messageInstance        An instance of a DeliverableMessage class
137          * @return      void
138          */
139         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
140                 // Add generic first
141                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
142
143                 // Add all ements
144                 foreach ($this->messageDataElements as $key) {
145                         // Is it there?
146                         assert(isset($messageData[$key]));
147
148                         // Add it
149                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
150                         $dataSetInstance->addCriteria($key, $messageData[$key]);
151                 } // END - foreach
152         }
153
154         /**
155          * Initializes configuration data from given message data array
156          *
157          * @param       $messageInstance        An instance of a DeliverableMessage class
158          * @return      void
159          */
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) {
164                         // Debug message
165                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
166
167                         // Set the element in configuration
168                         $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
169                 } // END - foreach
170
171                 // "Walk" throught the config-copy array
172                 foreach ($this->configCopy as $targetKey => $sourceKey) {
173                         // Debug message
174                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
175
176                         // Copy from source to targetKey
177                         $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
178                 } // END - foreach
179
180                 // Translate last exception into a status code
181                 $statusCode = $this->getTranslatedStatusFromLastException();
182
183                 // Set it in configuration (temporarily)
184                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
185         }
186
187         /**
188          * Removes configuration data with given message data array from global
189          * configuration
190          *
191          * @param       $messageData    An instance of a DeliverableMessage class
192          * @return      void
193          */
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);
199                 } // END - foreach
200
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);
205                 } // END - foreach
206
207                 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
208                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
209         }
210
211 }