]> git.mxchange.org Git - hub.git/blob
9c08b8c5aa6c84caae0284a32d5df181f3940a98
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Request\NodeList;
4
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;
15
16 // Import framework stuff
17 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
19
20 /**
21  * A NodeMessageRequestNodeList handler
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
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
28  *
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.
33  *
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.
38  *
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/>.
41  */
42 class NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set handler name
53                 $this->setHandlerName('message_request_node_list');
54
55                 // Init message data array
56                 $this->messageDataElements = array(
57                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
58                 );
59
60                 // Init config-copy array
61                 $this->configCopy = array(
62                 );
63
64                 // Init search data array
65                 $this->searchData = array(
66                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
67                 );
68         }
69
70         /**
71          * Creates an instance of this class
72          *
73          * @return      $handlerInstance        An instance of a HandleableMessage class
74          */
75         public final static function createNodeMessageRequestNodeListHandler () {
76                 // Get new instance
77                 $handlerInstance = new NodeMessageRequestNodeListHandler();
78
79                 // Return the prepared instance
80                 return $handlerInstance;
81         }
82
83         /**
84          * Handles data array of the message
85          *
86          * @param       $messageInstance        An instance of a DeliverableMessage class
87          * @param       $packageInstance        An instance of a Receivable class
88          * @return      void
89          * @throws      RequestNotAcceptedException             If this node does not accept this request
90          */
91         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
92                 // Get node instance
93                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
94                 $nodeInstance = NodeObjectFactory::createNodeInstance();
95
96                 // Is this node accepting announcements?
97                 if (!$nodeInstance->isAcceptingNodeListRequests()) {
98                         /*
99                          * This node is not accepting node list requests. Throw an
100                          * exception to abort any further processing.
101                          */
102                         throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageInstance), self::EXCEPTION_REQUEST_NOT_ACCEPTED);
103                 } // END - if
104
105                 // Register the announcing node with this node
106                 $this->registerNodeByMessageInstance($messageInstance);
107
108                 // Prepare answer message to be delivered back to the other node
109                 $this->prepareAnswerMessage($messageInstance, $packageInstance);
110         }
111
112         /**
113          * Adds all required elements from given array into data set instance
114          *
115          * @param       $dataSetInstance        An instance of a StoreableCriteria class
116          * @param       $messageInstance        An instance of a DeliverableMessage class
117          * @return      void
118          */
119         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
120                 // Add generic first
121                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
122
123                 // Add all ements
124                 foreach ($this->messageDataElements as $key) {
125                         // Is it there?
126                         assert(isset($messageData[$key]));
127
128                         // Add it
129                         $dataSetInstance->addCriteria($key, $messageData[$key]);
130                 } // END - foreach
131         }
132
133         /**
134          * Initializes configuration data from given message data array
135          *
136          * @param       $messageInstance        An instance of a DeliverableMessage class
137          * @return      void
138          */
139         protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
140                 // Debug message
141                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageInstance=' . print_r($messageInstance, TRUE));
142
143                 // "Walk" throught the config-copy array
144                 foreach ($this->configCopy as $targetKey => $sourceKey) {
145                         // Debug message
146                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
147
148                         // Copy from source to targetKey
149                         $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
150                 } // END - foreach
151
152                 // Query local DHT for nodes except given session id
153                 $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageInstance(
154                         $messageInstance,
155                         $this,
156                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
157                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
158                         BaseHubNode::OBJECT_LIST_SEPARATOR
159                 );
160
161                 // Debug message
162                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.');
163
164                 // Set it serialized in configuration (temporarily)
165                 $this->getConfigInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList)));
166
167                 // Translate last exception into a status code
168                 $statusCode = $this->getTranslatedStatusFromLastException();
169
170                 // Set it in configuration (temporarily)
171                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
172         }
173
174         /**
175          * Removes configuration data with given message data array from global
176          * configuration
177          *
178          * @param       $messageInstance        An instance of a DeliverableMessage class
179          * @return      void
180          */
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);
186                 } // END - foreach
187
188                 // Remove answer status/node list as well
189                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
190                 $this->getConfigInstance()->unsetConfigEntry('node_list');
191         }
192
193 }