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