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