From e7a89b8178869e2f3cbd357c421c3d1ec903fa18 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 5 Aug 2009 16:53:56 +0000 Subject: [PATCH] Iterator continued (not fully implemented), iteration on all clients and hubs should now work --- .gitattributes | 2 + .../lists/class_InvalidListHashException.php | 46 ++++++++++++++ .../pool/class_ShutdownPoolIterator.php | 37 +++++++---- .../hub/main/listener/class_BaseListener.php | 6 +- application/hub/main/lists/class_BaseList.php | 63 +++++++++++++++++-- db/node_list/.htaccess | 1 + 6 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 application/hub/exceptions/lists/class_InvalidListHashException.php create mode 100644 db/node_list/.htaccess diff --git a/.gitattributes b/.gitattributes index 13b2fc17b..fecd077c5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,6 +9,7 @@ application/hub/debug.php -text application/hub/exceptions.php -text application/hub/exceptions/.htaccess -text application/hub/exceptions/lists/.htaccess -text +application/hub/exceptions/lists/class_InvalidListHashException.php -text application/hub/exceptions/lists/class_ListGroupAlreadyAddedException.php -text application/hub/exceptions/lists/class_NoListGroupException.php -text application/hub/init.php -text @@ -168,6 +169,7 @@ application/hub/starter.php -text db/.htaccess -text db/news/.htaccess -text db/node_data/.htaccess -text +db/node_list/.htaccess -text docs/COPYING -text docs/COPYING.documents -text docs/COPYING.software -text diff --git a/application/hub/exceptions/lists/class_InvalidListHashException.php b/application/hub/exceptions/lists/class_InvalidListHashException.php new file mode 100644 index 000000000..faf6bb01f --- /dev/null +++ b/application/hub/exceptions/lists/class_InvalidListHashException.php @@ -0,0 +1,46 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * 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 . + */ +class InvalidListHashException extends FrameworkException { + /** + * The super constructor for all exceptions + * + * @param $messageArray Error message array + * @param $code Error code + * @return void + */ + public function __construct (array $messageArray, $code) { + // Construct the message + $message = sprintf("[%s] Hash %s with key %s is invalid.", + $messageArray[0]->__toString(), + $messageArray[1], + $messageArray[2] + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php b/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php index d6a16f562..1fbc0f190 100644 --- a/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php +++ b/application/hub/main/iterator/pool/class_ShutdownPoolIterator.php @@ -22,6 +22,11 @@ * along with this program. If not, see . */ class ShutdownPoolIterator extends BaseIterator implements Iterator { + /** + * Key for the global list index + */ + private $indexKey = 0; + /** * Protected constructor * @@ -53,12 +58,20 @@ class ShutdownPoolIterator extends BaseIterator implements Iterator { * 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; - $this->partialStub('Please implement this method.'); + // 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; @@ -67,16 +80,10 @@ class ShutdownPoolIterator extends BaseIterator implements Iterator { /** * Getter for key from group or generic * - * @return $key Current key in iteration + * @return $indexKey Current key in iteration */ public function key () { - // Default is null - $key = null; - - $this->partialStub('Please implement this method.'); - - // Return it - return $key; + return $this->indexKey; } /** @@ -85,7 +92,7 @@ class ShutdownPoolIterator extends BaseIterator implements Iterator { * @return void */ public function next () { - $this->partialStub('Please implement this method.'); + $this->indexKey++; } /** @@ -94,16 +101,20 @@ class ShutdownPoolIterator extends BaseIterator implements Iterator { * @return void */ public function rewind () { - $this->partialStub('Please implement this method.'); + $this->indexKey = 0; } /** * Checks wether the current entry is valid (not at the end of the list) * - * @return void + * @return $isValid Wether the current entry is there */ public function valid () { - $this->partialStub('Please implement this method.'); + // Check for total active clients and if we are not at the end + $isValid = ($this->key() < $this->getListInstance()->count()); + + // Return result + return $isValid; } } diff --git a/application/hub/main/listener/class_BaseListener.php b/application/hub/main/listener/class_BaseListener.php index 0fce415d1..32dbe2c7f 100644 --- a/application/hub/main/listener/class_BaseListener.php +++ b/application/hub/main/listener/class_BaseListener.php @@ -213,8 +213,10 @@ class BaseListener extends BaseHubSystem implements Visitable { // Visit this listener $visitorInstance->visitListener($this); - // Visit the pool - $this->getPoolInstance()->accept($visitor); + // Visit the pool if set + if ($this->getPoolInstance() instanceof Poolable) { + $this->getPoolInstance()->accept($visitor); + } // END - if // Debug message $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); diff --git a/application/hub/main/lists/class_BaseList.php b/application/hub/main/lists/class_BaseList.php index c81063933..fc6969ae6 100644 --- a/application/hub/main/lists/class_BaseList.php +++ b/application/hub/main/lists/class_BaseList.php @@ -21,10 +21,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseList extends BaseHubSystem implements IteratorAggregate { +class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { // Exception constants const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20; const EXCEPTION_GROUP_NOT_FOUND = 0xf21; + const EXCEPTION_INVALID_HASH = 0xf22; /** * List groups array @@ -182,12 +183,15 @@ class BaseList extends BaseHubSystem implements IteratorAggregate { } elseif ($entry instanceof FrameworkInterface) { // Own instance detected $entry2 = $entry->hashCode(); - } elseif (!is_array($entry)) { - // Non-array found, use it directly with type - $entry2 = gettype($entry) . ':' . $entry2; + } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) { + // Integer/float/resource detected + $entry2 = gettype($entry) . ':' . $entry; + } elseif (is_string($entry)) { + // String found + $entry2 = crc32($entry) . ':' . strlen($entry); } else { - // Arrays are unsupported at the momement - $this->debugOutut(__METHOD__ . ': entry is an array. UNSUPPORTED!'); + // Unsupported type detected + $this->debugOutut(__METHOD__ . ': entry type ' . gettype($entry) . ' is unsupported.'); // @TODO Extend this somehow? $entry2 = gettype($entry); @@ -202,6 +206,53 @@ class BaseList extends BaseHubSystem implements IteratorAggregate { // And return it return $hash; } + + /** + * Counts all entries in this list + * + * @return $count All entries in this list + */ + public final function count () { + return count($this->listIndex); + } + + /** + * Checks wether the given hash is valid + * + * @param $hash The hash we should validate + * @return $isValid Wether the given hash is valid + */ + public final function isHashValid ($hash) { + // Check it + $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash]))); + + // Return the result + return $isValid; + } + + /** + * Gets an entry from given hash index + * + * @param $hashIndex The hash index to resolve the mapped entry + * @return $entry Solved entry from list + * @throws InvalidListHashException If the solved hash index is invalid + */ + public function getEntry ($hashIndex) { + // Get the hash value + $hash = $this->listIndex[$hashIndex]; + + // Is the hash valid? + if (!$this->isHashValid($hash)) { + // Throw an exception here + throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH); + } // END - if + + // Now copy the entry + $entry = $this->listEntries[$hash]; + + // Return it + return $entry; + } } // diff --git a/db/node_list/.htaccess b/db/node_list/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/db/node_list/.htaccess @@ -0,0 +1 @@ +Deny from all -- 2.39.2