* @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.shipsimu.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 . */ abstract class BaseDht extends BaseHubSystem { /** * Stacker name for "INSERT" node data */ const STACKER_NAME_INSERT_NODE = 'dht_insert_node'; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); // Get a stacker instance for this DHT $stackerInstance = ObjectFactory::createObjectByConfiguredName('dht_stacker_class'); // Set it in this class $this->setStackerInstance($stackerInstance); // Init all stackers $this->initStacks(); /* * Get the state factory and create the initial state, we don't need * the state instance here */ DhtStateFactory::createDhtStateInstanceByName('init', $this); } /** * Initializes all stackers * * @return void */ private function initStacks () { // Initialize all stacker $this->getStackerInstance()->initStacks(array( self::STACKER_NAME_INSERT_NODE, )); } /** * Registers/updates an entry in the DHT with given data from $dhtData * array. Different DHT implemtations may handle this differently as they * may enrich the data with more meta data. * * @param $dhtData A valid array with DHT-related data (e.g. node/peer data) * @return void */ protected abstract function insertDataIntoDht (array $dhtData); /** * Updates/refreshes DHT data (e.g. status). * * @return void * @todo Find more to do here */ public function updateDhtData () { // Set some dummy configuration entries, e.g. dht_status $this->getConfigInstance()->setConfigEntry('dht_status', $this->getStateInstance()->getStateName()); } /** * Checks whether there are entries in "INSERT" node data stack * * @return $isPending Whether there are pending entries */ public function ifInsertNodeDataPending () { // Determine it if it is not empty $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_INSERT_NODE) === FALSE); // Return status return $isPending; } /** * Inserts a single entry of node data into the DHT * * @return void */ public function insertSingleNodeData () { // Get next node data from stack $nodeData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_INSERT_NODE); // Make sure $nodeData is really an array and has at least one entry assert((is_array($nodeData)) && (count($nodeData) > 0)); // Insert the data $this->insertDataIntoDht($nodeData); } } // [EOF] ?>