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