X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fminer%2Fclass_BaseHubMiner.php;fp=application%2Fhub%2Fmain%2Fminer%2Fclass_BaseHubMiner.php;h=a8e322321da5d3c43d23a0f83fed883b442f1f8a;hb=2f4667d20de8529f941eb7267e1497f05669c5b8;hp=0000000000000000000000000000000000000000;hpb=0f6ca728117cd91171aed79af515b52009570ced;p=hub.git diff --git a/application/hub/main/miner/class_BaseHubMiner.php b/application/hub/main/miner/class_BaseHubMiner.php new file mode 100644 index 000000000..a8e322321 --- /dev/null +++ b/application/hub/main/miner/class_BaseHubMiner.php @@ -0,0 +1,210 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2011 - 2012 Miner Developer Team + * @license GNU GPL 3.0 or any newer version + * @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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +abstract class BaseHubMiner extends BaseHubSystem implements Updateable { + /** + * Version information + */ + private $version = 'x.x'; + + /** + * By default no miner 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 + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + + // Init this miner + $this->initMiner(); + } + + /** + * Initialize the miner generically + * + * @return void + */ + private function initMiner () { + // Init the state + MinerStateFactory::createMinerStateInstanceByName('init', $this); + } + + /** + * Getter for version + * + * @return $version Version number of this miner + */ + protected final function getVersion () { + return $this->version; + } + + /** + * Setter for version + * + * @param $version Version number of this miner + * @return void + */ + protected final function setVersion ($version) { + $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('miner_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 miner 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 miner (just sets a flag) + * + * @param $version Version number of this miner + * @return void + */ + public final function enableIsActive ($isActive = TRUE) { + $this->isActive = (bool) $isActive; + } + + /** + * Determines whether the miner is active + * + * @return $isActive Whether the miner is active + */ + public final function isActive () { + return $this->isActive; + } + + /** + * Initializes all buffer queues (mostly in/out). This method is demanded + * by the MinerHelper 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('miner_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('MINER: 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 MinerHelper 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('miner_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 + * + * @param $fieldName Field to update + * @param $fieldValue New value to store + * @return void + * @throws DatabaseUpdateSupportException If this class does not support database updates + * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem + */ + public function updateDatabaseField ($fieldName, $fieldValue) { + // Unfinished + $this->partialStub('Unfinished!'); + return; + } +} + +// [EOF] +?>