Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / producer / class_BaseProducer.php
1 <?php
2 /**
3  * A general Producer class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub 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 BaseProducer extends BaseFrameworkSystem {
25         /**
26          * Outgoing work-queue
27          */
28         private $outgoingQueueInstance = NULL;
29
30         /**
31          * Incoming raw data/items queue
32          */
33         private $incomingQueueInstance = NULL;
34
35         /**
36          * Stacker name for incoming work
37          */
38         const STACKER_NAME_IN_QUEUE = 'incoming_queue';
39
40         /**
41          * Stacker name for outgoing work
42          */
43         const STACKER_NAME_OUT_QUEUE = 'outgoing_queue';
44
45         /**
46          * Protected constructor
47          *
48          * @param       $className      Name of the class
49          * @return      void
50          */
51         protected function __construct ($className) {
52                 // Call parent constructor
53                 parent::__construct($className);
54
55                 // Initialize all producers
56                 $this->initProducer();
57
58                 // Get miner instance
59                 $minerInstance = Registry::getRegistry()->getInstance('miner');
60
61                 // Change state to next state
62                 $minerInstance->blockProducerHasInitialized($this);
63
64                 // Initialize work queue (out-going, produced items)
65                 $this->initWorkQueue();
66         }
67
68         /**
69          * Getter for outgoing work queue
70          *
71          * @param       $outgoingQueueInstance  The outgoing work queue instance
72          */
73         protected final function getOutgoingQueueInstance () {
74                 return $this->outgoingQueueInstance;
75         }
76
77         /**
78          * Setter for outgoing work queue
79          *
80          * @param       $outgoingQueueInstance  The outgoing work queue instance
81          * @return      void
82          */
83         private final function setOutgoingQueueInstance (Stackable $outgoingQueueInstance) {
84                 $this->outgoingQueueInstance = $outgoingQueueInstance;
85         }
86
87         /**
88          * Getter for incoming raw data/items queue
89          *
90          * @param       $incomingQueueInstance  The incoming raw data/items queue instance
91          */
92         protected final function getIncomingQueueInstance () {
93                 return $this->incomingQueueInstance;
94         }
95
96         /**
97          * Setter for incoming raw data/items queue
98          *
99          * @param       $incomingQueueInstance  The incoming raw data/items queue instance
100          * @return      void
101          */
102         private final function setIncomingQueueInstance (Stackable $incomingQueueInstance) {
103                 $this->incomingQueueInstance = $incomingQueueInstance;
104         }
105
106         /**
107          * Initializes this producer, this method must be overwritten.
108          *
109          * @return      void
110          */
111         abstract protected function initProducer();
112
113         /**
114          * Initializes the work queue which is being used for outgoing, produced
115          * items.
116          *
117          * @return      void
118          */
119         protected function initWorkQueue () {
120                 // Get an instance and set it in this producer
121                 $this->setOutgoingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_outgoing_queue'));
122
123                 // Init the queue
124                 $this->initOutgoingQueue();
125
126                 // Get an instance and set it in this producer
127                 $this->setIncomingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_incoming_queue'));
128
129                 // Init the queue
130                 $this->initIncomingQueue();
131
132                 // Debug message
133                 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: All queues have been initialized.');
134         }
135
136         /**
137          * Inits the out-going queue stack
138          *
139          * @return      void
140          */
141         protected function initOutgoingQueue () {
142                 $this->getOutgoingQueueInstance()->initStack(self::STACKER_NAME_OUT_QUEUE, TRUE);
143         }
144
145         /**
146          * Adds an entry to the out-going work queue
147          *
148          * @param       $value  The value to be added to the out-going work queue
149          * @return      void
150          */
151         protected function addValueToOutgoingQueue ($value) {
152                 $this->getOutgoingQueueInstance()->pushNamed(self::STACKER_NAME_OUT_QUEUE, $value);
153         }
154
155         /**
156          * Checks whether a configurable out-going queue limit has been reached
157          *
158          * @param       $configEntry    Configuration entry where the limit is stored
159          * @return      $isReached              Whether the limit is reached
160          */
161         protected function isOutgoingQueueLimitReached ($configEntry) {
162                 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getOutgoingQueueInstance()->getStackCount(self::STACKER_NAME_OUT_QUEUE));
163         }
164
165         /**
166          * Inits the incoming queue stack
167          *
168          * @return      void
169          */
170         protected function initIncomingQueue () {
171                 $this->getIncomingQueueInstance()->initStack(self::STACKER_NAME_IN_QUEUE, TRUE);
172         }
173
174         /**
175          * Adds an entry to the incoming work queue
176          *
177          * @param       $value  The value to be added to the incoming work queue
178          * @return      void
179          */
180         protected function addValueToIncomingQueue ($value) {
181                 $this->getIncomingQueueInstance()->pushNamed(self::STACKER_NAME_IN_QUEUE, $value);
182         }
183
184         /**
185          * Checks whether a configurable incoming queue limit has been reached
186          *
187          * @param       $configEntry    Configuration entry where the limit is stored
188          * @return      $isReached              Whether the limit is reached
189          */
190         protected function isIncomingQueueLimitReached($configEntry) {
191                 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getIncomingQueueInstance()->getStackCount(self::STACKER_NAME_IN_QUEUE));
192         }
193 }
194
195 // [EOF]
196 ?>