* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class NodeDhtFacade extends BaseDht implements Distributable, Registerable { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @return $dhtInstance An instance of a Distributable class */ public final static function createNodeDhtFacade () { // Get new instance $dhtInstance = new NodeDhtFacade(); // Get a database wrapper instance $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_dht_db_wrapper_class'); // Set it in this class $dhtInstance->setWrapperInstance($wrapperInstance); // Return the prepared instance return $dhtInstance; } /** * Initializes the distributed hash table (DHT) * * @return void * @todo Please implement this method */ public function initDht () { // Is the local node registered? if ($this->getWrapperInstance()->isLocalNodeRegistered()) { // Then only update session id $this->getWrapperInstance()->updateLocalNode(); } else { // No, so register it $this->getWrapperInstance()->registerLocalNode(); } } /** * Finds a node by given session id * * @param $sessionId Session id to lookup * @return $nodeData Node data array */ public function findNodeBySessionId ($sessionId) { // Default is empty data array $nodeData = array(); // Call the wrapper to do the job and get back a result instance $resultInstance = $this->getWrapperInstance()->findNodeBySessionId($sessionId); // Is the next entry valid? if ($resultInstance->next()) { // Then load the entry $nodeData = $resultInstance->current(); } // END - if // Return node data return $nodeData; } /** * Registers an other node with this node by given message data. The * following data must always be present: * * - session-id (for finding the node's record together with below data) * - external-ip (hostname or IP number) * - listen-port (TCP/UDP listen port for inbound connections) * * @param $messageArray An array with all minimum message data * @param $handlerInstance An instance of a Handleable class * @param $forceUpdate Optionally force update, don't register (default: register if not found) * @return void * @throws NodeSessionIdVerficationException If the node was not found and update is forced */ public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance, $forceUpdate = FALSE) { // Get a search criteria class $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData=' . print_r($messageData, true)); // Search for the node's session id and external IP/hostname + TCP/UDP listen port foreach ($handlerInstance->getSearchData() as $key) { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData[' . $key . ']=' . $messageData[$key]); // Is it there? assert(isset($messageData[$key])); // Add criteria $searchInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); } // END - foreach // Only one entry is fine $searchInstance->setLimit(1); // Run the query $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance); // Is there already an entry? if ($resultInstance->next()) { // Entry found, so update it $this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance); } elseif ($forceUpdate === FALSE) { // Nothing found, so register it $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance); } else { /* * Do not register non-existent nodes here. This is maybe fatal, * caused by "stolen" session id and/or not matching IP * number/port combination. */ throw new NodeSessionIdVerficationException(array($this, $messageData), BaseHubSystem::EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING); } // Save last exception $handlerInstance->setLastException($this->getWrapperInstance()->getLastException()); } /** * Queries the local DHT data(base) for a node list with all supported * object types except the node by given session id. * * @param $messageData An array with message data from a node_list request * @return $nodeList An array with all found nodes * @todo Implement exclusion/choise-inclusion criteria */ public function queryLocalNodeListExceptByMessageData (array $messageData) { // Get a search criteria class $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData=' . print_r($messageData, true)); // Only X entries are fine $searchInstance->setLimit($this->getConfigInstance()->getConfigEntry('node_dht_list_limit')); // Run the query $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance); // Get node list $nodeList = array(); while ($resultInstance->next()) { // Add this entry array_push($nodeList, $resultInstance->current()); } // END - while // Save last exception $handlerInstance->setLastException($this->getWrapperInstance()->getLastException()); // Return node list (array) return $nodeList; } } // [EOF] ?>