]> git.mxchange.org Git - hub.git/blob
8b0e0ba9d3e9b1f09c652ccdf220af15de5981d4
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Handler\Node\Announcement\Answer;
4
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Org\Shipsimu\Hub\Factory\Dht\DhtObjectFactory;
8 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
9 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
10 use Org\Shipsimu\Hub\Network\Receive\Receivable;
11 use Org\Shipsimu\Hub\Node\BaseHubNode;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
13
14 // Import framework stuff
15 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
16 use Org\Mxchange\CoreFramework\Registry\Registerable;
17
18 /**
19  * A AnnouncementAnswerOkay handler
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
23  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
24  * @license             GNU GPL 3.0 or any newer version
25  * @link                http://www.shipsimu.org
26  *
27  * This program is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39  */
40 class NodeAnnouncementAnswerOkayHandler extends BaseAnswerStatusHandler implements HandleableAnswerStatus, Registerable {
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Init array
51                 $this->searchData = array(
52                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
53                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
54                 );
55
56                 // Set handler name
57                 $this->setHandlerName('announcement_answer_okay');
58         }
59
60         /**
61          * Creates an instance of this class
62          *
63          * @return      $handlerInstance        An instance of a HandleableMessage class
64          */
65         public final static function createNodeAnnouncementAnswerOkayHandler () {
66                 // Get new instance
67                 $handlerInstance = new NodeAnnouncementAnswerOkayHandler();
68
69                 // Return the prepared instance
70                 return $handlerInstance;
71         }
72
73         /**
74          * Handles given message data array
75          *
76          * @param       $messageData            An array of message data
77          * @param       $packageInstance        An instance of a Receivable class
78          * @return      void
79          * @todo        Do some more here: Handle karma, et cetera?
80          */
81         public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) {
82                 /*
83                  * Query DHT and force update (which will throw an exception if the
84                  * node is not found).
85                  */
86                 DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this, TRUE);
87
88                 // Get handler instance
89                 $handlerInstance = GenericRegistry::getRegistry()->getInstance('task_handler');
90
91                 // Generate DHT bootstrap task
92                 $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_late_bootstrap_task_class');
93
94                 // Register it as well
95                 $handlerInstance->registerTask('dht_late_bootstrap', $taskInstance);
96
97                 // Get the node instance
98                 $nodeInstance = NodeObjectFactory::createNodeInstance();
99
100                 // Change state
101                 $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful();
102
103                 // Prepare next message
104                 $this->prepareNextMessage($messageData, $packageInstance);
105         }
106
107         /**
108          * Initializes configuration data from given message data array
109          *
110          * The following array is being handled over:
111          *
112          *   my-external-address => 1.2.3.4
113          *   my-internal-address => 5.6.7.8
114          *   my-status           => reachable
115          *   my-node-id          => aaabbbcccdddeeefff123456789
116          *   my-session-id       => aaabbbcccdddeeefff123456789
117          *   my-tcp-port         => 9060
118          *   my-udp-port         => 9060
119          *   answer-status       => OKAY
120          *   message_type        => announcement_answer
121          *
122          * @param       $messageData    An array with all message data
123          * @return      void
124          */
125         protected function initMessageConfigurationData (array $messageData) {
126                 // Get node instance
127                 $nodeInstance = NodeObjectFactory::createNodeInstance();
128
129                 // Get an array of all accepted object types
130                 $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
131
132                 // Add missing (temporary) configuration 'accepted_object_types'
133                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
134         }
135
136         /**
137          * Removes configuration data with given message data array from global
138          * configuration. For content of $messageData see method comment above.
139          *
140          * @param       $messageData    An array with all message data
141          * @return      void
142          */
143         protected function removeMessageConfigurationData (array $messageData) {
144                 // Remove temporay configuration
145                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS);
146         }
147
148 }