3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Announcement;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper;
7 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
8 use Org\Shipsimu\Hub\Generic\BaseHubSystem;
9 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
10 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
11 use Org\Shipsimu\Hub\Network\Receive\Receivable;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Registry\Registerable;
20 * A NodeMessageAnnouncement handler
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
52 $this->setHandlerName('message_announcement');
54 // Init message data array
55 $this->messageDataElements = array(
56 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
57 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
59 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
60 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
61 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
62 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
65 // Init message-data->configuration translation array
66 $this->messageToConfig = array(
67 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
68 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
69 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
74 // Init config-copy array
75 $this->configCopy = array(
76 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
77 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
78 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
79 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
80 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
84 $this->searchData = array(
85 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
90 * Creates an instance of this class
92 * @return $handlerInstance An instance of a HandleableMessage class
94 public final static function createNodeMessageAnnouncementHandler () {
96 $handlerInstance = new NodeMessageAnnouncementHandler();
98 // Return the prepared instance
99 return $handlerInstance;
103 * Handles data array of the message
105 * @param $messageData An array with message data to handle
106 * @param $packageInstance An instance of a Receivable class
108 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
110 public function handleMessageData (array $messageData, Receivable $packageInstance) {
112 $nodeInstance = NodeObjectFactory::createNodeInstance();
114 // Is this node accepting announcements?
115 if (!$nodeInstance->isAcceptingAnnouncements()) {
117 * This node is not accepting announcements, then someone wants to
118 * announce his node to a non-bootstrap and non-master node.
120 throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
123 // Register the announcing node with this node
124 $this->registerNodeByMessageData($messageData);
126 // Prepare answer message to be delivered back to the other node
127 $this->prepareAnswerMessage($messageData, $packageInstance);
131 * Adds all required elements from given array into data set instance
133 * @param $dataSetInstance An instance of a StoreableCriteria class
134 * @param $messageData An array with all message data
137 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
139 parent::addArrayToDataSet($dataSetInstance, $messageData);
142 foreach ($this->messageDataElements as $key) {
144 assert(isset($messageData[$key]));
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...');
148 $dataSetInstance->addCriteria($key, $messageData[$key]);
153 * Initializes configuration data from given message data array
155 * @param $messageData An array with all message data
158 protected function initMessageConfigurationData (array $messageData) {
160 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE));
162 // "Walk" throught the translation array
163 foreach ($this->messageToConfig as $messageKey => $configKey) {
165 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]);
167 // Set the element in configuration
168 $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]);
171 // "Walk" throught the config-copy array
172 foreach ($this->configCopy as $targetKey => $sourceKey) {
174 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
176 // Copy from source to targetKey
177 $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
180 // Translate last exception into a status code
181 $statusCode = $this->getTranslatedStatusFromLastException();
183 // Set it in configuration (temporarily)
184 $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
188 * Removes configuration data with given message data array from global
191 * @param $messageData An array with all message data
194 protected function removeMessageConfigurationData (array $messageData) {
195 // "Walk" throught the translation array again
196 foreach ($this->messageToConfig as $dummy => $configKey) {
197 // Now unset this configuration entry (to save some memory)
198 $this->getConfigInstance()->unsetConfigEntry($configKey);
201 // "Walk" throught the config-copy array again
202 foreach ($this->configCopy as $configKey => $dummy) {
203 // Now unset this configuration entry (to save some memory again)
204 $this->getConfigInstance()->unsetConfigEntry($configKey);
207 // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
208 $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);