3 * A CruncherKey producer class
5 * @author Roland Haeder <webmaster@shipsimu.org>
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
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 class CruncherKeyProducer extends BaseKeyProducer implements KeyProducer, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this class
38 * @return $producerInstance An instance of a Producer class
40 public final static function createCruncherKeyProducer () {
42 $producerInstance = new CruncherKeyProducer();
44 // Get a helper instance, we now only need this for the key iterator
45 $helperInstance = ObjectFactory::createObjectByConfiguredName('crypto_random_message_helper_class', array('test'));
47 // Next get an iterator, again the helper will do that for us
48 $iteratorInstance = $helperInstance->getKeyIterator();
50 // Set it in the producer
51 $producerInstance->setIteratorInstance($iteratorInstance);
53 // Return the prepared instance
54 return $producerInstance;
58 * Initializes the producer. This method satisfies the abstract BaseProducer
62 * @todo Find something for init phase of this key producer
64 protected function initProducer () {
68 * Initializes the executor, whatever it does.
73 public function initExecutor (Stateable $stateInstance) {
74 $this->partialStub('Maybe implement this method?');
78 * Produces some keys and pushes them onto the queue
80 * @param $stateInstance An instance of a Stateable instance
84 public function produceKeys (Stateable $stateInstance) {
85 // Is this cruncher virgin?
86 if (!$stateInstance->isCruncherStateVirgin()) {
87 // This cruncher is not virgin, so skip it
88 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: The cruncher is not virgin. stateInstance=' . $stateInstance->__toString() . '');
90 } elseif (!$this->getIteratorInstance()->valid()) {
91 // This producer's iterator has finished its assignment
92 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: Finished creating keys. iteratorInstance=' . $this->getIteratorInstance()->__toString() . '');
97 * Now we need to create an iterator, just as for the work units,
98 * to create new keys from the encrypted message. The iterator will
99 * not iterate over an object nor a collection. It will instead
100 * encapsulate the "key production" into a class and not in a simple
101 * for() loop. These keys then needs to be bundled into test units
102 * and stored to database for later re-usage.
106 * Get current key (which is not the key of the iterator) This is always
109 $currentKey = $this->getIteratorInstance()->current();
111 // Add it to the out-going work queue
112 $this->addValueToOutgoingQueue($currentKey);
114 // Is the per-work unit limit reached?
115 if ($this->isOutgoingQueueLimitReached('cruncher_per_unit_key_limit')) {
116 // @TODO Send the produced key bundle to the unit producer's input queue
117 self::createDebugInstance(__CLASS__)->debugOutput('currentKey(b64)="' . base64_encode($currentKey) . '" needs to be processed.');
119 // At last re-init the stack
120 $this->initOutgoingQueue();
123 // Continue with next one
124 $this->getIteratorInstance()->next();