]> 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 54c5c3dec4d8962a75935257a73a55b2abfd041f..39471d34dca5da6286f48f774c76a96d2714c8ed 100644 (file)
@@ -21,7 +21,7 @@
  * 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
         */
@@ -46,6 +46,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);
        }
 
        /**
@@ -81,6 +94,27 @@ class BaseHubCruncher extends BaseHubSystem implements Updateable {
                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)
         *
@@ -101,7 +135,7 @@ class BaseHubCruncher extends BaseHubSystem implements Updateable {
        }
 
        /**
-        * Initializes all buffer queues (mostly in/out), this method is demanded
+        * Initializes all buffer queues (mostly in/out). This method is demanded
         * by the CruncherHelper interface.
         *
         * @return      void
@@ -121,6 +155,29 @@ class BaseHubCruncher extends BaseHubSystem implements Updateable {
                $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
+       }
+
        /**
         * Updates a given field with new value
         *