]> git.mxchange.org Git - hub.git/blob - application/hub/main/producer/cruncher/keys/class_CruncherKeyProducer.php
e18cdf4a226ef592563e7ab71c339a74caed184d
[hub.git] / application / hub / main / 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          * Produces some keys and pushes them onto the queue
69          *
70          * @param       $stateInstance  An instance of a Stateable instance
71          * @return      void
72          * @todo        ~30% done
73          */
74         public function produceKeys (Stateable $stateInstance) {
75                 // Is this cruncher virgin?
76                 if (!$stateInstance->isCruncherStateVirgin()) {
77                         // This cruncher is not virgin, so skip it
78                         self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: The cruncher is not virgin. stateInstance=' . $stateInstance->__toString() . '');
79                         return;
80                 } elseif (!$this->getIteratorInstance()->valid()) {
81                         // This producer's iterator has finished its assignment
82                         self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: Finished creating keys. iteratorInstance=' . $this->getIteratorInstance()->__toString() . '');
83                         return;
84                 }
85
86                 /*
87                  * Now we need to create an iterator, just as for the work units,
88                  * to create new keys from the encrypted message. The iterator will
89                  * not iterate over an object nor a collection. It will instead
90                  * encapsulate the "key production" into a class and not in a simple
91                  * for() loop. These keys then needs to be bundled into test units
92                  * and stored to database for later re-usage.
93                  */
94
95                 /*
96                  * Get current key (which is not the key of the iterator) This is always
97                  * an ASCII string.
98                  */
99                 $currentKey = $this->getIteratorInstance()->current();
100
101                 // Add it to the out-going work queue
102                 $this->addValueToOutgoingQueue($currentKey);
103
104                 // Is the per-work unit limit reached?
105                 if ($this->isOutgoingQueueLimitReached('cruncher_per_unit_key_limit')) {
106                         // @TODO Send the produced key bundle to the unit producer's input queue
107                         self::createDebugInstance(__CLASS__)->debugOutput('currentKey(b64)="' . base64_encode($currentKey) . '" needs to be processed.');
108
109                         // At last re-init the stack
110                         $this->initOutgoingQueue();
111                 } // END - if
112
113                 // Continue with next one
114                 $this->getIteratorInstance()->next();
115         }
116 }
117
118 // [EOF]
119 ?>