]> git.mxchange.org Git - hub.git/blob - application/hub/main/producer/class_BaseProducer.php
9c22b5dc02016f7ab9faf6100a9d73d7b5196e46
[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          * 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                 // Initialize work queue (out-going, produced items)
59                 $this->initWorkQueue();
60         }
61
62         /**
63          * Getter for outgoing work queue
64          *
65          * @param       $outgoingQueueInstance  The outgoing work queue instance
66          */
67         protected final function getOutgoingQueueInstance () {
68                 return $this->outgoingQueueInstance;
69         }
70
71         /**
72          * Setter for outgoing work queue
73          *
74          * @param       $outgoingQueueInstance  The outgoing work queue instance
75          * @return      void
76          */
77         private final function setOutgoingQueueInstance (Stackable $outgoingQueueInstance) {
78                 $this->outgoingQueueInstance = $outgoingQueueInstance;
79         }
80
81         /**
82          * Getter for incoming raw data/items queue
83          *
84          * @param       $incomingQueueInstance  The incoming raw data/items queue instance
85          */
86         protected final function getIncomingQueueInstance () {
87                 return $this->incomingQueueInstance;
88         }
89
90         /**
91          * Setter for incoming raw data/items queue
92          *
93          * @param       $incomingQueueInstance  The incoming raw data/items queue instance
94          * @return      void
95          */
96         private final function setIncomingQueueInstance (Stackable $incomingQueueInstance) {
97                 $this->incomingQueueInstance = $incomingQueueInstance;
98         }
99
100         /**
101          * Initializes this producer, this method must be overwritten.
102          *
103          * @return      void
104          */
105         abstract protected function initProducer();
106
107         /**
108          * Initializes the work queue which is being used for outgoing, produced
109          * items.
110          *
111          * @return      void
112          */
113         protected function initWorkQueue () {
114                 // Get an instance and set it in this producer
115                 $this->setOutgoingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_outgoing_queue'));
116
117                 // Init the queue
118                 $this->initOutgoingQueue();
119
120                 // Get an instance and set it in this producer
121                 $this->setIncomingQueueInstance(ObjectFactory::createObjectByConfiguredName('producer_incoming_queue'));
122
123                 // Init the queue
124                 $this->initIncomingQueue();
125
126                 // Debug message
127                 self::createDebugInstance(__CLASS__)->debugOutput('PRODUCER: All queues have been initialized.');
128         }
129
130         /**
131          * Inits the out-going queue stack
132          *
133          * @return      void
134          */
135         protected function initOutgoingQueue () {
136                 $this->getOutgoingQueueInstance()->initStack(self::STACKER_NAME_OUT_QUEUE, TRUE);
137         }
138
139         /**
140          * Adds an entry to the out-going work queue
141          *
142          * @param       $value  The value to be added to the out-going work queue
143          * @return      void
144          */
145         protected function addValueToOutgoingQueue ($value) {
146                 $this->getOutgoingQueueInstance()->pushNamed(self::STACKER_NAME_OUT_QUEUE, $value);
147         }
148
149         /**
150          * Checks whether a configurable out-going queue limit has been reached
151          *
152          * @param       $configEntry    Configuration entry where the limit is stored
153          * @return      $isReached              Whether the limit is reached
154          */
155         protected function isOutgoingQueueLimitReached ($configEntry) {
156                 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getOutgoingQueueInstance()->getStackCount(self::STACKER_NAME_OUT_QUEUE));
157         }
158
159         /**
160          * Inits the incoming queue stack
161          *
162          * @return      void
163          */
164         protected function initIncomingQueue () {
165                 $this->getIncomingQueueInstance()->initStack(self::STACKER_NAME_IN_QUEUE, TRUE);
166         }
167
168         /**
169          * Adds an entry to the incoming work queue
170          *
171          * @param       $value  The value to be added to the incoming work queue
172          * @return      void
173          */
174         protected function addValueToIncomingQueue ($value) {
175                 $this->getIncomingQueueInstance()->pushNamed(self::STACKER_NAME_IN_QUEUE, $value);
176         }
177
178         /**
179          * Checks whether a configurable incoming queue limit has been reached
180          *
181          * @param       $configEntry    Configuration entry where the limit is stored
182          * @return      $isReached              Whether the limit is reached
183          */
184         protected function isIncomingQueueLimitReached($configEntry) {
185                 return ($this->getConfigInstance()->getConfigEntry($configEntry) <= $this->getIncomingQueueInstance()->getStackCount(self::STACKER_NAME_IN_QUEUE));
186         }
187 }
188
189 // [EOF]
190 ?>