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