-<?php
-/**
- * A general hub miner class
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-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;
-
- /**
- * An array for initialized producers
- */
- private $producersInitialized = array();
-
- /**
- * Stacker name for incoming queue
- */
- const STACKER_NAME_IN_QUEUE = 'in_queue';
-
- /**
- * Stacker name for outcoming queue
- */
- const STACKER_NAME_OUT_QUEUE = 'out_queue';
-
- /**
- * Maximum number of producers (2: test and real)
- */
- const MAX_PRODUCERS = 2;
-
- /**
- * 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 () {
- // Add own instance to registry
- Registry::getRegistry()->addInstance('miner', $this);
-
- // Init the state
- MinerStateFactory::createMinerStateInstanceByName('init');
- }
-
- /**
- * 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
- * no 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;
- }
-
- /**
- * Changes the state to 'booting' and shall be called after the block
- * producer has been initialized.
- *
- * @param $producerInstance An instance of a BlockProducer class
- * @return void
- */
- public function blockProducerHasInitialized (BlockProducer $producerInstance) {
- // Make sure the state is correct ('init')
- $this->getStateInstance()->validateMinerStateIsInit();
-
- // Mark given producer as initialized
- $this->producersInitialized[$producerInstance->__toString()] = TRUE;
-
- // Has all producers been initialized?
- if (count($this->producersInitialized) == self::MAX_PRODUCERS) {
- // Change it to 'booting'
- MinerStateFactory::createMinerStateInstanceByName('booting');
- } // END - if
- }
-}
-
-// [EOF]
-?>