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