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\Criteria\Storing\StoreableCriteria;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
21 * A NodeMessageRequestNodeList handler
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 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 NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
53 $this->setHandlerName('message_request_node_list');
55 // Init message data array
56 $this->messageDataElements = array(
57 XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
60 // Init config-copy array
61 $this->configCopy = array(
64 // Init search data array
65 $this->searchData = array(
66 XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
71 * Creates an instance of this class
73 * @return $handlerInstance An instance of a HandleableMessage class
75 public final static function createNodeMessageRequestNodeListHandler () {
77 $handlerInstance = new NodeMessageRequestNodeListHandler();
79 // Return the prepared instance
80 return $handlerInstance;
84 * Handles data array of the message
86 * @param $messageInstance An instance of a DeliverableMessage class
87 * @param $packageInstance An instance of a Receivable class
89 * @throws RequestNotAcceptedException If this node does not accept this request
91 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
93 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
94 $nodeInstance = NodeObjectFactory::createNodeInstance();
96 // Is this node accepting announcements?
97 if (!$nodeInstance->isAcceptingNodeListRequests()) {
99 * This node is not accepting node list requests. Throw an
100 * exception to abort any further processing.
102 throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageInstance), self::EXCEPTION_REQUEST_NOT_ACCEPTED);
105 // Register the announcing node with this node
106 $this->registerNodeByMessageInstance($messageInstance);
108 // Prepare answer message to be delivered back to the other node
109 $this->prepareAnswerMessage($messageInstance, $packageInstance);
113 * Adds all required elements from given array into data set instance
115 * @param $dataSetInstance An instance of a StoreableCriteria class
116 * @param $messageInstance An instance of a DeliverableMessage class
119 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
121 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
124 foreach ($this->messageDataElements as $key) {
126 assert(isset($messageData[$key]));
129 $dataSetInstance->addCriteria($key, $messageData[$key]);
134 * Initializes configuration data from given message data array
136 * @param $messageInstance An instance of a DeliverableMessage class
139 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
141 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageInstance=' . print_r($messageInstance, TRUE));
143 // "Walk" throught the config-copy array
144 foreach ($this->configCopy as $targetKey => $sourceKey) {
146 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
148 // Copy from source to targetKey
149 $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
152 // Query local DHT for nodes except given session id
153 $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageInstance(
156 XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
157 XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
158 BaseHubNode::OBJECT_LIST_SEPARATOR
162 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.');
164 // Set it serialized in configuration (temporarily)
165 $this->getConfigInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList)));
167 // Translate last exception into a status code
168 $statusCode = $this->getTranslatedStatusFromLastException();
170 // Set it in configuration (temporarily)
171 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
175 * Removes configuration data with given message data array from global
178 * @param $messageInstance An instance of a DeliverableMessage class
181 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
182 // "Walk" throught the config-copy array again
183 foreach ($this->configCopy as $configKey => $dummy) {
184 // Now unset this configuration entry (to save some memory again)
185 $this->getConfigInstance()->unsetConfigEntry($configKey);
188 // Remove answer status/node list as well
189 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
190 $this->getConfigInstance()->unsetConfigEntry('node_list');