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