]> git.mxchange.org Git - hub.git/blob
5aaf1212d26dc992f3d5bfe6804515788b16f58c
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Handler\Answer\Node\DhtBootstrap;
4
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Factory\Dht\DhtObjectFactory;
7 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
8 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
9 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
10 use Org\Shipsimu\Hub\Network\Receive\Receivable;
11 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Dht\Bootstrap\XmlDhtBootstrapAnswerTemplateEngine;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Dht\Bootstrap\XmlDhtBootstrapTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
14
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
18 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
20
21 /**
22  * A NodeMessageDhtBootstrapAnswer handler
23  *
24  * @author              Roland Haeder <webmaster@shipsimu.org>
25  * @version             0.0.0
26  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 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 NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         private function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52
53                 // Set handler name
54                 $this->setHandlerName('message_announcement_answer');
55
56                 // Init message data array
57                 $this->messageDataElements = [
58                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
59                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
60                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
61                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
62                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
63                 ];
64
65                 // Init message-data->configuration translation array
66                 /*
67                 $this->messageToConfig = [
68                         @TODO Why commented out?
69                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'your_session_id'
72                 ];
73                 */
74
75                 // Init array
76                 $this->searchData = [
77                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
78                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
79                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
80                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
81                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
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 createNodeMessageDhtBootstrapAnswerHandler () {
91                 // Get new instance
92                 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
93
94                 // Return the prepared instance
95                 return $handlerInstance;
96         }
97
98         /**
99          * Handles data array of the message
100          *
101          * @param       $messageInstance        An instance of a DeliverableMessage class
102          * @param       $handlerInstance        An instance of a Receivable class
103          * @return      void
104          * @throws      NoDhtBootstrapAttemptedException        If this DHT has not attempted to bootstrap
105          * @todo        ~30% done
106          */
107         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
108                 // Get DHT instance
109                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
110
111                 // Has this DHT attempted to bootstrap?
112                 if (!$dhtInstance->ifDhtIsBooting()) {
113                         /*
114                          * This DHT has never bootstrapped, so it doesn't expect
115                          * announcement answer messages.
116                          */
117                         throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), HandleableMessage::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
118                 }
119
120                 // Unfinished
121                 DebugMiddleware::getSelfInstance()->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
122         }
123
124         /**
125          * Adds all required elements from given array into data set instance
126          *
127          * @param       $dataSetInstance        An instance of a StoreableCriteria class
128          * @param       $messageInstance        An instance of a DeliverableMessage class
129          * @return      void
130          */
131         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
132                 // Add generic first
133                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
134
135                 // Add all ements
136                 foreach ($this->messageDataElements as $key) {
137                         // Is it there?
138                         assert(isset($messageData[$key]));
139
140                         /*
141                          * Add it, but remove any 'my-' prefixes as they are not used in
142                          * database layer.
143                          */
144                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
145                 }
146         }
147
148 }