5 * @author Roland Haeder <webmaster@shipsimu.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.shipsimu.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 * "Cached" instance of a publish helper
28 private $publishHelperInstance = NULL;
31 * Stacker name for "INSERT" node data
33 const STACKER_NAME_INSERT_NODE = 'dht_insert_node';
34 const STACKER_NAME_PENDING_PUBLISHING = 'dht_pending_publish';
37 * Protected constructor
39 * @param $className Name of the class
42 protected function __construct ($className) {
43 // Call parent constructor
44 parent::__construct($className);
46 // Get a stacker instance for this DHT
47 $stackerInstance = ObjectFactory::createObjectByConfiguredName('dht_stacker_class');
49 // Set it in this class
50 $this->setStackerInstance($stackerInstance);
56 * Get the state factory and create the initial state, we don't need
57 * the state instance here
59 DhtStateFactory::createDhtStateInstanceByName('init', $this);
63 * Initializes all stackers
67 private function initStacks () {
68 // Initialize all stacker
69 $this->getStackerInstance()->initStacks(array(
70 self::STACKER_NAME_INSERT_NODE,
71 self::STACKER_NAME_PENDING_PUBLISHING,
76 * Registers/updates an entry in the DHT with given data from $dhtData
77 * array. Different DHT implemtations may handle this differently as they
78 * may enrich the data with more meta data.
80 * @param $dhtData A valid array with DHT-related data (e.g. node/peer data)
83 protected abstract function insertDataIntoDht (array $dhtData);
86 * Updates/refreshes DHT data (e.g. status).
89 * @todo Find more to do here
91 public function updateDhtData () {
92 // Set some dummy configuration entries, e.g. dht_status
93 $this->getConfigInstance()->setConfigEntry('dht_status', $this->getStateInstance()->getStateName());
97 * Checks whether there are entries in "INSERT" node data stack
99 * @return $isPending Whether there are pending entries
101 public function ifInsertNodeDataPending () {
102 // Determine it if it is not empty
103 $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_INSERT_NODE) === FALSE);
110 * Inserts a single entry of node data into the DHT
114 public function insertSingleNodeData () {
115 // Get next node data from stack
116 $nodeData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_INSERT_NODE);
118 // Make sure $nodeData is really an array and has at least one entry
119 assert((is_array($nodeData)) && (count($nodeData) > 0));
122 $this->insertDataIntoDht($nodeData);
126 * Checks whether there are unpublished entries
128 * @return $hasUnpublished Whether there are unpublished entries
129 * @todo Add minimum/maximum age limitations
131 public function hasUnpublishedEntries () {
132 // Call method on database wrapper
133 $hasUnpublished = $this->getWrapperInstance()->hasUnpublishedEntries();
136 return $hasUnpublished;
140 * Initializes publication of DHT entries. This does only prepare
141 * publication. The next step is to pickup such prepared entries and publish
142 * them by uploading to other (recently appeared) DHT members.
146 public function initEntryPublication () {
147 // Call method on database wrapper
148 $this->getWrapperInstance()->initEntryPublication();
150 // Get result instance
151 $resultInstance = $this->getWrapperInstance()->getUnpublishedEntriesInstance();
153 // Must still be valid
154 assert($resultInstance->valid());
156 // "Walk" through all entries
157 while ($resultInstance->next()) {
159 $current = $resultInstance->current();
161 // Make sure only valid entries pass
162 // @TODO Maybe add more small checks?
163 assert(is_array($current));
165 // ... and push it to the next stack
166 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...');
167 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_PENDING_PUBLISHING, $current);
172 * Checks whether there are entries pending publication
174 * @return $isPending Whether there are entries pending publication
176 public function hasEntriesPendingPublication () {
177 // Determine it if it is not empty
178 $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PENDING_PUBLISHING) === FALSE);
181 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isPending=' . intval($isPending));
186 * Publishes next entry found in stack. This method shall also update the
187 * corresponding dabase entry.
190 * @todo Find out if loadDescriptorXml() can be called only once to avoid a lot methods working.
192 public function publishEntry () {
193 // This test must not fail
194 assert($this->hasEntriesPendingPublication());
196 // Is there an instance?
197 if (!$this->publishHelperInstance instanceof HelpableDht) {
198 // Get a helper instance
199 $this->publishHelperInstance = ObjectFactory::createObjectByConfiguredName('dht_publish_entry_helper_class');
202 // Load the announcement descriptor
203 $this->publishHelperInstance->loadDescriptorXml($this);
206 $entry = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PENDING_PUBLISHING);
208 // Some sanity-checks
209 assert(is_array($entry));
212 $this->publishHelperInstance->getTemplateInstance()->injectConfigVariables($entry);
214 // "Publish" the descriptor by sending it to the bootstrap/list nodes
215 $this->publishHelperInstance->sendPackage($this);