]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/cruncher/class_BaseHubCruncher.php
Cruncher continued and rewritten to use states:
[hub.git] / application / hub / main / cruncher / class_BaseHubCruncher.php
index f552a19aa5b8dc62ed43f098cb6f7001cb9142e0..39471d34dca5da6286f48f774c76a96d2714c8ed 100644 (file)
  * 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;
+
        /**
         * Protected constructor
         *
@@ -31,6 +46,136 @@ 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);
+       }
+
+       /**
+        * Getter for version
+        *
+        * @return      $version        Version number of this cruncher
+        */
+       protected final function getVersion () {
+               return $this->version;
+       }
+
+       /**
+        * Setter for version
+        *
+        * @param       $version        Version number of this cruncher
+        * @return      void
+        */
+       protected final function setVersion ($version) {
+               $this->version = (string) $version;
+       }
+
+       /**
+        * Checks wether the in-buffer queue is filled by comparing it's current
+        * amount of entries against a threshold.
+        *
+        * @return      $isFilled       Wether the in-buffer is filled
+        */
+       protected function isInBufferQueueFilled () {
+               // Determine it
+               $isFilled = ($this->bufferInstance->getStackCount('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 wether the cruncher is active
+        *
+        * @return      $isActive       Wether 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->initStacker('in_queue');
+               $this->bufferInstance->initStacker('out_queue');
+
+               // Output debug message
+               $this->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
        }
 
        /**