3 namespace Hub\Handler\Node\Answer\Announcement;
5 // Import application-specific stuff
6 use Hub\Factory\Node\NodeObjectFactory;
7 use Hub\Network\Receive\Receivable;
8 use Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
10 // Import framework stuff
11 use CoreFramework\Criteria\Storing\StoreableCriteria;
12 use CoreFramework\Generic\UnsupportedOperationException;
13 use CoreFramework\Registry\Registerable;
16 * A NodeMessageAnnouncementAnswer handler
18 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
37 class NodeMessageAnnouncementAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
48 $this->setHandlerName('message_announcement_answer');
50 // Init message data array
51 $this->messageDataElements = array(
52 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
53 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
54 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
55 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
56 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
57 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
58 BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
61 // Init message-data->configuration translation array
62 $this->messageToConfig = array(
64 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
65 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
66 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id'
67 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash'
71 // Init config-copy array
72 $this->configCopy = array(
73 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
74 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
75 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
76 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
77 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
81 $this->searchData = array(
82 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
83 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
88 * Creates an instance of this class
90 * @return $handlerInstance An instance of a HandleableMessage class
92 public final static function createNodeMessageAnnouncementAnswerHandler () {
94 $handlerInstance = new NodeMessageAnnouncementAnswerHandler();
96 // Return the prepared instance
97 return $handlerInstance;
101 * Handles data array of the message
103 * @param $messageData An array with message data to handle
104 * @param $packageInstance An instance of a Receivable class
106 * @throws NoAnnouncementAttemptedException If this node has not attempted to announce itself
108 public function handleMessageData (array $messageData, Receivable $packageInstance) {
110 $nodeInstance = NodeObjectFactory::createNodeInstance();
112 // Has this node attempted to announce itself?
113 if (!$nodeInstance->ifNodeIsAnnouncing()) {
115 * This node has never announced itself, so it doesn't expect
116 * announcement answer messages.
118 throw new NoAnnouncementAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED);
121 // Register the announcing node with this node
122 $this->registerNodeByMessageData($messageData);
124 // Handle the answer status element
125 $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance);
129 * Adds all required elements from given array into data set instance
131 * @param $dataSetInstance An instance of a StoreableCriteria class
132 * @param $messageData An array with all message data
135 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
137 parent::addArrayToDataSet($dataSetInstance, $messageData);
140 foreach ($this->messageDataElements as $key) {
142 assert(isset($messageData[$key]));
145 * Add it, but remove any 'my-' prefixes as they are not used in
148 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
153 * Initializes configuration data from given message data array
155 * @param $messageData An array with all message data
157 * @throws UnsupportedOperationException If this method is called
159 protected function initMessageConfigurationData (array $messageData) {
160 // Please don't call this method
161 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
165 * Removes configuration data with given message data array from global
168 * @param $messageData An array with all message data
170 * @throws UnsupportedOperationException If this method is called
172 protected function removeMessageConfigurationData (array $messageData) {
173 // Please don't call this method
174 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);