]> git.mxchange.org Git - hub.git/blob - application/hub/main/pools/class_BasePool.php
9090a8be640f65461b3e7e5fe1ba99c8d47781c3
[hub.git] / application / hub / main / pools / class_BasePool.php
1 <?php
2 /**
3  * A general pool class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 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 class BasePool extends BaseHubSystem implements Visitable {
25         /**
26          * Socket array elements
27          */
28         const SOCKET_ARRAY_RESOURCE  = 'resource';
29         const SOCKET_ARRAY_CONN_TYPE = 'connection_type';
30
31         /**
32          * A list of pool entries
33          */
34         private $poolEntriesInstance = NULL;
35
36         /**
37          * An array with all valid connection types
38          */
39         private $connectionTypes = array();
40
41         /**
42          * Protected constructor
43          *
44          * @param       $className      Name of the class
45          * @return      void
46          */
47         protected function __construct ($className) {
48                 // Call parent constructor
49                 parent::__construct($className);
50
51                 // Init the pool entries
52                 $this->poolEntriesInstance = ObjectFactory::createObjectByConfiguredName('pool_entries_list_class');
53
54                 // Init array of connection types
55                 $this->connectionTypes = array(
56                         BaseConnectionHelper::CONNECTION_TYPE_INCOMING,
57                         BaseConnectionHelper::CONNECTION_TYPE_OUTGOING,
58                         BaseConnectionHelper::CONNECTION_TYPE_SERVER
59                 );
60         }
61
62         /**
63          * Getter for pool entries instance
64          *
65          * @return      $poolEntriesInstance    An instance for pool entries (list)
66          */
67         public final function getPoolEntriesInstance () {
68                 return $this->poolEntriesInstance;
69         }
70
71         /**
72          * Adds an instance to a pool segment
73          *
74          * @param       $group                          Name of the pool group
75          * @param       $poolSegment            Name of the pool segment
76          * @param       $visitableInstance      An instance of a class that should bed added to the pool
77          * @return      void
78          */
79         protected final function addInstance ($group, $poolName, Visitable $visitableInstance) {
80                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: group=' . $group . ',poolName=' . $poolName . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
81
82                 // Make sure the group is not 'invalid'
83                 assert($group != 'invalid');
84
85                 // Is the pool group there?
86                 if (!$this->getPoolEntriesInstance()->isGroupSet($group)) {
87                         // Create the missing pool group
88                         $this->getPoolEntriesInstance()->addGroup($group);
89                 } // END - if
90
91                 // Add it to given pool group
92                 $this->getPoolEntriesInstance()->addInstance($group, $poolName, $visitableInstance);
93         }
94
95         /**
96          * Adds an entry to the pool
97          *
98          * @param       $poolEntry      The new pool entry that should be added
99          * @return      void
100          */
101         protected final function addPoolEntry ($poolEntry) {
102                 $this->getPoolEntriesInstance()->addEntry('pool', $poolEntry);
103         }
104
105         /**
106          * Accepts the visitor to process the visit "request"
107          *
108          * @param       $visitorInstance        An instance of a Visitor class
109          * @return      void
110          */
111         public function accept (Visitor $visitorInstance) {
112                 // Debug message
113                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START');
114
115                 // Visit this pool
116                 $visitorInstance->visitPool($this);
117
118                 // Do we have a registry instance for this visitor's iterator?
119                 if (Registry::getRegistry()->instanceExists($visitorInstance->getVisitorMode() . '_pool_iterator')) {
120                         // Get the instance from registry
121                         $iteratorInstance = Registry::getRegistry()->getInstance($visitorInstance->getVisitorMode() . '_pool_iterator');
122                 } else {
123                         // Get a new iterator instance
124                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName($visitorInstance->getVisitorMode() . '_pool_iterator_class', array($this->getPoolEntriesInstance()));
125
126                         // Add it to the registry
127                         Registry::getRegistry()->addInstance($visitorInstance->getVisitorMode() . '_pool_iterator', $iteratorInstance);
128                 }
129
130                 // Reset the counter
131                 $iteratorInstance->rewind();
132
133                 // Visit all registered entries
134                 while ($iteratorInstance->valid()) {
135                         // Get current entry
136                         $poolEntry = $iteratorInstance->current();
137
138                         // Is this entry visitable?
139                         if ($poolEntry instanceof Visitable) {
140                                 // Visit this entry as well
141                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-POOL[' . __METHOD__ . ':' . __LINE__ . ']: Going to visit pooled object ' . $poolEntry->__toString() . ' with visitor ' . $visitorInstance->__toString() . ' ...');
142                                 $poolEntry->accept($visitorInstance);
143                         } else {
144                                 // Cannot visit this entry
145                                 $this->partialStub('Pool entry with key ' . $iteratorInstance->key() . ' has improper type ' . gettype($poolEntry) . ', reason: not implementing Visitable.');
146                         }
147
148                         // Advance to next entry
149                         $iteratorInstance->next();
150                 } // END - while
151
152                 // Debug message
153                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
154         }
155
156         /**
157          * Gets the array from specified list
158          *
159          * @param       $list   The list identifier we should return
160          * @return      $array  The requested array
161          */
162         protected final function getArrayFromList ($list) {
163                 // Get the array
164                 $array = $this->getPoolEntriesInstance()->getArrayFromList($list);
165
166                 // Return it
167                 return $array;
168         }
169
170         /**
171          * Checks whether the given connection type is valid
172          *
173          * @param       $connectionType         Type of connection, can be 'incoming', 'outgoing' or 'server'
174          * @return      $isValid                        Whether the provided connection type is valid
175          */
176         protected function isValidConnectionType ($connectionType) {
177                 // Is it valid?
178                 $isValid = in_array($connectionType, $this->connectionTypes, TRUE);
179
180                 // Return result
181                 return $isValid;
182         }
183 }
184
185 // [EOF]
186 ?>