]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php
Introduced NodeObjectFactory to encapsulate (and ease) a lot calls.
[hub.git] / application / hub / main / 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 - 2012 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_IP,
38                         XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_LISTEN_PORT
39                 );
40
41                 // Set handler name
42                 $this->setHandlerName('announcement_answer_okay');
43         }
44
45         /**
46          * Creates an instance of this class
47          *
48          * @return      $handlerInstance        An instance of a HandleableMessage class
49          */
50         public final static function createAnnouncementAnswerOkayHandler () {
51                 // Get new instance
52                 $handlerInstance = new AnnouncementAnswerOkayHandler();
53
54                 // Return the prepared instance
55                 return $handlerInstance;
56         }
57
58         /**
59          * Handles given message data array
60          *
61          * @param       $messageData            An array of message data
62          * @param       $packageInstance        An instance of a Receivable class
63          * @return      void
64          * @todo        Do some more here: Handle karma, et cetera?
65          */
66         public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) {
67                 /*
68                  * Query DHT and force update (which will throw an exception if the
69                  * node is not found).
70                  */
71                 $this->getDhtInstance()->registerNodeByMessageData($messageData, $this, TRUE);
72
73                 // Get handler instance
74                 $handlerInstance = Registry::getRegistry()->getInstance('task_handler');
75
76                 // Generate DHT bootstrap task
77                 $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_bootstrap_task_class');
78
79                 // Register it as well
80                 $handlerInstance->registerTask('dht_bootstrap', $taskInstance);
81
82                 // Get the node instance
83                 $nodeInstance = NodeObjectFactory::createNodeInstance();
84
85                 // Change state
86                 $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful();
87
88                 // Prepare next message
89                 $this->prepareNextMessage($messageData, $packageInstance);
90         }
91
92         /**
93          * Initializes configuration data from given message data array
94          *
95          * The following array is being handled over:
96          *
97          *   my-external-ip => 1.2.3.4
98          *   my-internal-ip => 5.6.7.8
99          *   my-status      => reachable
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('accepted_object_types', 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('accepted_object_types');
130         }
131 }
132
133 // [EOF]
134 ?>