application/hub/main/iterator/.htaccess -text
application/hub/main/iterator/class_ -text
application/hub/main/iterator/class_BaseIterator.php -text
+application/hub/main/iterator/network/.htaccess -text
+application/hub/main/iterator/network/class_NetworkListenIterator.php -text
application/hub/main/iterator/pool/.htaccess -text
application/hub/main/iterator/pool/handler/.htaccess -text
application/hub/main/iterator/pool/handler/class_Handler -text
// CFG: HANDLER-POOL-ITERATOR-CLASS
$cfg->setConfigEntry('handler_pool_iterator_class', 'HandlerPoolIterator');
+// CFG: NETWORK-LISTEN-ITERATOR-CLASS
+$cfg->setConfigEntry('network_listen_iterator_class', 'NetworkListenIterator');
+
// CFG: LIST-GROUP-CLASS
$cfg->setConfigEntry('list_group_class', 'ListGroupList');
--- /dev/null
+Deny from all
--- /dev/null
+<?php
+/**
+ * A NetworkListen iterator
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.ship-simu.org
+ * @todo This current implementation is not recommended, use a
+ * @todo latency-based iteration or similar approaches
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class NetworkListenIterator extends BaseIterator implements Iterator {
+ /**
+ * Key for the global list index
+ */
+ private $indexKey = 0;
+
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
+ protected function __construct () {
+ // Call parent constructor
+ parent::__construct(__CLASS__);
+ }
+
+ /**
+ * Creates an instance of this class
+ *
+ * @param $listInstance A list of a IteratorAggregate
+ * @return $iteratorInstance An instance a Iterator class
+ */
+ public final static function createNetworkListenIterator (IteratorAggregate $listInstance) {
+ // Get new instance
+ $iteratorInstance = new NetworkListenIterator();
+
+ // Set the list
+ $iteratorInstance->setListInstance($listInstance);
+
+ // Return the prepared instance
+ return $iteratorInstance;
+ }
+
+ /**
+ * Getter for current value from group or generic
+ *
+ * @return $current Current value in iteration
+ * @throws IndexOutOfBoundsException If $indexKey is out of bounds
+ */
+ public function current () {
+ // Default is null
+ $current = null;
+
+ // Is the entry valid?
+ if (!$this->valid()) {
+ // Throw an exception here
+ throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
+ } // END - if
+
+ // Now get the entry
+ $current = $this->getListInstance()->getEntry($this->key());
+
+ // Return it
+ return $current;
+ }
+
+ /**
+ * Getter for key from group or generic
+ *
+ * @return $indexKey Current key in iteration
+ */
+ public function key () {
+ return $this->indexKey;
+ }
+
+ /**
+ * Advances to the next entry
+ *
+ * @return void
+ */
+ public function next () {
+ $this->indexKey++;
+ }
+
+ /**
+ * Rewinds to the beginning of the iteration
+ *
+ * @return void
+ */
+ public function rewind () {
+ $this->indexKey = 0;
+ }
+
+ /**
+ * Checks wether the current entry is valid (not at the end of the list)
+ *
+ * @return $isValid Wether the current entry is there
+ */
+ public function valid () {
+ // Check for total active clients and if we are not at the end
+ $isValid = ($this->key() < $this->getListInstance()->count());
+
+ // Return result
+ return $isValid;
+ }
+}
+
+// [EOF]
+?>
* @return void
*/
protected final function setSocketResource ($socketResource) {
- $this->socketResource = $setSocketResource;
+ $this->socketResource = $socketResource;
}
/**
* @return void
*/
protected final function setPoolInstance (PoolableClient $poolInstance) {
- $this->poolInstance = $setPoolInstance;
+ $this->poolInstance = $poolInstance;
}
/**
// And add it to this listener
$this->setPoolInstance($poolInstance);
+ // Initialize iterator for listening on packages
+ $iteratorInstance = ObjectFactory::createObjectByConfiguredName('network_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
+
+ // Rewind it and remember it in this class
+ $iteratorInstance->rewind();
+ $this->setIteratorInstance($iteratorInstance);
+
// Output message
$this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
}
* @return void
* @todo 0% done
*/
- public function doListen() {
- $this->partialStub('Need to implement this method.');
+ public function doListen () {
+ // Get all readers
+ $readers = $this->getPoolInstance()->getAllSockets();
+
+ // Check if we have some clients left
+ $left = socket_select($readers, $writers = null, $excepts = null, 0, 150);
+
+ // Some new clients found?
+ if ($left < 1) {
+ // Nothing new found
+ return;
+ } // END - if
+
+ // Do we have changed clients?
+ if (in_array($this->getSocketResource(), $readers)) {
+ // Then accept it
+ $newSocket = socket_accept($this->getSocketResource());
+
+ // Add it to the clients
+ $this->getPoolInstance()->addClient($newSocket);
+ } // END - if
+
+ // Do we have to rewind?
+ if (!$this->getIteratorInstance()->valid()) {
+ // Rewind the list
+ $this->getIteratorInstance()->rewind();
+ } // END - if
+
+ // Get the current value
+ $current = $this->getIteratorInstance()->current();
+
+ // Handle it here
+ $this->partialStub('current['.gettype($current).']='.$current);
+
+ // Advance to next entry. This should be the last line
+ $this->getIteratorInstance()->next();
+ die("OK!\n");
}
}
//* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . '/' . $subGroup . ' - START');
}
+ /**
+ * Gets an array from given list
+ *
+ * @param $list The requested list
+ * @return $array The requested array
+ * @throws NoListGroupException If the given group is not found
+ */
+ public final function getArrayFromList ($list) {
+ // Is the group there?
+ if ((!is_null($list)) && (!$this->isGroupSet($list))) {
+ // Throw the exception here
+ throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
+ } // END - if
+
+ // Init array
+ $array = array();
+
+ // Is there another list?
+ if (!is_null($list)) {
+ // Then get it as well
+ $array = $this->listGroups[$list]->getArrayFromList(null);
+ } // END - if
+
+ // Walk through all entries
+ foreach ($this->listIndex as $hash) {
+ //* DEBUG: */ print __METHOD__.':hash='.$hash."\n";
+ // Is the list entry set?
+ if ($this->isHashValid($hash)) {
+ // Add it
+ $array[] = $this->listEntries[$hash];
+ //* DEBUG: */ print __METHOD__.": ADDED!\n";
+ } // END - if
+ } // END - foreach
+
+ // Return it
+ return $array;
+ }
+
/**
* Adds the given entry to list group
*
* @throws NoListGroupException If the given group is not found
*/
public function addEntry ($groupName, $entry) {
- //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - START');
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
// Is the group already added?
if (!$this->isGroupSet($groupName)) {
// Throw the exception here
// Generate hash
$hash = $this->generateHash($groupName, $groupName, $entry);
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): hash='.$hash.'');
// Add the hash to the index
$this->listIndex[] = $hash;
+ //* DEBUG: */ print $groupName.'/'.count($this->listIndex)."\n";
// Now add the entry to the list
$this->listEntries[$hash] = $entry;
- //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
+ //* DEBUG: */ print $groupName.'/'.count($this->listEntries)."\n";
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
}
/**
// Return the prepared instance
return $listInstance;
}
+
+ /**
+ * Gets an array from this group
+ *
+ * @return $array The array from this group
+ */
+ public final function getArrayFromGroup () {
+ $this->debugInstance();
+ }
}
// [EOF]
$this->poolEntriesInstance = ObjectFactory::createObjectByConfiguredName('pool_entries_list_class');
}
+ /**
+ * 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
*
*/
protected final function addInstance ($group, $poolName, Visitable $instance) {
// Is the pool group there?
- if (!$this->poolEntriesInstance->isGroupSet($group)) {
+ if (!$this->getPoolEntriesInstance()->isGroupSet($group)) {
// Create the missing pool group
- $this->poolEntriesInstance->addGroup($group);
+ $this->getPoolEntriesInstance()->addGroup($group);
} // END - if
// Add it to given pool group
- $this->poolEntriesInstance->addInstance($group, $poolName, $instance);
+ $this->getPoolEntriesInstance()->addInstance($group, $poolName, $instance);
}
/**
* @return void
*/
protected final function addPoolEntry ($poolEntry) {
- $this->poolEntriesInstance->addEntry('generic', $poolEntry);
+ $this->getPoolEntriesInstance()->addEntry('generic', $poolEntry);
}
/**
// Debug message
//* DEBUG: */ $this->debugOutput('POOL: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
}
+
+ /**
+ * Gets the arra 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;
+ }
}
// [EOF]
}
/**
- * Adds a socket resource to the client pool
+ * Validates given socket
*
- * @param $socketResource A valid (must be!) socket resource
+ * @param $socketResource A valid socket resource
* @return void
- * @throws InvalidSocketException If the given resource is invalid or errorous
*/
- public function addClient ($socketResource) {
+ private function validateSocket ($socketResource) {
// Is it a valid resource?
if (!is_resource($socketResource)) {
// Throw an exception
$errorMessage = socket_strerror($errorCode);
// Shutdown this socket
- $this->getListenerInstance()->shutdownSocket($mainSocket);
+ $this->getListenerInstance()->shutdownSocket($socketResource);
// And throw again
throw new InvalidSocketException(array($this, gettype($socketResource), $errorCode, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
} // END - if
+ }
+
+ /**
+ * Adds a socket resource to the client pool
+ *
+ * @param $socketResource A valid (must be!) socket resource
+ * @return void
+ * @throws InvalidSocketException If the given resource is invalid or errorous
+ */
+ public function addClient ($socketResource) {
+ // Validate the socket
+ $this->validateSocket($socketResource);
// Add it finally to the pool
$this->addPoolEntry($socketResource);
}
+
+ /**
+ * Getter for array of all socket resources
+ *
+ * @return $sockets An array with all sockets
+ */
+ public final function getAllSockets () {
+ // Get the list
+ $sockets = $this->getArrayFromList('generic');
+
+ // Return it
+ return $sockets;
+ }
}
//