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\Middleware\Debug\DebugMiddleware;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
22 * A NodeMessageDhtBootstrapAnswer handler
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Hub Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
45 * Protected constructor
49 private function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
54 $this->setHandlerName('message_announcement_answer');
56 // Init message data array
57 $this->messageDataElements = [
58 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
59 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
60 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
61 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
62 BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
65 // Init message-data->configuration translation array
67 $this->messageToConfig = [
68 @TODO Why commented out?
69 XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70 XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71 XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'your_session_id'
77 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
78 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
79 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
80 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
81 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
86 * Creates an instance of this class
88 * @return $handlerInstance An instance of a HandleableMessage class
90 public final static function createNodeMessageDhtBootstrapAnswerHandler () {
92 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
94 // Return the prepared instance
95 return $handlerInstance;
99 * Handles data array of the message
101 * @param $messageInstance An instance of a DeliverableMessage class
102 * @param $handlerInstance An instance of a Receivable class
104 * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap
107 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
109 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
111 // Has this DHT attempted to bootstrap?
112 if (!$dhtInstance->ifDhtIsBooting()) {
114 * This DHT has never bootstrapped, so it doesn't expect
115 * announcement answer messages.
117 throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageInstance), HandleableMessage::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
121 DebugMiddleware::getSelfInstance()->partialStub('UNHANDLED: messageInstance=' . print_r($messageInstance, TRUE));
125 * Adds all required elements from given array into data set instance
127 * @param $dataSetInstance An instance of a StoreableCriteria class
128 * @param $messageInstance An instance of a DeliverableMessage class
131 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
133 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
136 foreach ($this->messageDataElements as $key) {
138 assert(isset($messageData[$key]));
141 * Add it, but remove any 'my-' prefixes as they are not used in
144 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);