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