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