3 * A general Producer class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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();
58 // Initialize work queue (out-going, produced items)
59 $this->initWorkQueue();
63 * Getter for outgoing work queue
65 * @param $outgoingQueueInstance The outgoing work queue instance
67 protected final function getOutgoingQueueInstance () {
68 return $this->outgoingQueueInstance;
72 * Setter for outgoing work queue
74 * @param $outgoingQueueInstance The outgoing work queue instance
77 private final function setOutgoingQueueInstance (Stackable $outgoingQueueInstance) {
78 $this->outgoingQueueInstance = $outgoingQueueInstance;
82 * Getter for incoming raw data/items queue
84 * @param $incomingQueueInstance The incoming raw data/items queue instance
86 protected final function getIncomingQueueInstance () {
87 return $this->incomingQueueInstance;
91 * Setter for incoming raw data/items queue
93 * @param $incomingQueueInstance The incoming raw data/items queue instance
96 private final function setIncomingQueueInstance (Stackable $incomingQueueInstance) {
97 $this->incomingQueueInstance = $incomingQueueInstance;
101 * Initializes this producer, this method must be overwritten.
105 abstract protected function initProducer();
108 * Initializes the work queue which is being used for outgoing, produced
113 protected function initWorkQueue () {
114 // Get an instance and set it in this producer
115 $this->setOutgoingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_outgoing_queue'));
118 $this->initOutgoingQueue();
120 // Get an instance and set it in this producer
121 $this->setIncomingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_incoming_queue'));
124 $this->initIncomingQueue();
127 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: All queues have been initialized.');
131 * Inits the out-going queue stack
135 protected function initOutgoingQueue () {
136 $this->getOutgoingQueueInstance()->initStack(self::STACKER_NAME_OUT_QUEUE, TRUE);
140 * Adds an entry to the out-going work queue
142 * @param $value The value to be added to the out-going work queue
145 protected function addValueToOutgoingQueue ($value) {
146 $this->getOutgoingQueueInstance()->pushNamed(self::STACKER_NAME_OUT_QUEUE, $value);
150 * Checks whether a configurable out-going queue limit has been reached
152 * @param $configEntry Configuration entry where the limit is stored
153 * @return $isReached Whether the limit is reached
155 protected function isOutgoingQueueLimitReached ($configEntry) {
156 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getOutgoingQueueInstance()->getStackCount(self::STACKER_NAME_OUT_QUEUE));
160 * Inits the incoming queue stack
164 protected function initIncomingQueue () {
165 $this->getIncomingQueueInstance()->initStack(self::STACKER_NAME_IN_QUEUE, TRUE);
169 * Adds an entry to the incoming work queue
171 * @param $value The value to be added to the incoming work queue
174 protected function addValueToIncomingQueue ($value) {
175 $this->getIncomingQueueInstance()->pushNamed(self::STACKER_NAME_IN_QUEUE, $value);
179 * Checks whether a configurable incoming queue limit has been reached
181 * @param $configEntry Configuration entry where the limit is stored
182 * @return $isReached Whether the limit is reached
184 protected function isIncomingQueueLimitReached($configEntry) {
185 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getIncomingQueueInstance()->getStackCount(self::STACKER_NAME_IN_QUEUE));