]> git.mxchange.org Git - hub.git/blob - application/hub/main/pools/class_BasePool.php
Added line numbers to debug lines as this will become the 'norm'
[hub.git] / application / hub / main / pools / class_BasePool.php
1 <?php
2 /**
3  * A general pool 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 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       $instance               An instance of a class that should bed added to the pool
77          * @return      void
78          */
79         protected final function addInstance ($group, $poolName, Visitable $instance) {
80                 // Is the pool group there?
81                 if (!$this->getPoolEntriesInstance()->isGroupSet($group)) {
82                         // Create the missing pool group
83                         $this->getPoolEntriesInstance()->addGroup($group);
84                 } // END - if
85
86                 // Add it to given pool group
87                 $this->getPoolEntriesInstance()->addInstance($group, $poolName, $instance);
88         }
89
90         /**
91          * Adds an entry to the pool
92          *
93          * @param       $poolEntry      The new pool entry that should be added
94          * @return      void
95          */
96         protected final function addPoolEntry ($poolEntry) {
97                 $this->getPoolEntriesInstance()->addEntry('pool', $poolEntry);
98         }
99
100         /**
101          * Accepts the visitor to process the visit "request"
102          *
103          * @param       $visitorInstance        An instance of a Visitor class
104          * @return      void
105          */
106         public function accept (Visitor $visitorInstance) {
107                 // Debug message
108                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START');
109
110                 // Visit this pool
111                 $visitorInstance->visitPool($this);
112
113                 // Do we have a registry instance for this visitor's iterator?
114                 if (Registry::getRegistry()->instanceExists($visitorInstance->getVisitorMode() . '_pool_iterator')) {
115                         // Get the instance from registry
116                         $iteratorInstance = Registry::getRegistry()->getInstance($visitorInstance->getVisitorMode() . '_pool_iterator');
117                 } else {
118                         // Get a new iterator instance
119                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName($visitorInstance->getVisitorMode() . '_pool_iterator_class', array($this->getPoolEntriesInstance()));
120
121                         // Add it to the registry
122                         Registry::getRegistry()->addInstance($visitorInstance->getVisitorMode() . '_pool_iterator', $iteratorInstance);
123                 }
124
125                 // Reset the counter
126                 $iteratorInstance->rewind();
127
128                 // Visit all registered entries
129                 while ($iteratorInstance->valid()) {
130                         // Get current entry
131                         $poolEntry = $iteratorInstance->current();
132
133                         // Is this entry visitable?
134                         if ($poolEntry instanceof Visitable) {
135                                 // Visit this entry as well
136                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-POOL[' . __LINE__ . ']: Going to visit pooled object ' . $poolEntry->__toString() . ' with visitor ' . $visitorInstance->__toString() . ' ...');
137                                 $poolEntry->accept($visitorInstance);
138                         } else {
139                                 // Cannot visit this entry
140                                 $this->partialStub('Pool entry with key ' . $iteratorInstance->key() . ' has improper type ' . gettype($poolEntry) . ', reason: not implementing Visitable.');
141                         }
142
143                         // Advance to next entry
144                         $iteratorInstance->next();
145                 } // END - while
146
147                 // Debug message
148                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
149         }
150
151         /**
152          * Gets the array from specified list
153          *
154          * @param       $list   The list identifier we should return
155          * @return      $array  The requested array
156          */
157         protected final function getArrayFromList ($list) {
158                 // Get the array
159                 $array = $this->getPoolEntriesInstance()->getArrayFromList($list);
160
161                 // Return it
162                 return $array;
163         }
164
165         /**
166          * Checks whether the given connection type is valid
167          *
168          * @param       $connectionType         Type of connection, can be 'incoming', 'outgoing' or 'server'
169          * @return      $isValid                        Whether the provided connection type is valid
170          */
171         protected function isValidConnectionType ($connectionType) {
172                 // Is it valid?
173                 $isValid = in_array($connectionType, $this->connectionTypes, true);
174
175                 // Return result
176                 return $isValid;
177         }
178 }
179
180 // [EOF]
181 ?>