5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BasePool extends BaseHubSystem implements Visitable {
26 * Socket array elements
28 const SOCKET_ARRAY_RESOURCE = 'resource';
29 const SOCKET_ARRAY_CONN_TYPE = 'connection_type';
32 * A list of pool entries
34 private $poolEntriesInstance = NULL;
37 * An array with all valid connection types
39 private $connectionTypes = array();
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct ($className) {
48 // Call parent constructor
49 parent::__construct($className);
51 // Init the pool entries
52 $this->poolEntriesInstance = ObjectFactory::createObjectByConfiguredName('pool_entries_list_class');
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
63 * Getter for pool entries instance
65 * @return $poolEntriesInstance An instance for pool entries (list)
67 public final function getPoolEntriesInstance () {
68 return $this->poolEntriesInstance;
72 * Adds an instance to a pool segment
74 * @param $group Name of the pool group
75 * @param $poolSegment Name of the pool segment
76 * @param $instance An instance of a class we should add to the pool
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);
86 // Add it to given pool group
87 $this->getPoolEntriesInstance()->addInstance($group, $poolName, $instance);
91 * Adds an entry to the pool
93 * @param $poolEntry The new pool entry we should add
96 protected final function addPoolEntry ($poolEntry) {
97 $this->getPoolEntriesInstance()->addEntry('pool', $poolEntry);
101 * Accepts the visitor to process the visit "request"
103 * @param $visitorInstance An instance of a Visitor class
106 public function accept (Visitor $visitorInstance) {
108 //* NOISY-DEBUG: */ $this->debugOutput('POOL: ' . $visitorInstance->__toString() . ' has visited - START');
111 $visitorInstance->visitPool($this);
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');
118 // Get a new iterator instance
119 $iteratorInstance = ObjectFactory::createObjectByConfiguredName($visitorInstance->getVisitorMode() . '_pool_iterator_class', array($this->getPoolEntriesInstance()));
121 // Add it to the registry
122 Registry::getRegistry()->addInstance($visitorInstance->getVisitorMode() . '_pool_iterator', $iteratorInstance);
126 $iteratorInstance->rewind();
128 // Visit all registered entries
129 while ($iteratorInstance->valid()) {
131 $poolEntry = $iteratorInstance->current();
133 // Is this entry visitable?
134 if ($poolEntry instanceof Visitable) {
135 // Visit this entry as well
136 //* NOISY-DEBUG: */ $this->debugOutput('BASE-POOL: Going to visit pooled object ' . $poolEntry->__toString() . ' with visitor ' . $visitorInstance->__toString() . ' ...');
137 $poolEntry->accept($visitorInstance);
139 // Cannot visit this entry
140 $this->partialStub('Pool entry with key ' . $iteratorInstance->key() . ' has improper type ' . gettype($poolEntry) . ', reason: not implementing Visitable.');
143 // Advance to next entry
144 $iteratorInstance->next();
148 //* NOISY-DEBUG: */ $this->debugOutput('POOL: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
152 * Gets the array from specified list
154 * @param $list The list identifier we should return
155 * @return $array The requested array
157 protected final function getArrayFromList ($list) {
159 $array = $this->getPoolEntriesInstance()->getArrayFromList($list);
166 * Checks whether the given connection type is valid
168 * @param $connectionType Type of connection, can be 'incoming', 'outgoing' or 'server'
169 * @return $isValid Whether the provided connection type is valid
171 protected function isValidConnectionType ($connectionType) {
173 $isValid = in_array($connectionType, $this->connectionTypes, true);