3 namespace Org\Shipsimu\Hub\Handler\Node\Message\Announcement;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseFrontend;
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\Message\DeliverableMessage;
12 use Org\Shipsimu\Hub\Network\Receive\Receivable;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
14 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
16 // Import framework stuff
17 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
18 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
22 * A NodeMessageAnnouncement handler
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
54 $this->setHandlerName('message_announcement');
56 // Init message data array
57 $this->messageDataElements = [
58 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
59 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
60 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
61 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE,
62 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
63 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
64 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
67 // Init message-data->configuration translation array
69 $this->messageToConfig = [
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
72 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id',
73 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id',
74 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash',
80 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS
81 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
82 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
83 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
84 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
89 * Creates an instance of this class
91 * @return $handlerInstance An instance of a HandleableMessage class
93 public final static function createNodeMessageAnnouncementHandler () {
95 $handlerInstance = new NodeMessageAnnouncementHandler();
97 // Return the prepared instance
98 return $handlerInstance;
102 * Handles data array of the message
104 * @param $messageInstance An instance of a DeliverableMessage class
105 * @param $handlerInstance An instance of a Receivable class
107 * @throws AnnouncementNotAcceptedException If this node does not accept announcements
109 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $handlerInstance) {
111 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
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, $messageInstance), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED);
123 // Register the announcing node with this node
124 $this->registerNodeByMessageInstance($messageInstance);
126 // Prepare answer message to be delivered back to the other node
127 $this->prepareAnswerMessage($messageInstance, $handlerInstance);
131 * Adds all required elements from given array into data set instance
133 * @param $dataSetInstance An instance of a StoreableCriteria class
134 * @param $messageInstance An instance of a DeliverableMessage class
137 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
139 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
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]);