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 abstract class BaseDht extends BaseHubSystem {
26 * Stacker name for "INSERT" node data
28 const STACKER_NAME_INSERT_NODE = 'dht_insert_node';
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
40 // Get a stacker instance for this DHT
41 $stackerInstance = ObjectFactory::createObjectByConfiguredName('dht_stacker_class');
43 // Set it in this class
44 $this->setStackerInstance($stackerInstance);
50 * Get the state factory and create the initial state, we don't need
51 * the state instance here
53 DhtStateFactory::createDhtStateInstanceByName('init', $this);
57 * Initializes all stackers
61 private function initStacks () {
62 // Initialize all stacker
63 $this->getStackerInstance()->initStacks(array(
64 self::STACKER_NAME_INSERT_NODE,
69 * Registers/updates an entry in the DHT with given data from $dhtData
70 * array. Different DHT implemtations may handle this differently as they
71 * may enrich the data with more meta data.
73 * @param $dhtData A valid array with DHT-related data (e.g. node/peer data)
76 protected abstract function insertDataIntoDht (array $dhtData);
79 * Updates/refreshes DHT data (e.g. status).
82 * @todo Find more to do here
84 public function updateDhtData () {
85 // Set some dummy configuration entries, e.g. dht_status
86 $this->getConfigInstance()->setConfigEntry('dht_status', $this->getStateInstance()->getStateName());
90 * Checks whether there are entries in "INSERT" node data stack
92 * @return $isPending Whether there are pending entries
94 public function ifInsertNodeDataPending () {
95 // Determine it if it is not empty
96 $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_INSERT_NODE) === FALSE);
103 * Inserts a single entry of node data into the DHT
107 public function insertSingleNodeData () {
108 // Get next node data from stack
109 $nodeData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_INSERT_NODE);
111 // Make sure $nodeData is really an array and has at least one entry
112 assert((is_array($nodeData)) && (count($nodeData) > 0));
115 $this->insertDataIntoDht($nodeData);