3 * A general Producer class
5 * @author Roland Haeder <webmaster@shipsimu.org>
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
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 abstract class BaseProducer extends BaseFrameworkSystem {
28 private $outgoingQueueInstance = NULL;
31 * Incoming raw data/items queue
33 private $incomingQueueInstance = NULL;
36 * Stacker name for incoming work
38 const STACKER_NAME_IN_QUEUE = 'incoming_queue';
41 * Stacker name for outgoing work
43 const STACKER_NAME_OUT_QUEUE = 'outgoing_queue';
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className) {
52 // Call parent constructor
53 parent::__construct($className);
55 // Initialize all producers
56 $this->initProducer();
59 $minerInstance = Registry::getRegistry()->getInstance('miner');
61 // Change state to next state
62 $minerInstance->blockProducerHasInitialized($this);
64 // Initialize work queue (out-going, produced items)
65 $this->initWorkQueue();
69 * Getter for outgoing work queue
71 * @param $outgoingQueueInstance The outgoing work queue instance
73 protected final function getOutgoingQueueInstance () {
74 return $this->outgoingQueueInstance;
78 * Setter for outgoing work queue
80 * @param $outgoingQueueInstance The outgoing work queue instance
83 private final function setOutgoingQueueInstance (Stackable $outgoingQueueInstance) {
84 $this->outgoingQueueInstance = $outgoingQueueInstance;
88 * Getter for incoming raw data/items queue
90 * @param $incomingQueueInstance The incoming raw data/items queue instance
92 protected final function getIncomingQueueInstance () {
93 return $this->incomingQueueInstance;
97 * Setter for incoming raw data/items queue
99 * @param $incomingQueueInstance The incoming raw data/items queue instance
102 private final function setIncomingQueueInstance (Stackable $incomingQueueInstance) {
103 $this->incomingQueueInstance = $incomingQueueInstance;
107 * Initializes this producer, this method must be overwritten.
111 abstract protected function initProducer();
114 * Initializes the work queue which is being used for outgoing, produced
119 protected function initWorkQueue () {
120 // Get an instance and set it in this producer
121 $this->setOutgoingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_outgoing_queue'));
124 $this->initOutgoingQueue();
126 // Get an instance and set it in this producer
127 $this->setIncomingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_incoming_queue'));
130 $this->initIncomingQueue();
133 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: All queues have been initialized.');
137 * Inits the out-going queue stack
141 protected function initOutgoingQueue () {
142 $this->getOutgoingQueueInstance()->initStack(self::STACKER_NAME_OUT_QUEUE, TRUE);
146 * Adds an entry to the out-going work queue
148 * @param $value The value to be added to the out-going work queue
151 protected function addValueToOutgoingQueue ($value) {
152 $this->getOutgoingQueueInstance()->pushNamed(self::STACKER_NAME_OUT_QUEUE, $value);
156 * Checks whether a configurable out-going queue limit has been reached
158 * @param $configEntry Configuration entry where the limit is stored
159 * @return $isReached Whether the limit is reached
161 protected function isOutgoingQueueLimitReached ($configEntry) {
162 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getOutgoingQueueInstance()->getStackCount(self::STACKER_NAME_OUT_QUEUE));
166 * Inits the incoming queue stack
170 protected function initIncomingQueue () {
171 $this->getIncomingQueueInstance()->initStack(self::STACKER_NAME_IN_QUEUE, TRUE);
175 * Adds an entry to the incoming work queue
177 * @param $value The value to be added to the incoming work queue
180 protected function addValueToIncomingQueue ($value) {
181 $this->getIncomingQueueInstance()->pushNamed(self::STACKER_NAME_IN_QUEUE, $value);
185 * Checks whether a configurable incoming queue limit has been reached
187 * @param $configEntry Configuration entry where the limit is stored
188 * @return $isReached Whether the limit is reached
190 protected function isIncomingQueueLimitReached($configEntry) {
191 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getIncomingQueueInstance()->getStackCount(self::STACKER_NAME_IN_QUEUE));