]> git.mxchange.org Git - hub.git/blob
fb926640ea6153d0842dd27729081d43d487fe9d
[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                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
99                 $nodeInstance = NodeObjectFactory::createNodeInstance();
100
101                 // Change state
102                 $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful();
103
104                 // Prepare next message
105                 $this->prepareNextMessage($messageData, $packageInstance);
106         }
107
108         /**
109          * Initializes configuration data from given message data array
110          *
111          * The following array is being handled over:
112          *
113          *   my-external-address => 1.2.3.4
114          *   my-internal-address => 5.6.7.8
115          *   my-status           => reachable
116          *   my-node-id          => aaabbbcccdddeeefff123456789
117          *   my-session-id       => aaabbbcccdddeeefff123456789
118          *   my-tcp-port         => 9060
119          *   my-udp-port         => 9060
120          *   answer-status       => OKAY
121          *   message_type        => announcement_answer
122          *
123          * @param       $messageData    An array with all message data
124          * @return      void
125          */
126         protected function initMessageConfigurationData (array $messageData) {
127                 // Get node instance
128                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
129                 $nodeInstance = NodeObjectFactory::createNodeInstance();
130
131                 // Get an array of all accepted object types
132                 $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
133
134                 // Add missing (temporary) configuration 'accepted_object_types'
135                 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
136         }
137
138         /**
139          * Removes configuration data with given message data array from global
140          * configuration. For content of $messageData see method comment above.
141          *
142          * @param       $messageData    An array with all message data
143          * @return      void
144          */
145         protected function removeMessageConfigurationData (array $messageData) {
146                 // Remove temporay configuration
147                 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS);
148         }
149
150 }