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