]> git.mxchange.org Git - hub.git/blob
ce01a51e6d609607047c8826161c1ffce69250a1
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Hub\Handler\Node\Message\Request\NodeList;
4
5 // Import application-specific stuff
6 use Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Hub\Factory\Dht\DhtObjectFactory;
8 use Hub\Factory\Node\NodeObjectFactory;
9 use Hub\Network\Receive\Receivable;
10 use Hub\Node\BaseHubNode;
11
12 // Import framework stuff
13 use CoreFramework\Criteria\Storing\StoreableCriteria;
14 use CoreFramework\Registry\Registerable;
15
16 /**
17  * A NodeMessageRequestNodeList handler
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37  */
38 class NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47
48                 // Set handler name
49                 $this->setHandlerName('message_request_node_list');
50
51                 // Init message data array
52                 $this->messageDataElements = array(
53                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
54                 );
55
56                 // Init config-copy array
57                 $this->configCopy = array(
58                 );
59
60                 // Init search data array
61                 $this->searchData = array(
62                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
63                 );
64         }
65
66         /**
67          * Creates an instance of this class
68          *
69          * @return      $handlerInstance        An instance of a HandleableMessage class
70          */
71         public final static function createNodeMessageRequestNodeListHandler () {
72                 // Get new instance
73                 $handlerInstance = new NodeMessageRequestNodeListHandler();
74
75                 // Return the prepared instance
76                 return $handlerInstance;
77         }
78
79         /**
80          * Handles data array of the message
81          *
82          * @param       $messageData            An array with message data to handle
83          * @param       $packageInstance        An instance of a Receivable class
84          * @return      void
85          * @throws      RequestNotAcceptedException             If this node does not accept this request
86          */
87         public function handleMessageData (array $messageData, Receivable $packageInstance) {
88                 // Get node instance
89                 $nodeInstance = NodeObjectFactory::createNodeInstance();
90
91                 // Is this node accepting announcements?
92                 if (!$nodeInstance->isAcceptingNodeListRequests()) {
93                         /*
94                          * This node is not accepting node list requests. Throw an
95                          * exception to abort any further processing.
96                          */
97                         throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_REQUEST_NOT_ACCEPTED);
98                 } // END - if
99
100                 // Register the announcing node with this node
101                 $this->registerNodeByMessageData($messageData);
102
103                 // Prepare answer message to be delivered back to the other node
104                 $this->prepareAnswerMessage($messageData, $packageInstance);
105         }
106
107         /**
108          * Adds all required elements from given array into data set instance
109          *
110          * @param       $dataSetInstance        An instance of a StoreableCriteria class
111          * @param       $messageData            An array with all message data
112          * @return      void
113          */
114         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
115                 // Add generic first
116                 parent::addArrayToDataSet($dataSetInstance, $messageData);
117
118                 // Add all ements
119                 foreach ($this->messageDataElements as $key) {
120                         // Is it there?
121                         assert(isset($messageData[$key]));
122
123                         // Add it
124                         $dataSetInstance->addCriteria($key, $messageData[$key]);
125                 } // END - foreach
126         }
127
128         /**
129          * Initializes configuration data from given message data array
130          *
131          * @param       $messageData    An array with all message data
132          * @return      void
133          */
134         protected function initMessageConfigurationData (array $messageData) {
135                 // Debug message
136                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageData=' . print_r($messageData, TRUE));
137
138                 // "Walk" throught the config-copy array
139                 foreach ($this->configCopy as $targetKey => $sourceKey) {
140                         // Debug message
141                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
142
143                         // Copy from source to targetKey
144                         $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
145                 } // END - foreach
146
147                 // Query local DHT for nodes except given session id
148                 $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageData(
149                         $messageData,
150                         $this,
151                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
152                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
153                         BaseHubNode::OBJECT_LIST_SEPARATOR
154                 );
155
156                 // Debug message
157                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.');
158
159                 // Set it serialized in configuration (temporarily)
160                 $this->getConfigInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList)));
161
162                 // Translate last exception into a status code
163                 $statusCode = $this->getTranslatedStatusFromLastException();
164
165                 // Set it in configuration (temporarily)
166                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
167         }
168
169         /**
170          * Removes configuration data with given message data array from global
171          * configuration
172          *
173          * @param       $messageData    An array with all message data
174          * @return      void
175          */
176         protected function removeMessageConfigurationData (array $messageData) {
177                 // "Walk" throught the config-copy array again
178                 foreach ($this->configCopy as $configKey => $dummy) {
179                         // Now unset this configuration entry (to save some memory again)
180                         $this->getConfigInstance()->unsetConfigEntry($configKey);
181                 } // END - foreach
182
183                 // Remove answer status/node list as well
184                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
185                 $this->getConfigInstance()->unsetConfigEntry('node_list');
186         }
187
188 }