]> git.mxchange.org Git - hub.git/blob
e47c57a422d18e00f1e977f44db87d0e0fc49372
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Hub\Handler\Answer\Node\DhtBootstrap;
4
5 // Import application-specific stuff
6 use Hub\Factory\Dht\DhtObjectFactory;
7 use Hub\Network\Receive\Receivable;
8 use Hub\Template\Engine\Xml\Dht\Bootstrap\XmlDhtBootstrapTemplateEngine;
9 use Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
10
11 // Import framework stuff
12 use CoreFramework\Criteria\Storing\StoreableCriteria;
13 use CoreFramework\Generic\UnsupportedOperationException;
14 use CoreFramework\Registry\Registerable;
15
16 /**
17  * A NodeMessageDhtBootstrapAnswer handler
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37  */
38 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47
48                 // Set handler name
49                 $this->setHandlerName('message_announcement_answer');
50
51                 // Init message data array
52                 $this->messageDataElements = array(
53                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
54                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
55                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
56                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
57                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
58                 );
59
60                 // Init message-data->configuration translation array
61                 $this->messageToConfig = array(
62                 /*
63                         @TODO Why commented out?
64                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
65                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
66                         XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'your_session_id'
67                 */
68                 );
69
70                 // Init config-copy array
71                 $this->configCopy = array(
72                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address',
73                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address',
74                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS      => 'node_status',
75                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'session_id',
76                 );
77
78                 // Init array
79                 $this->searchData = array(
80                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
81                         XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
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       $messageData            An array with message data to handle
102          * @param       $packageInstance        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 (array $messageData, Receivable $packageInstance) {
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, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
118                 } // END - if
119
120                 // Unfinished
121                 $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, 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       $messageData            An array with all message data
129          * @return      void
130          */
131         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
132                 // Add generic first
133                 parent::addArrayToDataSet($dataSetInstance, $messageData);
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                 } // END - foreach
146         }
147
148         /**
149          * Initializes configuration data from given message data array
150          *
151          * @param       $messageData    An array with all message data
152          * @return      void
153          * @throws      UnsupportedOperationException   If this method is called
154          */
155         protected function initMessageConfigurationData (array $messageData) {
156                 // Please don't call this method
157                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
158         }
159
160         /**
161          * Removes configuration data with given message data array from global
162          * configuration
163          *
164          * @param       $messageData    An array with all message data
165          * @return      void
166          * @throws      UnsupportedOperationException   If this method is called
167          */
168         protected function removeMessageConfigurationData (array $messageData) {
169                 // Please don't call this method
170                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
171         }
172 }
173
174 // [EOF]
175 ?>