]> git.mxchange.org Git - hub.git/blob
debb99b65fd970ab8f7a6f86cebe6856994567a1
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Hub\Handler\Node\Message\Announcement;
4
5 // Import application-specific stuff
6 use Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Hub\Factory\Node\NodeObjectFactory;
8 use Hub\Generic\BaseHubSystem;
9 use Hub\Network\Receive\Receivable;
10
11 // Import framework stuff
12 use CoreFramework\Criteria\Storing\StoreableCriteria;
13 use CoreFramework\Registry\Registerable;
14
15 /**
16  * A NodeMessageAnnouncement handler
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36  */
37 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46
47                 // Set handler name
48                 $this->setHandlerName('message_announcement');
49
50                 // Init message data array
51                 $this->messageDataElements = array(
52                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
53                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
54                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
55                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
56                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
57                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
58                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
59                 );
60
61                 // Init message-data->configuration translation array
62                 $this->messageToConfig = array(
63                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
64                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
65                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID          => 'your_node_id',
66                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID       => 'your_session_id',
67                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
68                 );
69
70                 // Init config-copy array
71                 $this->configCopy = array(
72                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
73                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
74                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS      => 'node_status',
75                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID       => 'session_id',
76                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
77                 );
78
79                 // Init array
80                 $this->searchData = array(
81                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
82                 );
83         }
84
85         /**
86          * Creates an instance of this class
87          *
88          * @return      $handlerInstance        An instance of a HandleableMessage class
89          */
90         public final static function createNodeMessageAnnouncementHandler () {
91                 // Get new instance
92                 $handlerInstance = new NodeMessageAnnouncementHandler();
93
94                 // Return the prepared instance
95                 return $handlerInstance;
96         }
97
98         /**
99          * Handles data array of the message
100          *
101          * @param       $messageData            An array with message data to handle
102          * @param       $packageInstance        An instance of a Receivable class
103          * @return      void
104          * @throws      AnnouncementNotAcceptedException        If this node does not accept announcements
105          */
106         public function handleMessageData (array $messageData, Receivable $packageInstance) {
107                 // Get node instance
108                 $nodeInstance = NodeObjectFactory::createNodeInstance();
109
110                 // Is this node accepting announcements?
111                 if (!$nodeInstance->isAcceptingAnnouncements()) {
112                         /*
113                          * This node is not accepting announcements, then someone wants to
114                          * announce his node to a non-bootstrap and non-master node.
115                          */
116                         throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
117                 } // END - if
118
119                 // Register the announcing node with this node
120                 $this->registerNodeByMessageData($messageData);
121
122                 // Prepare answer message to be delivered back to the other node
123                 $this->prepareAnswerMessage($messageData, $packageInstance);
124         }
125
126         /**
127          * Adds all required elements from given array into data set instance
128          *
129          * @param       $dataSetInstance        An instance of a StoreableCriteria class
130          * @param       $messageData            An array with all message data
131          * @return      void
132          */
133         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
134                 // Add generic first
135                 parent::addArrayToDataSet($dataSetInstance, $messageData);
136
137                 // Add all ements
138                 foreach ($this->messageDataElements as $key) {
139                         // Is it there?
140                         assert(isset($messageData[$key]));
141
142                         // Add it
143                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
144                         $dataSetInstance->addCriteria($key, $messageData[$key]);
145                 } // END - foreach
146         }
147
148         /**
149          * Initializes configuration data from given message data array
150          *
151          * @param       $messageData    An array with all message data
152          * @return      void
153          */
154         protected function initMessageConfigurationData (array $messageData) {
155                 // Debug message
156                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
157
158                 // "Walk" throught the translation array
159                 foreach ($this->messageToConfig as $messageKey => $configKey) {
160                         // Debug message
161                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
162
163                         // Set the element in configuration
164                         $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
165                 } // END - foreach
166
167                 // "Walk" throught the config-copy array
168                 foreach ($this->configCopy as $targetKey => $sourceKey) {
169                         // Debug message
170                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
171
172                         // Copy from source to targetKey
173                         $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
174                 } // END - foreach
175
176                 // Translate last exception into a status code
177                 $statusCode = $this->getTranslatedStatusFromLastException();
178
179                 // Set it in configuration (temporarily)
180                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
181         }
182
183         /**
184          * Removes configuration data with given message data array from global
185          * configuration
186          *
187          * @param       $messageData    An array with all message data
188          * @return      void
189          */
190         protected function removeMessageConfigurationData (array $messageData) {
191                 // "Walk" throught the translation array again
192                 foreach ($this->messageToConfig as $dummy => $configKey) {
193                         // Now unset this configuration entry (to save some memory)
194                         $this->getConfigInstance()->unsetConfigEntry($configKey);
195                 } // END - foreach
196
197                 // "Walk" throught the config-copy array again
198                 foreach ($this->configCopy as $configKey => $dummy) {
199                         // Now unset this configuration entry (to save some memory again)
200                         $this->getConfigInstance()->unsetConfigEntry($configKey);
201                 } // END - foreach
202
203                 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
204                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
205         }
206
207 }