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