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\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;
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;
20 * A NodeMessageDhtBootstrapAnswer handler
22 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
41 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
52 $this->setHandlerName('message_announcement_answer');
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,
63 // Init message-data->configuration translation array
64 $this->messageToConfig = array(
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'
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',
82 $this->searchData = array(
83 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
84 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
89 * Creates an instance of this class
91 * @return $handlerInstance An instance of a HandleableMessage class
93 public final static function createNodeMessageDhtBootstrapAnswerHandler () {
95 $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler();
97 // Return the prepared instance
98 return $handlerInstance;
102 * Handles data array of the message
104 * @param $messageData An array with message data to handle
105 * @param $packageInstance An instance of a Receivable class
107 * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap
110 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
112 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
114 // Has this DHT attempted to bootstrap?
115 if (!$dhtInstance->ifDhtIsBooting()) {
117 * This DHT has never bootstrapped, so it doesn't expect
118 * announcement answer messages.
120 throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
124 $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, TRUE));
128 * Adds all required elements from given array into data set instance
130 * @param $dataSetInstance An instance of a StoreableCriteria class
131 * @param $messageData An array with all message data
134 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
136 parent::addArrayToDataSet($dataSetInstance, $messageData);
139 foreach ($this->messageDataElements as $key) {
141 assert(isset($messageData[$key]));
144 * Add it, but remove any 'my-' prefixes as they are not used in
147 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
152 * Initializes configuration data from given message data array
154 * @param $messageData An array with all message data
156 * @throws UnsupportedOperationException If this method is called
158 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
159 // Please don't call this method
160 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
164 * Removes configuration data with given message data array from global
167 * @param $messageData An array with all message data
169 * @throws UnsupportedOperationException If this method is called
171 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
172 // Please don't call this method
173 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);