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