]> git.mxchange.org Git - hub.git/blob
aefa1735e0caeb7130dd4da0f06e1d9250f9ae99
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Node\Helper\Answer\Request\NodeList;
4
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Factory\Handler\Network\NetworkPackageHandlerFactory;
7 use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory;
8 use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler;
9 use Org\Shipsimu\Hub\Helper\Node\HelpableNode;
10 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
11 use Org\Shipsimu\Hub\Node\Node;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Request\NodeList\XmlRequestNodeListTemplateEngine;
13
14 // Import framework stuff
15 use Org\Mxchange\CoreFramework\Factory\Template\XmlTemplateEngineFactory;
16
17 /**
18  * A RequestNodeListMessageAnswer node helper class
19  *
20  * @author              Roland Haeder <webmaster@shipsimu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Hub Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.shipsimu.org
25  * @todo                Find an interface for hub helper
26  *
27  * This program is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program. If not, see <http://www.gnu.org/licenses/>.
39  */
40 class NodeRequestNodeListMessageAnswerHelper extends BaseHubAnswerHelper implements HelpableNode {
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         private function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Init package tags
51                 $this->setPackageTags(array('request_node_list_answer'));
52         }
53
54         /**
55          * Creates the helper class
56          *
57          * @param       $messageInstance        An instance of a DeliverableMessage class
58          * @return      $helperInstance         A prepared instance of this helper
59          */
60         public final static function createNodeRequestNodeListMessageAnswerHelper (DeliverableMessage $messageInstance) {
61                 // Get new instance
62                 $helperInstance = new NodeRequestNodeListMessageAnswerHelper();
63
64                 // Set session id of other peer as recipient
65                 $helperInstance->setRecipientType(NetworkPackageHandler::RECIPIENT_TYPE_DIRECT);
66                 $helperInstance->setRecipientId($messageData[XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID]);
67
68                 // Set message data
69                 $helperInstance->setMessageData($messageInstance);
70
71                 // Return the prepared instance
72                 return $helperInstance;
73         }
74
75         /**
76          * Loads the descriptor XML file
77          *
78          * @param       $nodeInstance   An instance of a Node class
79          * @return      void
80          */
81         public function loadDescriptorXml (Node $nodeInstance) {
82                 // Debug message
83                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: Attempting to answer a request: node-list...');
84
85                 // Get a XML template instance
86                 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_request_node_list_answer_template_class');
87
88                 // Set it for later use
89                 $this->setTemplateInstance($templateInstance);
90
91                 // Read the XML descriptor
92                 $templateInstance->loadXmlTemplate();
93
94                 // "Cache" entries instance for next foreach loop
95                 $entriesInstance = $templateInstance->getTemplateInstance();
96
97                 // Is must not be NULL (then it can only be a template instance)
98                 assert(!is_null($entriesInstance));
99
100                 // Render the XML content
101                 $templateInstance->renderXmlContent();
102         }
103
104         /**
105          * Send out request-node-list answer package
106          *
107          * @param       $nodeInstance   An instance of a Node class
108          * @return      void
109          */
110         public function sendPackage (Node $nodeInstance) {
111                 // Sanity check: Is the node in the approx. state? (active/reachable)
112                 $nodeInstance->getStateInstance()->validateNodeStateIsActiveOrReachable();
113
114                 // Compile the template, this inserts the loaded node data into the gaps.
115                 $this->getTemplateInstance()->compileTemplate();
116
117                 // Get a singleton network package instance
118                 $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance();
119
120                 // Next, feed the content in. The network package class is a pipe-through class.
121                 $packageInstance->enqueueRawDataFromTemplate($this);
122         }
123
124 }