]> git.mxchange.org Git - hub.git/blob
bf552b046da06062c2b3d90c7f2f66dceee95adb
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Handler\Node\Answer\Request\NodeList;
4
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
7 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
8 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
9 use Org\Shipsimu\Hub\Network\Receive\Receivable;
10 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Request\NodeList\XmlRequestNodeListAnswerTemplateEngine;
11 use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Request\NodeList\XmlRequestNodeListTemplateEngine;
13
14 // Import framework stuff
15 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
16 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
17 use Org\Mxchange\CoreFramework\Registry\Registerable;
18
19 /**
20  * A NodeMessageRequestNodeListAnswer handler
21  *
22  * @author              Roland Haeder <webmaster@shipsimu.org>
23  * @version             0.0.0
24  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
25  * @license             GNU GPL 3.0 or any newer version
26  * @link                http://www.shipsimu.org
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
40  */
41 class NodeMessageRequestNodeListAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50
51                 // Set handler name
52                 $this->setHandlerName('message_request_node_list_answer');
53
54                 // Init message data array
55                 $this->messageDataElements = array(
56                         XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID,
57                         XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST,
58                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
59                 );
60
61                 // Init message-data->configuration translation array
62                 $this->messageToConfig = array(
63                 /*
64                         XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID  => 'your_session_id'
65                 */
66                 );
67
68                 // Init search data array
69                 $this->searchData = array(
70                         XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID,
71                 );
72         }
73
74         /**
75          * Creates an instance of this class
76          *
77          * @return      $handlerInstance        An instance of a HandleableMessage class
78          */
79         public final static function createNodeMessageRequestNodeListAnswerHandler () {
80                 // Get new instance
81                 $handlerInstance = new NodeMessageRequestNodeListAnswerHandler();
82
83                 // Return the prepared instance
84                 return $handlerInstance;
85         }
86
87         /**
88          * Handles data array of the message
89          *
90          * @param       $messageData            An array with message data to handle
91          * @param       $packageInstance        An instance of a Receivable class
92          * @return      void
93          * @throws      NoRequestNodeListAttemptedException     If this node has not attempted to announce itself
94          */
95         public function handleMessageData (array $messageData, Receivable $packageInstance) {
96                 // Get node instance
97                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
98                 $nodeInstance = NodeObjectFactory::createNodeInstance();
99
100                 // Has this node attempted to announce itself?
101                 if (!$nodeInstance->ifNodeHasAnnouncementCompleted()) {
102                         /*
103                          * This node has never announced itself, so it doesn't expect
104                          * request-node-list answer messages.
105                          */
106                         throw new NoRequestNodeListAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED);
107                 } // END - if
108
109                 // Register the announcing node with this node
110                 $this->registerNodeByMessageData($messageData);
111
112                 // Handle the answer status element
113                 $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance);
114         }
115
116         /**
117          * Adds all required elements from given array into data set instance
118          *
119          * @param       $dataSetInstance        An instance of a StoreableCriteria class
120          * @param       $messageData            An array with all message data
121          * @return      void
122          */
123         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
124                 // Add generic first
125                 parent::addArrayToDataSet($dataSetInstance, $messageData);
126
127                 // Debug message
128                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData=' . print_r($messageData, TRUE));
129
130                 // Add all ements
131                 foreach ($this->messageDataElements as $key) {
132                         // Debug message
133                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData[' . $key . ']=' . $messageData[$key]);
134
135                         // Is it there?
136                         assert(isset($messageData[$key]));
137
138                         /*
139                          * Add it, but remove any 'my-' prefixes as they are not used in
140                          * database layer.
141                          */
142                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
143                 } // END - foreach
144         }
145
146         /**
147          * Initializes configuration data from given message data array
148          *
149          * @param       $messageData    An array with all message data
150          * @return      void
151          * @throws      UnsupportedOperationException   If this method is called
152          */
153         protected function initMessageConfigurationData (array $messageData) {
154                 // Please don't call this method
155                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
156         }
157
158         /**
159          * Removes configuration data with given message data array from global
160          * configuration
161          *
162          * @param       $messageData    An array with all message data
163          * @return      void
164          * @throws      UnsupportedOperationException   If this method is called
165          */
166         protected function removeMessageConfigurationData (array $messageData) {
167                 // Please don't call this method
168                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
169         }
170
171 }