]> git.mxchange.org Git - hub.git/blob
4f87b3911fc9cd747a8a22b75dd146a18b545e54
[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                 $nodeInstance = NodeObjectFactory::createNodeInstance();
113
114                 // Is this node accepting announcements?
115                 if (!$nodeInstance->isAcceptingAnnouncements()) {
116                         /*
117                          * This node is not accepting announcements, then someone wants to
118                          * announce his node to a non-bootstrap and non-master node.
119                          */
120                         throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
121                 } // END - if
122
123                 // Register the announcing node with this node
124                 $this->registerNodeByMessageData($messageData);
125
126                 // Prepare answer message to be delivered back to the other node
127                 $this->prepareAnswerMessage($messageData, $packageInstance);
128         }
129
130         /**
131          * Adds all required elements from given array into data set instance
132          *
133          * @param       $dataSetInstance        An instance of a StoreableCriteria class
134          * @param       $messageData            An array with all message data
135          * @return      void
136          */
137         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
138                 // Add generic first
139                 parent::addArrayToDataSet($dataSetInstance, $messageData);
140
141                 // Add all ements
142                 foreach ($this->messageDataElements as $key) {
143                         // Is it there?
144                         assert(isset($messageData[$key]));
145
146                         // Add it
147                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
148                         $dataSetInstance->addCriteria($key, $messageData[$key]);
149                 } // END - foreach
150         }
151
152         /**
153          * Initializes configuration data from given message data array
154          *
155          * @param       $messageData    An array with all message data
156          * @return      void
157          */
158         protected function initMessageConfigurationData (array $messageData) {
159                 // Debug message
160                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
161
162                 // "Walk" throught the translation array
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 array with all message data
192          * @return      void
193          */
194         protected function removeMessageConfigurationData (array $messageData) {
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 }