* @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 HandleableMessage class * @return void */ public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) { // 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); } else { // Nothing found, so register it $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance); } // Save last exception $handlerInstance->setLastException($this->getWrapperInstance()->getLastException()); } } // [EOF] ?>