]> git.mxchange.org Git - hub.git/blob
40ea9932e8c725c77a0bd13e258ee2f153f06a38
[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\NodeDistributedHashTableDatabaseFrontend;
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 - 2020 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         private 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 = [
58                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
59                 ];
60
61                 // Init search data array
62                 $this->searchData = [
63                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID,
64                 ];
65         }
66
67         /**
68          * Creates an instance of this class
69          *
70          * @return      $handlerInstance        An instance of a HandleableMessage class
71          */
72         public final static function createNodeMessageRequestNodeListHandler () {
73                 // Get new instance
74                 $handlerInstance = new NodeMessageRequestNodeListHandler();
75
76                 // Return the prepared instance
77                 return $handlerInstance;
78         }
79
80         /**
81          * Handles data array of the message
82          *
83          * @param       $messageInstance        An instance of a DeliverableMessage class
84          * @param       $handlerInstance        An instance of a Receivable class
85          * @return      void
86          * @throws      RequestNotAcceptedException             If this node does not accept this request
87          */
88         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
89                 // Get node instance
90                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
91                 $nodeInstance = NodeObjectFactory::createNodeInstance();
92
93                 // Is this node accepting announcements?
94                 if (!$nodeInstance->isAcceptingNodeListRequests()) {
95                         /*
96                          * This node is not accepting node list requests. Throw an
97                          * exception to abort any further processing.
98                          */
99                         throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageInstance), self::EXCEPTION_REQUEST_NOT_ACCEPTED);
100                 }
101
102                 // Register the announcing node with this node
103                 $this->registerNodeByMessageInstance($messageInstance);
104
105                 // Prepare answer message to be delivered back to the other node
106                 $this->prepareAnswerMessage($messageInstance, $handlerInstance);
107         }
108
109         /**
110          * Adds all required elements from given array into data set instance
111          *
112          * @param       $dataSetInstance        An instance of a StoreableCriteria class
113          * @param       $messageInstance        An instance of a DeliverableMessage class
114          * @return      void
115          */
116         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
117                 // Add generic first
118                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
119
120                 // Add all ements
121                 foreach ($this->messageDataElements as $key) {
122                         // Is it there?
123                         assert(isset($messageData[$key]));
124
125                         // Add it
126                         $dataSetInstance->addCriteria($key, $messageData[$key]);
127                 }
128         }
129
130 }