]> git.mxchange.org Git - hub.git/blob
4d28c0ef3fa993d7c2f8e2a574d1e05844febfbb
[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                 $nodeInstance = NodeObjectFactory::createNodeInstance();
98
99                 // Has this node attempted to announce itself?
100                 if (!$nodeInstance->ifNodeHasAnnouncementCompleted()) {
101                         /*
102                          * This node has never announced itself, so it doesn't expect
103                          * request-node-list answer messages.
104                          */
105                         throw new NoRequestNodeListAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED);
106                 } // END - if
107
108                 // Register the announcing node with this node
109                 $this->registerNodeByMessageData($messageData);
110
111                 // Handle the answer status element
112                 $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance);
113         }
114
115         /**
116          * Adds all required elements from given array into data set instance
117          *
118          * @param       $dataSetInstance        An instance of a StoreableCriteria class
119          * @param       $messageData            An array with all message data
120          * @return      void
121          */
122         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
123                 // Add generic first
124                 parent::addArrayToDataSet($dataSetInstance, $messageData);
125
126                 // Debug message
127                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData=' . print_r($messageData, TRUE));
128
129                 // Add all ements
130                 foreach ($this->messageDataElements as $key) {
131                         // Debug message
132                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData[' . $key . ']=' . $messageData[$key]);
133
134                         // Is it there?
135                         assert(isset($messageData[$key]));
136
137                         /*
138                          * Add it, but remove any 'my-' prefixes as they are not used in
139                          * database layer.
140                          */
141                         $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
142                 } // END - foreach
143         }
144
145         /**
146          * Initializes configuration data from given message data array
147          *
148          * @param       $messageData    An array with all message data
149          * @return      void
150          * @throws      UnsupportedOperationException   If this method is called
151          */
152         protected function initMessageConfigurationData (array $messageData) {
153                 // Please don't call this method
154                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
155         }
156
157         /**
158          * Removes configuration data with given message data array from global
159          * configuration
160          *
161          * @param       $messageData    An array with all message data
162          * @return      void
163          * @throws      UnsupportedOperationException   If this method is called
164          */
165         protected function removeMessageConfigurationData (array $messageData) {
166                 // Please don't call this method
167                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
168         }
169
170 }