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