3 * A Node DHT facade class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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.ship-simu.org
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.
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.
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/>.
24 class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this class
38 * @return $dhtInstance An instance of a Distributable class
40 public final static function createNodeDhtFacade () {
42 $dhtInstance = new NodeDhtFacade();
44 // Get a database wrapper instance
45 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_dht_db_wrapper_class');
47 // Set it in this class
48 $dhtInstance->setWrapperInstance($wrapperInstance);
50 // Return the prepared instance
55 * Initializes the distributed hash table (DHT)
58 * @todo Please implement this method
60 public function initDht () {
61 // Is the local node registered?
62 if ($this->getWrapperInstance()->isLocalNodeRegistered()) {
63 // Then only update session id
64 $this->getWrapperInstance()->updateLocalNode();
67 $this->getWrapperInstance()->registerLocalNode();
72 * Finds a node by given session id
74 * @param $sessionId Session id to lookup
75 * @return $nodeData Node data array
77 public function findNodeBySessionId ($sessionId) {
78 // Default is empty data array
81 // Call the wrapper to do the job and get back a result instance
82 $resultInstance = $this->getWrapperInstance()->findNodeBySessionId($sessionId);
84 // Is the next entry valid?
85 if ($resultInstance->next()) {
86 // Then load the entry
87 $nodeData = $resultInstance->current();
95 * Registers an other node with this node by given message data. The
96 * following data must always be present:
98 * - session-id (for finding the node's record together with below data)
99 * - external-ip (hostname or IP number)
100 * - listen-port (TCP/UDP listen port for inbound connections)
102 * @param $messageArray An array with all minimum message data
103 * @param $handlerInstance An instance of a HandleableMessage class
106 public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) {
107 // Get a search criteria class
108 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
111 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData=' . print_r($messageData, true));
113 // Search for the node's session id and external IP/hostname + TCP/UDP listen port
114 foreach ($handlerInstance->getSearchData() as $key) {
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData[' . $key . ']=' . $messageData[$key]);
119 assert(isset($messageData[$key]));
122 $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
125 // Only one entry is fine
126 $searchInstance->setLimit(1);
129 $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
131 // Is there already an entry?
132 if ($resultInstance->next()) {
133 // Entry found, so update it
134 $this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance);
136 // Nothing found, so register it
137 $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance);
140 // Save last exception
141 $handlerInstance->setLastException($this->getWrapperInstance()->getLastException());