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