5 * @author Roland Haeder <webmaster@shipsimu.org>
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
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 $visitableInstance An instance of a class that should bed added to the pool
79 protected final function addInstance ($group, $poolName, Visitable $visitableInstance) {
81 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: group=' . $group . ',poolName=' . $poolName . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
83 // Make sure the group is not 'invalid'
84 assert($group != 'invalid');
86 // Is the pool group there?
87 if (!$this->getPoolEntriesInstance()->isGroupSet($group)) {
88 // Create the missing pool group
89 $this->getPoolEntriesInstance()->addGroup($group);
92 // Add it to given pool group
93 $this->getPoolEntriesInstance()->addInstance($group, $poolName, $visitableInstance);
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
100 * Adds an entry to the pool
102 * @param $poolEntry The new pool entry that should be added
105 protected final function addPoolEntry ($poolEntry) {
106 $this->getPoolEntriesInstance()->addEntry('pool', $poolEntry);
110 * Accepts the visitor to process the visit "request"
112 * @param $visitorInstance An instance of a Visitor class
115 public function accept (Visitor $visitorInstance) {
117 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - CALLED!');
120 $visitorInstance->visitPool($this);
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');
127 // Get a new iterator instance
128 $iteratorInstance = ObjectFactory::createObjectByConfiguredName($visitorInstance->getVisitorMode() . '_pool_iterator_class', array($this->getPoolEntriesInstance()));
130 // Add it to the registry
131 Registry::getRegistry()->addInstance($visitorInstance->getVisitorMode() . '_pool_iterator', $iteratorInstance);
135 $iteratorInstance->rewind();
137 // Visit all registered entries
138 while ($iteratorInstance->valid()) {
140 $poolEntry = $iteratorInstance->current();
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);
148 // Cannot visit this entry
149 $this->partialStub('Pool entry with key ' . $iteratorInstance->key() . ' has improper type ' . gettype($poolEntry) . ', reason: not implementing Visitable.');
152 // Advance to next entry
153 $iteratorInstance->next();
157 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - EXIT!');
161 * Gets the array from specified list
163 * @param $list The list identifier we should return
164 * @return $array The requested array
166 protected final function getArrayFromList ($list) {
168 $array = $this->getPoolEntriesInstance()->getArrayFromList($list);
175 * Checks whether the given connection type is valid
177 * @param $connectionType Type of connection, can be 'incoming', 'outgoing' or 'server'
178 * @return $isValid Whether the provided connection type is valid
180 protected function isValidConnectionType ($connectionType) {
182 $isValid = in_array($connectionType, $this->connectionTypes, TRUE);