X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fpools%2Fclass_BasePool.php;h=c65f8266b65d1fbcd11e9a69674b0bf782ee74a2;hb=9a1243bea20f7602ef7cdfe912a7db5ee958fba8;hp=6fb5bd1fd572c115c97f9b09050df3b67c745a82;hpb=fd2ceb64f224067ebc5e0b0afe8883e0a2378019;p=hub.git diff --git a/application/hub/main/pools/class_BasePool.php b/application/hub/main/pools/class_BasePool.php index 6fb5bd1fd..c65f8266b 100644 --- a/application/hub/main/pools/class_BasePool.php +++ b/application/hub/main/pools/class_BasePool.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -21,11 +21,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BasePool extends BaseHubSystem { +class BasePool extends BaseHubSystem implements Visitable { /** - * A list of instances for this pool + * Socket array elements */ - private $instancePool = array(); + const SOCKET_ARRAY_RESOURCE = 'resource'; + const SOCKET_ARRAY_CONN_TYPE = 'connection_type'; + + /** + * A list of pool entries + */ + private $poolEntriesInstance = NULL; + + /** + * An array with all valid connection types + */ + private $connectionTypes = array(); /** * Protected constructor @@ -36,17 +47,133 @@ class BasePool extends BaseHubSystem { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + + // Init the pool entries + $this->poolEntriesInstance = ObjectFactory::createObjectByConfiguredName('pool_entries_list_class'); + + // Init array of connection types + $this->connectionTypes = array( + BaseConnectionHelper::CONNECTION_TYPE_INCOMING, + BaseConnectionHelper::CONNECTION_TYPE_OUTGOING, + BaseConnectionHelper::CONNECTION_TYPE_SERVER + ); + } + + /** + * Getter for pool entries instance + * + * @return $poolEntriesInstance An instance for pool entries (list) + */ + public final function getPoolEntriesInstance () { + return $this->poolEntriesInstance; } /** * Adds an instance to a pool segment * + * @param $group Name of the pool group * @param $poolSegment Name of the pool segment - * @param $instance An instance of a class we should add to the pool + * @param $instance An instance of a class that should bed added to the pool + * @return void + */ + protected final function addInstance ($group, $poolName, Visitable $instance) { + // Is the pool group there? + if (!$this->getPoolEntriesInstance()->isGroupSet($group)) { + // Create the missing pool group + $this->getPoolEntriesInstance()->addGroup($group); + } // END - if + + // Add it to given pool group + $this->getPoolEntriesInstance()->addInstance($group, $poolName, $instance); + } + + /** + * Adds an entry to the pool + * + * @param $poolEntry The new pool entry that should be added + * @return void + */ + protected final function addPoolEntry ($poolEntry) { + $this->getPoolEntriesInstance()->addEntry('pool', $poolEntry); + } + + /** + * Accepts the visitor to process the visit "request" + * + * @param $visitorInstance An instance of a Visitor class * @return void */ - protected final function addInstance($poolName, FrameworkInterface $instance) { - $this->instancePool[$poolName][] = $instance; + public function accept (Visitor $visitorInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START'); + + // Visit this pool + $visitorInstance->visitPool($this); + + // Do we have a registry instance for this visitor's iterator? + if (Registry::getRegistry()->instanceExists($visitorInstance->getVisitorMode() . '_pool_iterator')) { + // Get the instance from registry + $iteratorInstance = Registry::getRegistry()->getInstance($visitorInstance->getVisitorMode() . '_pool_iterator'); + } else { + // Get a new iterator instance + $iteratorInstance = ObjectFactory::createObjectByConfiguredName($visitorInstance->getVisitorMode() . '_pool_iterator_class', array($this->getPoolEntriesInstance())); + + // Add it to the registry + Registry::getRegistry()->addInstance($visitorInstance->getVisitorMode() . '_pool_iterator', $iteratorInstance); + } + + // Reset the counter + $iteratorInstance->rewind(); + + // Visit all registered entries + while ($iteratorInstance->valid()) { + // Get current entry + $poolEntry = $iteratorInstance->current(); + + // Is this entry visitable? + if ($poolEntry instanceof Visitable) { + // Visit this entry as well + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-POOL[' . __LINE__ . ']: Going to visit pooled object ' . $poolEntry->__toString() . ' with visitor ' . $visitorInstance->__toString() . ' ...'); + $poolEntry->accept($visitorInstance); + } else { + // Cannot visit this entry + $this->partialStub('Pool entry with key ' . $iteratorInstance->key() . ' has improper type ' . gettype($poolEntry) . ', reason: not implementing Visitable.'); + } + + // Advance to next entry + $iteratorInstance->next(); + } // END - while + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); + } + + /** + * Gets the array from specified list + * + * @param $list The list identifier we should return + * @return $array The requested array + */ + protected final function getArrayFromList ($list) { + // Get the array + $array = $this->getPoolEntriesInstance()->getArrayFromList($list); + + // Return it + return $array; + } + + /** + * Checks whether the given connection type is valid + * + * @param $connectionType Type of connection, can be 'incoming', 'outgoing' or 'server' + * @return $isValid Whether the provided connection type is valid + */ + protected function isValidConnectionType ($connectionType) { + // Is it valid? + $isValid = in_array($connectionType, $this->connectionTypes, true); + + // Return result + return $isValid; } }