]> git.mxchange.org Git - hub.git/blob
856de438f23ba4ab01e77f9879fd17045464900d
[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\Receive\Receivable;
10 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Dht\Bootstrap\XmlDhtBootstrapAnswerTemplateEngine;
11 use Org\Shipsimu\Hub\Template\Engine\Xml\Dht\Bootstrap\XmlDhtBootstrapTemplateEngine;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
13
14 // Import framework stuff
15 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
16 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
17 use Org\Mxchange\CoreFramework\Registry\Registerable;
18
19 /**
20  * A NodeMessageDhtBootstrapAnswer handler
21  *
22  * @author              Roland Haeder <webmaster@shipsimu.org>
23  * @version             0.0.0
24  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
25  * @license             GNU GPL 3.0 or any newer version
26  * @link                http://www.shipsimu.org
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
40  */
41 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50
51                 // Set handler name
52                 $this->setHandlerName('message_announcement_answer');
53
54                 // Init message data array
55                 $this->messageDataElements = array(
56                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
57                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
58                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
59                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
60                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
61                 );
62
63                 // Init message-data->configuration translation array
64                 $this->messageToConfig = array(
65                 /*
66                         @TODO Why commented out?
67                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
68                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
69                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'your_session_id'
70                 */
71                 );
72
73                 // Init config-copy array
74                 $this->configCopy = array(
75                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address',
76                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address',
77                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS      => 'node_status',
78                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'session_id',
79                 );
80
81                 // Init array
82                 $this->searchData = array(
83                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
84                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
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 createNodeMessageDhtBootstrapAnswerHandler () {
94                 // Get new instance
95                 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
96
97                 // Return the prepared instance
98                 return $handlerInstance;
99         }
100
101         /**
102          * Handles data array of the message
103          *
104          * @param       $messageData            An array with message data to handle
105          * @param       $packageInstance        An instance of a Receivable class
106          * @return      void
107          * @throws      NoDhtBootstrapAttemptedException        If this DHT has not attempted to bootstrap
108          * @todo        ~30% done
109          */
110         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
111                 // Get DHT instance
112                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
113
114                 // Has this DHT attempted to bootstrap?
115                 if (!$dhtInstance->ifDhtIsBooting()) {
116                         /*
117                          * This DHT has never bootstrapped, so it doesn't expect
118                          * announcement answer messages.
119                          */
120                         throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
121                 } // END - if
122
123                 // Unfinished
124                 $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, TRUE));
125         }
126
127         /**
128          * Adds all required elements from given array into data set instance
129          *
130          * @param       $dataSetInstance        An instance of a StoreableCriteria class
131          * @param       $messageData            An array with all message data
132          * @return      void
133          */
134         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
135                 // Add generic first
136                 parent::addArrayToDataSet($dataSetInstance, $messageData);
137
138                 // Add all ements
139                 foreach ($this->messageDataElements as $key) {
140                         // Is it there?
141                         assert(isset($messageData[$key]));
142
143                         /*
144                          * Add it, but remove any 'my-' prefixes as they are not used in
145                          * database layer.
146                          */
147                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
148                 } // END - foreach
149         }
150
151         /**
152          * Initializes configuration data from given message data array
153          *
154          * @param       $messageData    An array with all message data
155          * @return      void
156          * @throws      UnsupportedOperationException   If this method is called
157          */
158         protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
159                 // Please don't call this method
160                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
161         }
162
163         /**
164          * Removes configuration data with given message data array from global
165          * configuration
166          *
167          * @param       $messageData    An array with all message data
168          * @return      void
169          * @throws      UnsupportedOperationException   If this method is called
170          */
171         protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
172                 // Please don't call this method
173                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
174         }
175 }
176
177 // [EOF]
178 ?>