]> git.mxchange.org Git - hub.git/blob
1f89332a9d3d5ee86e76465e54729cd145ffaa90
[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 - 2018 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                 $this->messageToConfig = [
66                 /*
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 config-copy array
75                 $this->configCopy = [
76                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address',
77                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address',
78                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS      => 'node_status',
79                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'session_id',
80                 ];
81
82                 // Init array
83                 $this->searchData = [
84                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
85                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
86                 ];
87         }
88
89         /**
90          * Creates an instance of this class
91          *
92          * @return      $handlerInstance        An instance of a HandleableMessage class
93          */
94         public final static function createNodeMessageDhtBootstrapAnswerHandler () {
95                 // Get new instance
96                 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
97
98                 // Return the prepared instance
99                 return $handlerInstance;
100         }
101
102         /**
103          * Handles data array of the message
104          *
105          * @param       $messageInstance        An instance of a DeliverableMessage class
106          * @param       $packageInstance        An instance of a Receivable class
107          * @return      void
108          * @throws      NoDhtBootstrapAttemptedException        If this DHT has not attempted to bootstrap
109          * @todo        ~30% done
110          */
111         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
112                 // Get DHT instance
113                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
114
115                 // Has this DHT attempted to bootstrap?
116                 if (!$dhtInstance->ifDhtIsBooting()) {
117                         /*
118                          * This DHT has never bootstrapped, so it doesn't expect
119                          * announcement answer messages.
120                          */
121                         throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
122                 } // END - if
123
124                 // Unfinished
125                 $this->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
126         }
127
128         /**
129          * Adds all required elements from given array into data set instance
130          *
131          * @param       $dataSetInstance        An instance of a StoreableCriteria class
132          * @param       $messageInstance        An instance of a DeliverableMessage class
133          * @return      void
134          */
135         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
136                 // Add generic first
137                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
138
139                 // Add all ements
140                 foreach ($this->messageDataElements as $key) {
141                         // Is it there?
142                         assert(isset($messageData[$key]));
143
144                         /*
145                          * Add it, but remove any 'my-' prefixes as they are not used in
146                          * database layer.
147                          */
148                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
149                 } // END - foreach
150         }
151
152         /**
153          * Initializes configuration data from given message data array
154          *
155          * @param       $messageInstance        An instance of a DeliverableMessage class
156          * @return      void
157          * @throws      UnsupportedOperationException   If this method is called
158          */
159         protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
160                 // Please don't call this method
161                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
162         }
163
164         /**
165          * Removes configuration data with given message data array from global
166          * configuration
167          *
168          * @param       $messageInstance        An instance of a DeliverableMessage class
169          * @return      void
170          * @throws      UnsupportedOperationException   If this method is called
171          */
172         protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
173                 // Please don't call this method
174                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
175         }
176 }
177
178 // [EOF]
179 ?>