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