3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Request\NodeList;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Org\Shipsimu\Hub\Factory\Dht\DhtObjectFactory;
8 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
9 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
10 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
11 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
12 use Org\Shipsimu\Hub\Network\Receive\Receivable;
13 use Org\Shipsimu\Hub\Node\BaseHubNode;
14 use Org\Shipsimu\Hub\Template\Engine\Xml\Request\NodeList\XmlRequestNodeListTemplateEngine;
16 // Import framework stuff
17 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
18 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
22 * A NodeMessageRequestNodeList handler
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 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 NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
54 $this->setHandlerName('message_request_node_list');
56 // Init message data array
57 $this->messageDataElements = array(
58 XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
61 // Init config-copy array
62 $this->configCopy = array(
65 // Init search data array
66 $this->searchData = array(
67 XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
72 * Creates an instance of this class
74 * @return $handlerInstance An instance of a HandleableMessage class
76 public final static function createNodeMessageRequestNodeListHandler () {
78 $handlerInstance = new NodeMessageRequestNodeListHandler();
80 // Return the prepared instance
81 return $handlerInstance;
85 * Handles data array of the message
87 * @param $messageInstance An instance of a DeliverableMessage class
88 * @param $packageInstance An instance of a Receivable class
90 * @throws RequestNotAcceptedException If this node does not accept this request
92 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
95 $nodeInstance = NodeObjectFactory::createNodeInstance();
97 // Is this node accepting announcements?
98 if (!$nodeInstance->isAcceptingNodeListRequests()) {
100 * This node is not accepting node list requests. Throw an
101 * exception to abort any further processing.
103 throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageInstance), self::EXCEPTION_REQUEST_NOT_ACCEPTED);
106 // Register the announcing node with this node
107 $this->registerNodeByMessageInstance($messageInstance);
109 // Prepare answer message to be delivered back to the other node
110 $this->prepareAnswerMessage($messageInstance, $packageInstance);
114 * Adds all required elements from given array into data set instance
116 * @param $dataSetInstance An instance of a StoreableCriteria class
117 * @param $messageInstance An instance of a DeliverableMessage class
120 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
122 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
125 foreach ($this->messageDataElements as $key) {
127 assert(isset($messageData[$key]));
130 $dataSetInstance->addCriteria($key, $messageData[$key]);
135 * Initializes configuration data from given message data array
137 * @param $messageInstance An instance of a DeliverableMessage class
140 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageInstance=' . print_r($messageInstance, TRUE));
144 // "Walk" throught the config-copy array
145 foreach ($this->configCopy as $targetKey => $sourceKey) {
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
149 // Copy from source to targetKey
150 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry($targetKey, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($sourceKey));
153 // Query local DHT for nodes except given session id
154 $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageInstance(
157 XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
158 XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
159 BaseHubNode::OBJECT_LIST_SEPARATOR
163 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.');
165 // Set it serialized in configuration (temporarily)
166 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList)));
168 // Translate last exception into a status code
169 $statusCode = $this->getTranslatedStatusFromLastException();
171 // Set it in configuration (temporarily)
172 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
176 * Removes configuration data with given message data array from global
179 * @param $messageInstance An instance of a DeliverableMessage class
182 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
183 // "Walk" throught the config-copy array again
184 foreach ($this->configCopy as $configKey => $dummy) {
185 // Now unset this configuration entry (to save some memory again)
186 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry($configKey);
189 // Remove answer status/node list as well
190 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
191 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry('node_list');