]> git.mxchange.org Git - hub.git/blob
3fc075abf030ec0c89ef1cd097fc411270ae2051
[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\Registry\Registerable;
19
20 /**
21  * A NodeMessageDhtBootstrapAnswer handler
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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 NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set handler name
53                 $this->setHandlerName('message_announcement_answer');
54
55                 // Init message data array
56                 $this->messageDataElements = [
57                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
58                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
59                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
60                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
61                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
62                 ];
63
64                 // Init message-data->configuration translation array
65                 /*
66                 $this->messageToConfig = [
67                         @TODO Why commented out?
68                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
69                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
70                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'your_session_id'
71                 ];
72                 */
73
74                 // Init array
75                 $this->searchData = [
76                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
77                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
78                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
79                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
80                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
81                 ];
82         }
83
84         /**
85          * Creates an instance of this class
86          *
87          * @return      $handlerInstance        An instance of a HandleableMessage class
88          */
89         public final static function createNodeMessageDhtBootstrapAnswerHandler () {
90                 // Get new instance
91                 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
92
93                 // Return the prepared instance
94                 return $handlerInstance;
95         }
96
97         /**
98          * Handles data array of the message
99          *
100          * @param       $messageInstance        An instance of a DeliverableMessage class
101          * @param       $handlerInstance        An instance of a Receivable class
102          * @return      void
103          * @throws      NoDhtBootstrapAttemptedException        If this DHT has not attempted to bootstrap
104          * @todo        ~30% done
105          */
106         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
107                 // Get DHT instance
108                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
109
110                 // Has this DHT attempted to bootstrap?
111                 if (!$dhtInstance->ifDhtIsBooting()) {
112                         /*
113                          * This DHT has never bootstrapped, so it doesn't expect
114                          * announcement answer messages.
115                          */
116                         throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
117                 }
118
119                 // Unfinished
120                 $this->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
121         }
122
123         /**
124          * Adds all required elements from given array into data set instance
125          *
126          * @param       $dataSetInstance        An instance of a StoreableCriteria class
127          * @param       $messageInstance        An instance of a DeliverableMessage class
128          * @return      void
129          */
130         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
131                 // Add generic first
132                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
133
134                 // Add all ements
135                 foreach ($this->messageDataElements as $key) {
136                         // Is it there?
137                         assert(isset($messageData[$key]));
138
139                         /*
140                          * Add it, but remove any 'my-' prefixes as they are not used in
141                          * database layer.
142                          */
143                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
144                 }
145         }
146
147 }