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