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