Iterator continued (not fully implemented), iteration on all clients and hubs should...
authorRoland Häder <roland@mxchange.org>
Wed, 5 Aug 2009 16:53:56 +0000 (16:53 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 5 Aug 2009 16:53:56 +0000 (16:53 +0000)
.gitattributes
application/hub/exceptions/lists/class_InvalidListHashException.php [new file with mode: 0644]
application/hub/main/iterator/pool/class_ShutdownPoolIterator.php
application/hub/main/listener/class_BaseListener.php
application/hub/main/lists/class_BaseList.php
db/node_list/.htaccess [new file with mode: 0644]

index 13b2fc17b86db5e8db85606cf108fe70a0f34222..fecd077c53a25148ab14988d811b38a82182c4ff 100644 (file)
@@ -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 (file)
index 0000000..faf6bb0
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * This exception is thrown when a hash is invalid
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 <span class=\"exception_reason\">%s</span> with key <span class=\"exception_reason\">%s</span> is invalid.",
+                       $messageArray[0]->__toString(),
+                       $messageArray[1],
+                       $messageArray[2]
+               );
+
+               // Call parent exception constructor
+               parent::__construct($message, $code);
+       }
+}
+
+// [EOF]
+?>
index d6a16f562b8a7b1c013cab3d91c14762a17fffe3..1fbc0f19027187cbcbc3991e601e911527384cb5 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 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;
        }
 }
 
index 0fce415d1de27246053ca13ea3582883f8cbffdf..32dbe2c7f2aa95c6aba1cb5b9364c4a6ac50980d 100644 (file)
@@ -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');
index c81063933225d54c66bcce029fff4aa6e07c39f4..fc6969ae6943d100a368f9ddb57a6c9d81705c74 100644 (file)
  * 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 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 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all