5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 implements Distributable {
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 $stackInstance = ObjectFactory::createObjectByConfiguredName('dht_stacker_class');
49 // Set it in this class
50 $this->setStackInstance($stackInstance);
55 // Get the state factory and create the initial state.
56 DhtStateFactory::createDhtStateInstanceByName('init', $this);
60 * Initializes all stackers
64 private function initStacks () {
65 // Initialize all stacker
66 $this->getStackInstance()->initStacks(array(
67 self::STACKER_NAME_INSERT_NODE,
68 self::STACKER_NAME_PENDING_PUBLISHING,
73 * Registers/updates an entry in the DHT with given data from $dhtData
74 * array. Different DHT implemtations may handle this differently as they
75 * may enrich the data with more meta data.
77 * @param $dhtData A valid array with DHT-related data (e.g. node/peer data)
80 protected abstract function insertDataIntoDht (array $dhtData);
83 * Updates/refreshes DHT data (e.g. status).
86 * @todo Find more to do here
88 public function updateDhtData () {
89 // Set some dummy configuration entries, e.g. dht_status
90 $this->getConfigInstance()->setConfigEntry('dht_status', $this->getStateInstance()->getStateName());
94 * Checks whether there are entries in "INSERT" node data stack
96 * @return $isPending Whether there are pending entries
98 public function ifInsertNodeDataPending () {
99 // Determine it if it is not empty
100 $isPending = ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_INSERT_NODE) === FALSE);
107 * Inserts a single entry of node data into the DHT
111 public function insertSingleNodeData () {
112 // Get next node data from stack
113 $nodeData = $this->getStackInstance()->popNamed(self::STACKER_NAME_INSERT_NODE);
115 // Make sure $nodeData is really an array and has at least one entry
116 assert((is_array($nodeData)) && (count($nodeData) > 0));
119 $this->insertDataIntoDht($nodeData);
123 * Checks whether there are unpublished entries
125 * @return $hasUnpublished Whether there are unpublished entries
126 * @todo Add minimum/maximum age limitations
128 public function hasUnpublishedEntries () {
129 // Call method on database wrapper
130 $hasUnpublished = $this->getWrapperInstance()->hasUnpublishedEntries();
133 return $hasUnpublished;
137 * Initializes publication of DHT entries. This does only prepare
138 * publication. The next step is to pickup such prepared entries and publish
139 * them by uploading to other (recently appeared) DHT members.
143 public function initEntryPublication () {
144 // Call method on database wrapper
145 $this->getWrapperInstance()->initEntryPublication();
147 // Get result instance
148 $resultInstance = $this->getWrapperInstance()->getUnpublishedEntriesInstance();
150 // Make sure the result instance is valid
151 assert($resultInstance instanceof SearchableResult);
152 assert($resultInstance->valid());
154 // "Walk" through all entries
155 while ($resultInstance->next()) {
157 $current = $resultInstance->current();
159 // Make sure only valid entries pass
160 // @TODO Maybe add more small checks?
161 assert(is_array($current));
163 // ... and push it to the next stack
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-DHT[' . __METHOD__ . ':' . __LINE__ . '] Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...');
165 $this->getStackInstance()->pushNamed(self::STACKER_NAME_PENDING_PUBLISHING, $current);
170 * Checks whether there are entries pending publication
172 * @return $isPending Whether there are entries pending publication
174 public function hasEntriesPendingPublication () {
175 // Determine it if it is not empty
176 $isPending = ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PENDING_PUBLISHING) === FALSE);
179 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-DHT[' . __METHOD__ . ':' . __LINE__ . '] isPending=' . intval($isPending));
184 * Whether this DHT's state is 'booting'
186 * @return $isBooting Whether this DHT is currently booting
188 public function ifDhtIsBooting () {
189 // Call state instance
190 $isBooting = $this->getStateInstance()->ifDhtIsBooting();
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-DHT[' . __METHOD__ . ':' . __LINE__ . '] isBooting=' . intval($isBooting));
198 * Publishes next entry found in stack. This method shall also update the
199 * corresponding dabase entry.
202 * @todo Find out if loadDescriptorXml() can be called only once to avoid a lot methods working.
204 public function publishEntry () {
205 // This test must not fail
206 assert($this->hasEntriesPendingPublication());
208 // Is there an instance?
209 if (!$this->publishHelperInstance instanceof HelpableDht) {
210 // Get a helper instance
211 $this->publishHelperInstance = ObjectFactory::createObjectByConfiguredName('dht_publish_entry_helper_class');
214 // Load the announcement descriptor
215 $this->publishHelperInstance->loadDescriptorXml($this);
218 $entry = $this->getStackInstance()->popNamed(self::STACKER_NAME_PENDING_PUBLISHING);
220 // Some sanity-checks
221 assert(is_array($entry));
223 // Remove any non-public data the database layer desires
224 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT[' . __METHOD__ . ':' . __LINE__ . ']: Calling this->getWrapperInstance()->removeNonPublicDataFromArray(data) ...');
225 $entry = $this->getWrapperInstance()->removeNonPublicDataFromArray($entry);
226 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT[' . __METHOD__ . ':' . __LINE__ . ']: entry[]=' . gettype($entry));
228 // Some sanity-checks again
229 assert(is_array($entry));
232 $this->publishHelperInstance->getTemplateInstance()->injectConfigVariables($entry);
234 // "Publish" the descriptor by sending it to the bootstrap/list nodes
235 $this->publishHelperInstance->sendPackage($this);
239 * Whether the DHT has fully bootstrapped (after state 'booting')
241 * @return $isFullyBooted Whether the DHT is fully booted
244 public function hasFullyBootstrapped () {
245 // Get state and check it
246 $this->partialStub('Please implement this method.');
250 * Enable DHT bootstrap request acceptance for local node
253 * @todo Switch flag 'accept_bootstrap'
255 public function enableAcceptDhtBootstrap () {
256 // Call method on database wrapper
257 $this->getWrapperInstance()->enableAcceptDhtBootstrap();