3 * A general hub cruncher class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 abstract class BaseHubCruncher extends BaseHubSystem implements Updateable {
28 private $version = 'x.x';
31 * By default no cruncher is active
33 private $isActive = false;
36 * All buffer queue instances (a FIFO)
38 private $bufferInstance = NULL;
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
51 $this->initCruncher();
55 * Initialize the cruncher generically
59 private function initCruncher () {
61 CruncherStateFactory::createCruncherStateInstanceByName('init', $this);
67 * @return $version Version number of this cruncher
69 protected final function getVersion () {
70 return $this->version;
76 * @param $version Version number of this cruncher
79 protected final function setVersion ($version) {
80 $this->version = (string) $version;
84 * Checks whether the in-buffer queue is filled by comparing it's current
85 * amount of entries against a threshold.
87 * @return $isFilled Whether the in-buffer is filled
89 protected function isInBufferQueueFilled () {
91 $isFilled = ($this->bufferInstance->getStackCount('in_queue') > $this->getConfigInstance()->getConfigEntry('cruncher_in_buffer_min_threshold'));
93 // And return the result
98 * This method fills the in-buffer with (a) test unit(s) which are mainly
99 * used for development of the crunching part. They must be enabled in
100 * configuration, or else your cruncher runs out of WUs and waits for more
103 * In this method we already know that the in-buffer is going depleted so
104 * we don't need to double-check it here.
108 abstract protected function fillInBufferQueueWithTestUnits ();
111 * This method fills the in-buffer with (real) WUs which will be crunched
112 * and the result be sent back to the key producer instance.
116 abstract protected function fillInBufferQueueWithWorkUnits ();
119 * Enables/disables the cruncher (just sets a flag)
121 * @param $version Version number of this cruncher
124 public final function enableIsActive ($isActive = true) {
125 $this->isActive = (bool) $isActive;
129 * Determines whether the cruncher is active
131 * @return $isActive Whether the cruncher is active
133 public final function isActive () {
134 return $this->isActive;
138 * Initializes all buffer queues (mostly in/out). This method is demanded
139 * by the CruncherHelper interface.
143 public function initBufferQueues () {
145 * Initialize both buffer queues, we can use the FIFO class here
146 * directly and encapsulate its method calls with protected methods.
148 $this->bufferInstance = ObjectFactory::createObjectByConfiguredName('cruncher_buffer_stacker_class');
150 // Initialize common stackers, like in/out
151 $this->bufferInstance->initStacker('in_queue');
152 $this->bufferInstance->initStacker('out_queue');
154 // Output debug message
155 self::createDebugInstance(__CLASS__)->debugOutput('CRUNCHER: All buffers are now initialized.');
159 * This method determines if the in-buffer is going to depleted and if so,
160 * it fetches more WUs from the network. If no WU can be fetched from the
161 * network and if enabled, a random test WU is being generated.
163 * This method is demanded from the CruncherHelper interface.
167 public function doFetchWorkUnits () {
168 // Simply check if we have enough WUs left in the in-buffer queue (a FIFO)
169 if (!$this->isInBufferQueueFilled()) {
170 // The in-buffer queue needs filling, so head out and get some work
171 $this->fillInBufferQueueWithWorkUnits();
173 // Is the buffer still not filled and are test-packages allowed?
174 if ((!$this->isInBufferQueueFilled()) && ($this->getConfigInstance()->getConfigEntry('cruncher_test_units_enabled') == 'Y')) {
175 // Then fill the in-buffer with (one) test-unit(s)
176 $this->fillInBufferQueueWithTestUnits();
182 * Updates a given field with new value
184 * @param $fieldName Field to update
185 * @param $fieldValue New value to store
187 * @throws DatabaseUpdateSupportException If this class does not support database updates
188 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
190 public function updateDatabaseField ($fieldName, $fieldValue) {
192 $this->partialStub('Unfinished!');