]> git.mxchange.org Git - hub.git/blob
716c099f44f5c6339ae3149d4fbc7c8059e28acf
[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\Message\DeliverableMessage;
10 use Org\Shipsimu\Hub\Network\Receive\Receivable;
11 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
14
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
19
20 /**
21  * A NodeMessageAnnouncementAnswer handler
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
41  */
42 class NodeMessageAnnouncementAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set handler name
53                 $this->setHandlerName('message_announcement_answer');
54
55                 // Init message data array
56                 $this->messageDataElements = [
57                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
58                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
59                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
60                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
61                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
62                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
63                         BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
64                 ];
65
66                 // Init message-data->configuration translation array
67                 /*
68                 $this->messageToConfig = [
69                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID       => 'your_session_id'
72                         XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash'
73                 ];
74                 */
75
76                 // Init array
77                 $this->searchData = [
78                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
79                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
80                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
81                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
82                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
83                 ];
84         }
85
86         /**
87          * Creates an instance of this class
88          *
89          * @return      $handlerInstance        An instance of a HandleableMessage class
90          */
91         public final static function createNodeMessageAnnouncementAnswerHandler () {
92                 // Get new instance
93                 $handlerInstance = new NodeMessageAnnouncementAnswerHandler();
94
95                 // Return the prepared instance
96                 return $handlerInstance;
97         }
98
99         /**
100          * Handles data array of the message
101          *
102          * @param       $messageInstance        An instance of a DeliverableMessage class
103          * @param       $packageInstance        An instance of a Receivable class
104          * @return      void
105          * @throws      NoAnnouncementAttemptedException        If this node has not attempted to announce itself
106          */
107         public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
108                 // Get node instance
109                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating 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, $messageInstance), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED);
119                 } // END - if
120
121                 // Register the announcing node with this node
122                 $this->registerNodeByMessageInstance($messageInstance);
123
124                 // Handle the answer status element
125                 $nodeInstance->handleAnswerStatusByMessageInstance($messageInstance, $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       $messageInstance        An instance of a DeliverableMessage class
133          * @return      void
134          */
135         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
136                 // Add generic first
137                 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
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 }