]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/cruncher/class_BaseHubCruncher.php
Renamed 'stacker' -> 'stack'
[hub.git] / application / hub / main / cruncher / class_BaseHubCruncher.php
index 60ef0cb345bf1ae8fce13a5e523899782edaaadc..0f68adc7c27cd3d1314eb6fec06f2d2989b7eaba 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2011 - 2012 Cruncher Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseHubCruncher extends BaseHubSystem implements Updateable {
+abstract class BaseHubCruncher extends BaseHubSystem implements Updateable {
        /**
         * Version information
         */
        private $version = 'x.x';
 
+       /**
+        * By default no cruncher is active
+        */
+       private $isActive = FALSE;
+
+       /**
+        * All buffer queue instances (a FIFO)
+        */
+       private $bufferInstance = NULL;
+
+       /**
+        * Stacker name for incoming queue
+        */
+       const STACKER_NAME_IN_QUEUE = 'in_queue';
+
+       /**
+        * Stacker name for outcoming queue
+        */
+       const STACKER_NAME_OUT_QUEUE = 'out_queue';
+
        /**
         * Protected constructor
         *
@@ -36,6 +56,19 @@ class BaseHubCruncher extends BaseHubSystem implements Updateable {
        protected function __construct ($className) {
                // Call parent constructor
                parent::__construct($className);
+
+               // Init this cruncher
+               $this->initCruncher();
+       }
+
+       /**
+        * Initialize the cruncher generically
+        *
+        * @return      void
+        */
+       private function initCruncher () {
+               // Init the state
+               CruncherStateFactory::createCruncherStateInstanceByName('init', $this);
        }
 
        /**
@@ -57,6 +90,106 @@ class BaseHubCruncher extends BaseHubSystem implements Updateable {
                $this->version = (string) $version;
        }
 
+       /**
+        * Checks whether the in-buffer queue is filled by comparing it's current
+        * amount of entries against a threshold.
+        *
+        * @return      $isFilled       Whether the in-buffer is filled
+        */
+       protected function isInBufferQueueFilled () {
+               // Determine it
+               $isFilled = ($this->bufferInstance->getStackCount(self::STACKER_NAME_IN_QUEUE) > $this->getConfigInstance()->getConfigEntry('cruncher_in_buffer_min_threshold'));
+
+               // And return the result
+               return $isFilled;
+       }
+
+       /**
+        * This method fills the in-buffer with (a) test unit(s) which are mainly
+        * used for development of the crunching part. They must be enabled in
+        * configuration, or else your cruncher runs out of WUs and waits for more
+        * to show up.
+        *
+        * In this method we already know that the in-buffer is going depleted so
+        * we don't need to double-check it here.
+        *
+        * @return      void
+        */
+       abstract protected function fillInBufferQueueWithTestUnits ();
+
+       /**
+        * This method fills the in-buffer with (real) WUs which will be crunched
+        * and the result be sent back to the key producer instance.
+        *
+        * @return      void
+        */
+       abstract protected function fillInBufferQueueWithWorkUnits ();
+
+       /**
+        * Enables/disables the cruncher (just sets a flag)
+        *
+        * @param       $version        Version number of this cruncher
+        * @return      void
+        */
+       public final function enableIsActive ($isActive = TRUE) {
+               $this->isActive = (bool) $isActive;
+       }
+
+       /**
+        * Determines whether the cruncher is active
+        *
+        * @return      $isActive       Whether the cruncher is active
+        */
+       public final function isActive () {
+               return $this->isActive;
+       }
+
+       /**
+        * Initializes all buffer queues (mostly in/out). This method is demanded
+        * by the CruncherHelper interface.
+        *
+        * @return      void
+        */
+       public function initBufferQueues () {
+               /*
+                * Initialize both buffer queues, we can use the FIFO class here
+                * directly and encapsulate its method calls with protected methods.
+                */
+               $this->bufferInstance = ObjectFactory::createObjectByConfiguredName('cruncher_buffer_stacker_class');
+
+               // Initialize common stackers, like in/out
+               $this->bufferInstance->initStacks(array(
+                       self::STACKER_NAME_IN_QUEUE,
+                       self::STACKER_NAME_OUT_QUEUE
+               ));
+
+               // Output debug message
+               self::createDebugInstance(__CLASS__)->debugOutput('CRUNCHER: All buffers are now initialized.');
+       }
+
+       /**
+        * This method determines if the in-buffer is going to depleted and if so,
+        * it fetches more WUs from the network. If no WU can be fetched from the
+        * network and if enabled, a random test WU is being generated.
+        *
+        * This method is demanded from the CruncherHelper interface.
+        *
+        * @return      void
+        */
+       public function doFetchWorkUnits () {
+               // Simply check if we have enough WUs left in the in-buffer queue (a FIFO)
+               if (!$this->isInBufferQueueFilled()) {
+                       // The in-buffer queue needs filling, so head out and get some work
+                       $this->fillInBufferQueueWithWorkUnits();
+
+                       // Is the buffer still not filled and are test-packages allowed?
+                       if ((!$this->isInBufferQueueFilled()) && ($this->getConfigInstance()->getConfigEntry('cruncher_test_units_enabled') == 'Y')) {
+                               // Then fill the in-buffer with (one) test-unit(s)
+                               $this->fillInBufferQueueWithTestUnits();
+                       } // END - if
+               } // END - if
+       }
+
        /**
         * Updates a given field with new value
         *