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