]> git.mxchange.org Git - hub.git/blob
b2454547845182caa877954c426230d2d68e056a
[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\Receive\Receivable;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
14
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Registry\Registerable;
18
19 /**
20  * A NodeMessageAnnouncement handler
21  *
22  * @author              Roland Haeder <webmaster@shipsimu.org>
23  * @version             0.0.0
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
27  *
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.
32  *
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.
37  *
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/>.
40  */
41 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50
51                 // Set handler name
52                 $this->setHandlerName('message_announcement');
53
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,
63                 );
64
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',
72                 );
73
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',
81                 );
82
83                 // Init array
84                 $this->searchData = array(
85                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
86                 );
87         }
88
89         /**
90          * Creates an instance of this class
91          *
92          * @return      $handlerInstance        An instance of a HandleableMessage class
93          */
94         public final static function createNodeMessageAnnouncementHandler () {
95                 // Get new instance
96                 $handlerInstance = new NodeMessageAnnouncementHandler();
97
98                 // Return the prepared instance
99                 return $handlerInstance;
100         }
101
102         /**
103          * Handles data array of the message
104          *
105          * @param       $messageData            An array with message data to handle
106          * @param       $packageInstance        An instance of a Receivable class
107          * @return      void
108          * @throws      AnnouncementNotAcceptedException        If this node does not accept announcements
109          */
110         public function handleMessageData (array $messageData, Receivable $packageInstance) {
111                 // Get node instance
112                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
113                 $nodeInstance = NodeObjectFactory::createNodeInstance();
114
115                 // Is this node accepting announcements?
116                 if (!$nodeInstance->isAcceptingAnnouncements()) {
117                         /*
118                          * This node is not accepting announcements, then someone wants to
119                          * announce his node to a non-bootstrap and non-master node.
120                          */
121                         throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
122                 } // END - if
123
124                 // Register the announcing node with this node
125                 $this->registerNodeByMessageData($messageData);
126
127                 // Prepare answer message to be delivered back to the other node
128                 $this->prepareAnswerMessage($messageData, $packageInstance);
129         }
130
131         /**
132          * Adds all required elements from given array into data set instance
133          *
134          * @param       $dataSetInstance        An instance of a StoreableCriteria class
135          * @param       $messageData            An array with all message data
136          * @return      void
137          */
138         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
139                 // Add generic first
140                 parent::addArrayToDataSet($dataSetInstance, $messageData);
141
142                 // Add all ements
143                 foreach ($this->messageDataElements as $key) {
144                         // Is it there?
145                         assert(isset($messageData[$key]));
146
147                         // Add it
148                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
149                         $dataSetInstance->addCriteria($key, $messageData[$key]);
150                 } // END - foreach
151         }
152
153         /**
154          * Initializes configuration data from given message data array
155          *
156          * @param       $messageData    An array with all message data
157          * @return      void
158          */
159         protected function initMessageConfigurationData (array $messageData) {
160                 // Debug message
161                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
162
163                 // "Walk" throught the translation array
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                         $this->getConfigInstance()->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                         $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->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                 $this->getConfigInstance()->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 array with all message data
193          * @return      void
194          */
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);
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                         $this->getConfigInstance()->unsetConfigEntry($configKey);
206                 } // END - foreach
207
208                 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
209                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
210         }
211
212 }