3 namespace Org\Shipsimu\Hub\Handler\Answer\Node\DhtBootstrap;
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;
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;
21 * A NodeMessageDhtBootstrapAnswer handler
23 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
42 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
53 $this->setHandlerName('message_announcement_answer');
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,
64 // Init message-data->configuration translation array
65 $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'
74 // Init config-copy array
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',
84 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
85 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
90 * Creates an instance of this class
92 * @return $handlerInstance An instance of a HandleableMessage class
94 public final static function createNodeMessageDhtBootstrapAnswerHandler () {
96 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
98 // Return the prepared instance
99 return $handlerInstance;
103 * Handles data array of the message
105 * @param $messageInstance An instance of a DeliverableMessage class
106 * @param $packageInstance An instance of a Receivable class
108 * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap
111 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
113 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
115 // Has this DHT attempted to bootstrap?
116 if (!$dhtInstance->ifDhtIsBooting()) {
118 * This DHT has never bootstrapped, so it doesn't expect
119 * announcement answer messages.
121 throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
125 $this->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
129 * Adds all required elements from given array into data set instance
131 * @param $dataSetInstance An instance of a StoreableCriteria class
132 * @param $messageInstance An instance of a DeliverableMessage class
135 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
137 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
140 foreach ($this->messageDataElements as $key) {
142 assert(isset($messageData[$key]));
145 * Add it, but remove any 'my-' prefixes as they are not used in
148 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
153 * Initializes configuration data from given message data array
155 * @param $messageInstance An instance of a DeliverableMessage class
157 * @throws UnsupportedOperationException If this method is called
159 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
160 // Please don't call this method
161 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
165 * Removes configuration data with given message data array from global
168 * @param $messageInstance An instance of a DeliverableMessage class
170 * @throws UnsupportedOperationException If this method is called
172 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
173 // Please don't call this method
174 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);