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