/**
* A general DHT class
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @author Roland Haeder <webmaster@shipsimu.org>
* @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
+ * @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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseDht extends BaseHubSystem {
+ /**
+ * "Cached" instance of a publish helper
+ */
+ private $publishHelperInstance = NULL;
+
/**
* Stacker name for "INSERT" node data
*/
- const STACKER_NAME_INSERT_NODE = 'dht_insert_node';
+ const STACKER_NAME_INSERT_NODE = 'dht_insert_node';
+ const STACKER_NAME_PENDING_PUBLISHING = 'dht_pending_publish';
/**
* Protected constructor
$this->setStackerInstance($stackerInstance);
// Init all stackers
- $this->initStackers();
+ $this->initStacks();
/*
* Get the state factory and create the initial state, we don't need
*
* @return void
*/
- private function initStackers () {
+ private function initStacks () {
// Initialize all stacker
- $this->getStackerInstance()->initStackers(array(
+ $this->getStackerInstance()->initStacks(array(
self::STACKER_NAME_INSERT_NODE,
+ self::STACKER_NAME_PENDING_PUBLISHING,
));
}
// Insert the data
$this->insertDataIntoDht($nodeData);
}
+
+ /**
+ * Checks whether there are unpublished entries
+ *
+ * @return $hasUnpublished Whether there are unpublished entries
+ * @todo Add minimum/maximum age limitations
+ */
+ public function hasUnpublishedEntries () {
+ // Call method on database wrapper
+ $hasUnpublished = $this->getWrapperInstance()->hasUnpublishedEntries();
+
+ // Return it
+ return $hasUnpublished;
+ }
+
+ /**
+ * Initializes publication of DHT entries. This does only prepare
+ * publication. The next step is to pickup such prepared entries and publish
+ * them by uploading to other (recently appeared) DHT members.
+ *
+ * @return void
+ */
+ public function initEntryPublication () {
+ // Call method on database wrapper
+ $this->getWrapperInstance()->initEntryPublication();
+
+ // Get result instance
+ $resultInstance = $this->getWrapperInstance()->getUnpublishedEntriesInstance();
+
+ // Must still be valid
+ assert($resultInstance->valid());
+
+ // "Walk" through all entries
+ while ($resultInstance->next()) {
+ // Get current entry
+ $current = $resultInstance->current();
+
+ // Make sure only valid entries pass
+ // @TODO Maybe add more small checks?
+ assert(is_array($current));
+
+ // ... and push it to the next stack
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...');
+ $this->getStackerInstance()->pushNamed(self::STACKER_NAME_PENDING_PUBLISHING, $current);
+ } // END - while
+ }
+
+ /**
+ * Checks whether there are entries pending publication
+ *
+ * @return $isPending Whether there are entries pending publication
+ */
+ public function hasEntriesPendingPublication () {
+ // Determine it if it is not empty
+ $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PENDING_PUBLISHING) === FALSE);
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isPending=' . intval($isPending));
+ return $isPending;
+ }
+
+ /**
+ * Publishes next entry found in stack. This method shall also update the
+ * corresponding dabase entry.
+ *
+ * @return void
+ * @todo Find out if loadDescriptorXml() can be called only once to avoid a lot methods working.
+ */
+ public function publishEntry () {
+ // This test must not fail
+ assert($this->hasEntriesPendingPublication());
+
+ // Is there an instance?
+ if (!$this->publishHelperInstance instanceof HelpableDht) {
+ // Get a helper instance
+ $this->publishHelperInstance = ObjectFactory::createObjectByConfiguredName('dht_publish_entry_helper_class');
+ } // END - if
+
+ // Load the announcement descriptor
+ $this->publishHelperInstance->loadDescriptorXml($this);
+
+ // Compile all variables
+ $this->publishHelperInstance->getTemplateInstance()->compileConfigInVariables();
+
+ // "Publish" the descriptor by sending it to the bootstrap/list nodes
+ $this->publishHelperInstance->sendPackage($this);
+ }
}
// [EOF]