Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / producer / cruncher / keys / class_CruncherKeyProducer.php
1 <?php
2 /**
3  * A CruncherKey producer 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 class CruncherKeyProducer extends BaseKeyProducer implements KeyProducer, Registerable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Creates an instance of this class
37          *
38          * @return      $producerInstance       An instance of a Producer class
39          */
40         public final static function createCruncherKeyProducer () {
41                 // Get new instance
42                 $producerInstance = new CruncherKeyProducer();
43
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'));
46
47                 // Next get an iterator, again the helper will do that for us
48                 $iteratorInstance = $helperInstance->getKeyIterator();
49
50                 // Set it in the producer
51                 $producerInstance->setIteratorInstance($iteratorInstance);
52
53                 // Return the prepared instance
54                 return $producerInstance;
55         }
56
57         /**
58          * Initializes the producer. This method satisfies the abstract BaseProducer
59          * class.
60          *
61          * @return      void
62          * @todo        Find something for init phase of this key producer
63          */
64         protected function initProducer () {
65         }
66
67         /**
68          * Initializes the executor, whatever it does.
69          *
70          * @return      void
71          * @todo        0% done
72          */
73         public function initExecutor (Stateable $stateInstance) {
74                 $this->partialStub('Maybe implement this method?');
75         }
76
77         /**
78          * Produces some keys and pushes them onto the queue
79          *
80          * @param       $stateInstance  An instance of a Stateable instance
81          * @return      void
82          * @todo        ~30% done
83          */
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() . '');
89                         return;
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() . '');
93                         return;
94                 }
95
96                 /*
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.
103                  */
104
105                 /*
106                  * Get current key (which is not the key of the iterator) This is always
107                  * an ASCII string.
108                  */
109                 $currentKey = $this->getIteratorInstance()->current();
110
111                 // Add it to the out-going work queue
112                 $this->addValueToOutgoingQueue($currentKey);
113
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.');
118
119                         // At last re-init the stack
120                         $this->initOutgoingQueue();
121                 } // END - if
122
123                 // Continue with next one
124                 $this->getIteratorInstance()->next();
125         }
126 }
127
128 // [EOF]
129 ?>