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