]> git.mxchange.org Git - hub.git/blob
809c9ff4812db694c40cd41b9471d70e54b26f01
[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\Message\DeliverableMessage;
11 use Org\Shipsimu\Hub\Network\Receive\Receivable;
12 use Org\Shipsimu\Hub\Node\BaseHubNode;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
14
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
17 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
19
20 /**
21  * A AnnouncementAnswerOkay 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 NodeAnnouncementAnswerOkayHandler extends BaseAnswerStatusHandler implements HandleableAnswerStatus, Registerable {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Init array
53                 $this->searchData = array(
54                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
55                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
56                 );
57
58                 // Set handler name
59                 $this->setHandlerName('announcement_answer_okay');
60         }
61
62         /**
63          * Creates an instance of this class
64          *
65          * @return      $handlerInstance        An instance of a HandleableMessage class
66          */
67         public final static function createNodeAnnouncementAnswerOkayHandler () {
68                 // Get new instance
69                 $handlerInstance = new NodeAnnouncementAnswerOkayHandler();
70
71                 // Return the prepared instance
72                 return $handlerInstance;
73         }
74
75         /**
76          * Handles given message data array
77          *
78          * @param       $messageInstance        An instance of a DeliverableMessage class
79          * @param       $packageInstance        An instance of a Receivable class
80          * @return      void
81          * @todo        Do some more here: Handle karma, et cetera?
82          */
83         public function handleAnswerMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
84                 /*
85                  * Query DHT and force update (which will throw an exception if the
86                  * node is not found).
87                  */
88                 DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageInstance($messageInstance, $this, TRUE);
89
90                 // Get handler instance
91                 $handlerInstance = GenericRegistry::getRegistry()->getInstance('task_handler');
92
93                 // Generate DHT bootstrap task
94                 $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_late_bootstrap_task_class');
95
96                 // Register it as well
97                 $handlerInstance->registerTask('dht_late_bootstrap', $taskInstance);
98
99                 // Get the node instance
100                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
101                 $nodeInstance = NodeObjectFactory::createNodeInstance();
102
103                 // Change state
104                 $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful();
105
106                 // Prepare next message
107                 $this->prepareNextMessage($messageInstance, $packageInstance);
108         }
109
110         /**
111          * Initializes configuration data from given message data array
112          *
113          * The following array is being handled over:
114          *
115          *   my-external-address => 1.2.3.4
116          *   my-internal-address => 5.6.7.8
117          *   my-status           => reachable
118          *   my-node-id          => aaabbbcccdddeeefff123456789
119          *   my-session-id       => aaabbbcccdddeeefff123456789
120          *   my-tcp-port         => 9060
121          *   my-udp-port         => 9060
122          *   answer-status       => OKAY
123          *   message_type        => announcement_answer
124          *
125          * @param       $messageInstance        An instance of a DeliverableMessage class
126          * @return      void
127          */
128         protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
129                 // Get node instance
130                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
131                 $nodeInstance = NodeObjectFactory::createNodeInstance();
132
133                 // Get an array of all accepted object types
134                 $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
135
136                 // Add missing (temporary) configuration 'accepted_object_types'
137                 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
138         }
139
140         /**
141          * Removes configuration data with given message data array from global
142          * configuration. For content of $messageInstance see method comment above.
143          *
144          * @param       $messageInstance        An instance of a DeliverableMessage class
145          * @return      void
146          */
147         protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
148                 // Remove temporay configuration
149                 FrameworkBootstrap::getConfigurationInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS);
150         }
151
152 }