]> git.mxchange.org Git - hub.git/blob
f8990da2733a7b2ac0ab852038c4bd10a99e0814
[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\NodeDistributedHashTableDatabaseFrontend;
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 - 2020 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 = [
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                 /*
69                 $this->messageToConfig = [
70                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
71                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
72                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID          => 'your_node_id',
73                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID       => 'your_session_id',
74                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
75                 ];
76                 */
77
78                 // Init array
79                 $this->searchData = [
80                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
81                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
82                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
83                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
84                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
85                 ];
86         }
87
88         /**
89          * Creates an instance of this class
90          *
91          * @return      $handlerInstance        An instance of a HandleableMessage class
92          */
93         public final static function createNodeMessageAnnouncementHandler () {
94                 // Get new instance
95                 $handlerInstance = new NodeMessageAnnouncementHandler();
96
97                 // Return the prepared instance
98                 return $handlerInstance;
99         }
100
101         /**
102          * Handles data array of the message
103          *
104          * @param       $messageInstance        An instance of a DeliverableMessage class
105          * @param       $handlerInstance        An instance of a Receivable class
106          * @return      void
107          * @throws      AnnouncementNotAcceptedException        If this node does not accept announcements
108          */
109         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
110                 // Get node instance
111                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating 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, $messageInstance), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
121                 }
122
123                 // Register the announcing node with this node
124                 $this->registerNodeByMessageInstance($messageInstance);
125
126                 // Prepare answer message to be delivered back to the other node
127                 $this->prepareAnswerMessage($messageInstance, $handlerInstance);
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       $messageInstance        An instance of a DeliverableMessage class
135          * @return      void
136          */
137         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
138                 // Add generic first
139                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
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                 }
150         }
151
152 }