3 namespace Hub\Handler\Answer\Node\DhtBootstrap;
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;
11 // Import framework stuff
12 use CoreFramework\Criteria\Storing\StoreableCriteria;
13 use CoreFramework\Generic\UnsupportedOperationException;
14 use CoreFramework\Registry\Registerable;
17 * A NodeMessageDhtBootstrapAnswer handler
19 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
38 class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
49 $this->setHandlerName('message_announcement_answer');
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,
60 // Init message-data->configuration translation array
61 $this->messageToConfig = array(
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'
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',
79 $this->searchData = array(
80 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
81 XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
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 $messageData An array with message data to handle
102 * @param $packageInstance An instance of a Receivable class
104 * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap
107 public function handleMessageData (array $messageData, Receivable $packageInstance) {
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, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED);
121 $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, TRUE));
125 * Adds all required elements from given array into data set instance
127 * @param $dataSetInstance An instance of a StoreableCriteria class
128 * @param $messageData An array with all message data
131 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
133 parent::addArrayToDataSet($dataSetInstance, $messageData);
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]);
149 * Initializes configuration data from given message data array
151 * @param $messageData An array with all message data
153 * @throws UnsupportedOperationException If this method is called
155 protected function initMessageConfigurationData (array $messageData) {
156 // Please don't call this method
157 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
161 * Removes configuration data with given message data array from global
164 * @param $messageData An array with all message data
166 * @throws UnsupportedOperationException If this method is called
168 protected function removeMessageConfigurationData (array $messageData) {
169 // Please don't call this method
170 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);