Updated 'core'.
[hub.git] / application / hub / main / cruncher / class_BaseHubCruncher.php
1 <?php
2 /**
3  * A general hub cruncher class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2011 - 2014 Cruncher Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 abstract class BaseHubCruncher extends BaseHubSystem implements Updateable {
25         /**
26          * Version information
27          */
28         private $version = 'x.x';
29
30         /**
31          * By default no cruncher is active
32          */
33         private $isActive = FALSE;
34
35         /**
36          * All buffer queue instances (a FIFO)
37          */
38         private $bufferInstance = NULL;
39
40         /**
41          * Stacker name for incoming queue
42          */
43         const STACKER_NAME_IN_QUEUE = 'in_queue';
44
45         /**
46          * Stacker name for outcoming queue
47          */
48         const STACKER_NAME_OUT_QUEUE = 'out_queue';
49
50         /**
51          * Protected constructor
52          *
53          * @param       $className      Name of the class
54          * @return      void
55          */
56         protected function __construct ($className) {
57                 // Call parent constructor
58                 parent::__construct($className);
59
60                 // Set this cruncher instance in registry
61                 Registry::getRegistry()->addInstance('cruncher', $this);
62
63                 // Init this cruncher
64                 $this->initCruncher();
65         }
66
67         /**
68          * Initialize the cruncher generically
69          *
70          * @return      void
71          */
72         private function initCruncher () {
73                 // Init the state
74                 CruncherStateFactory::createCruncherStateInstanceByName('init');
75         }
76
77         /**
78          * Getter for version
79          *
80          * @return      $version        Version number of this cruncher
81          */
82         protected final function getVersion () {
83                 return $this->version;
84         }
85
86         /**
87          * Setter for version
88          *
89          * @param       $version        Version number of this cruncher
90          * @return      void
91          */
92         protected final function setVersion ($version) {
93                 $this->version = (string) $version;
94         }
95
96         /**
97          * Checks whether the in-buffer queue is filled by comparing it's current
98          * amount of entries against a threshold.
99          *
100          * @return      $isFilled       Whether the in-buffer is filled
101          */
102         protected function isInBufferQueueFilled () {
103                 // Determine it
104                 $isFilled = ($this->bufferInstance->getStackCount(self::STACKER_NAME_IN_QUEUE) > $this->getConfigInstance()->getConfigEntry('cruncher_in_buffer_min_threshold'));
105
106                 // And return the result
107                 return $isFilled;
108         }
109
110         /**
111          * This method fills the in-buffer with (a) test unit(s) which are mainly
112          * used for development of the crunching part. They must be enabled in
113          * configuration, or else your cruncher runs out of WUs and waits for more
114          * to show up.
115          *
116          * In this method we already know that the in-buffer is going depleted so
117          * no need to double-check it here.
118          *
119          * @return      void
120          */
121         abstract protected function fillInBufferQueueWithTestUnits ();
122
123         /**
124          * This method fills the in-buffer with (real) WUs which will be crunched
125          * and the result be sent back to the key producer instance.
126          *
127          * @return      void
128          */
129         abstract protected function fillInBufferQueueWithWorkUnits ();
130
131         /**
132          * Enables/disables the cruncher (just sets a flag)
133          *
134          * @param       $version        Version number of this cruncher
135          * @return      void
136          */
137         public final function enableIsActive ($isActive = TRUE) {
138                 $this->isActive = (bool) $isActive;
139         }
140
141         /**
142          * Determines whether the cruncher is active
143          *
144          * @return      $isActive       Whether the cruncher is active
145          */
146         public final function isActive () {
147                 return $this->isActive;
148         }
149
150         /**
151          * Initializes all buffer queues (mostly in/out). This method is demanded
152          * by the CruncherHelper interface.
153          *
154          * @return      void
155          */
156         public function initBufferQueues () {
157                 /*
158                  * Initialize both buffer queues, we can use the FIFO class here
159                  * directly and encapsulate its method calls with protected methods.
160                  */
161                 $this->bufferInstance = ObjectFactory::createObjectByConfiguredName('cruncher_buffer_stacker_class');
162
163                 // Initialize common stackers, like in/out
164                 $this->bufferInstance->initStacks(array(
165                         self::STACKER_NAME_IN_QUEUE,
166                         self::STACKER_NAME_OUT_QUEUE
167                 ));
168
169                 // Output debug message
170                 self::createDebugInstance(__CLASS__)->debugOutput('CRUNCHER: All buffers are now initialized.');
171         }
172
173         /**
174          * This method determines if the in-buffer is going to depleted and if so,
175          * it fetches more WUs from the network. If no WU can be fetched from the
176          * network and if enabled, a random test WU is being generated.
177          *
178          * This method is demanded from the CruncherHelper interface.
179          *
180          * @return      void
181          */
182         public function doFetchWorkUnits () {
183                 // Simply check if we have enough WUs left in the in-buffer queue (a FIFO)
184                 if (!$this->isInBufferQueueFilled()) {
185                         // The in-buffer queue needs filling, so head out and get some work
186                         $this->fillInBufferQueueWithWorkUnits();
187
188                         // Is the buffer still not filled and are test-packages allowed?
189                         if ((!$this->isInBufferQueueFilled()) && ($this->getConfigInstance()->getConfigEntry('cruncher_test_units_enabled') == 'Y')) {
190                                 // Then fill the in-buffer with (one) test-unit(s)
191                                 $this->fillInBufferQueueWithTestUnits();
192                         } // END - if
193                 } // END - if
194         }
195
196         /**
197          * Updates a given field with new value
198          *
199          * @param       $fieldName              Field to update
200          * @param       $fieldValue             New value to store
201          * @return      void
202          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
203          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
204          */
205         public function updateDatabaseField ($fieldName, $fieldValue) {
206                 // Unfinished
207                 $this->partialStub('Unfinished!');
208                 return;
209         }
210 }
211
212 // [EOF]
213 ?>