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 - 2020 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
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'
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,
85 * Creates an instance of this class
87 * @return $handlerInstance An instance of a HandleableMessage class
89 public final static function createNodeMessageDhtBootstrapAnswerHandler () {
91 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
93 // Return the prepared instance
94 return $handlerInstance;
98 * Handles data array of the message
100 * @param $messageInstance An instance of a DeliverableMessage class
101 * @param $handlerInstance An instance of a Receivable class
103 * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap
106 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
108 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
110 // Has this DHT attempted to bootstrap?
111 if (!$dhtInstance->ifDhtIsBooting()) {
113 * This DHT has never bootstrapped, so it doesn't expect
114 * announcement answer messages.
116 throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
120 $this->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
124 * Adds all required elements from given array into data set instance
126 * @param $dataSetInstance An instance of a StoreableCriteria class
127 * @param $messageInstance An instance of a DeliverableMessage class
130 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
132 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
135 foreach ($this->messageDataElements as $key) {
137 assert(isset($messageData[$key]));
140 * Add it, but remove any 'my-' prefixes as they are not used in
143 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);