]> git.mxchange.org Git - hub.git/blob
c965cc8b7b13697d7b729a0e706be2224063eac9
[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\Handler\Message\BaseMessageHandler;
9 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
10 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
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\Bootstrap\FrameworkBootstrap;
17 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
19
20 /**
21  * A NodeMessageAnnouncement handler
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Hub Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
41  */
42 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         private function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set handler name
53                 $this->setHandlerName('message_announcement');
54
55                 // Init message data array
56                 $this->messageDataElements = [
57                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
58                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
59                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
60                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
61                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
62                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
63                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
64                 ];
65
66                 // Init message-data->configuration translation array
67                 /*
68                 $this->messageToConfig = [
69                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID          => 'your_node_id',
72                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID       => 'your_session_id',
73                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
74                 ];
75                 */
76
77                 // Init array
78                 $this->searchData = [
79                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
80                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
81                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
82                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
83                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
84                 ];
85         }
86
87         /**
88          * Creates an instance of this class
89          *
90          * @return      $handlerInstance        An instance of a HandleableMessage class
91          */
92         public final static function createNodeMessageAnnouncementHandler () {
93                 // Get new instance
94                 $handlerInstance = new NodeMessageAnnouncementHandler();
95
96                 // Return the prepared instance
97                 return $handlerInstance;
98         }
99
100         /**
101          * Handles data array of the message
102          *
103          * @param       $messageInstance        An instance of a DeliverableMessage class
104          * @param       $handlerInstance        An instance of a Receivable class
105          * @return      void
106          * @throws      AnnouncementNotAcceptedException        If this node does not accept announcements
107          */
108         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
109                 // Get node instance
110                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
111                 $nodeInstance = NodeObjectFactory::createNodeInstance();
112
113                 // Is this node accepting announcements?
114                 if (!$nodeInstance->isAcceptingAnnouncements()) {
115                         /*
116                          * This node is not accepting announcements, then someone wants to
117                          * announce his node to a non-bootstrap and non-master node.
118                          */
119                         throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageInstance), HandleableMessage::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
120                 }
121
122                 // Register the announcing node with this node
123                 $this->registerNodeByMessageInstance($messageInstance);
124
125                 // Prepare answer message to be delivered back to the other node
126                 $this->prepareAnswerMessage($messageInstance, $handlerInstance);
127         }
128
129         /**
130          * Adds all required elements from given array into data set instance
131          *
132          * @param       $dataSetInstance        An instance of a StoreableCriteria class
133          * @param       $messageInstance        An instance of a DeliverableMessage class
134          * @return      void
135          */
136         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
137                 // Add generic first
138                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
139
140                 // Add all ements
141                 foreach ($this->messageDataElements as $key) {
142                         // Is it there?
143                         assert(isset($messageData[$key]));
144
145                         // Add it
146                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
147                         $dataSetInstance->addCriteria($key, $messageData[$key]);
148                 }
149         }
150
151 }