]> git.mxchange.org Git - hub.git/commitdiff
Package class renamed, continued (sorry for lame description)
authorRoland Häder <roland@mxchange.org>
Mon, 12 Apr 2010 20:25:14 +0000 (20:25 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 12 Apr 2010 20:25:14 +0000 (20:25 +0000)
- Class NetworkPackageFactory was renamed from PackageFactory because the class
  does actually create only network package classes
- HubList class added for handling connected/disconnected classes
- Some tasks (e.g. NetworkPackageWriterTask) continued

22 files changed:
.gitattributes
application/hub/config.php
application/hub/interfaces/package/class_Deliverable.php
application/hub/interfaces/tasks/class_Taskable.php
application/hub/main/factories/package/class_NetworkPackageFactory.php [new file with mode: 0644]
application/hub/main/factories/package/class_PackageFactory.php [deleted file]
application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php
application/hub/main/listener/class_BaseListener.php
application/hub/main/listener/tcp/class_TcpListener.php
application/hub/main/listener/udp/class_UdpListener.php
application/hub/main/lists/hub/.htaccess [new file with mode: 0644]
application/hub/main/lists/hub/class_HubList.php [new file with mode: 0644]
application/hub/main/nodes/class_BaseHubNode.php
application/hub/main/package/class_NetworkPackage.php
application/hub/main/pools/client/class_DefaultClientPool.php
application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php
application/hub/main/tasks/hub/class_HubSelfConnectTask.php
application/hub/main/tasks/hub/ping/class_HubPingTask.php
application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php
application/hub/main/tasks/idle/class_IdleLoopTask.php
application/hub/main/tasks/network/class_NetworkPackageWriterTask.php
application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php

index 84a41d66e22a15f97a95df72e4361dfa85863d14..17550b9881af6c9dd091fad9432a7e6faa54324b 100644 (file)
@@ -199,6 +199,7 @@ application/hub/main/lists/class_ -text
 application/hub/main/lists/class_BaseList.php -text
 application/hub/main/lists/groups/.htaccess -text
 application/hub/main/lists/groups/class_ListGroupList.php -text
+application/hub/main/lists/hub/.htaccess -text
 application/hub/main/lists/pool/.htaccess -text
 application/hub/main/lists/pool/class_PoolEntriesList.php -text
 application/hub/main/lists/query/.htaccess -text
index 99d368310f9fc682316246417fcfc0eeb0a67a4f..261a6f7e49b4f4327b4fb2db2697df6d567f8b31 100644 (file)
@@ -345,5 +345,8 @@ $cfg->setConfigEntry('deco_package_compressor_class', 'NetworkPackageCompressorD
 // CFG: RAW-PACKAGE-COMPRESSOR-CLASS
 $cfg->setConfigEntry('raw_package_compressor_class', 'GzipCompressor');
 
+// CFG: HUB-LIST-CLASS
+$cfg->setConfigEntry('hub_list_class', 'HubList');
+
 // [EOF]
 ?>
index 072bcd765d6cd5e1c412cdcb2c2fa88877b72e42..a3bd1ad73d216227376c6cfd97ae8aa2e9eee117 100644 (file)
@@ -30,6 +30,25 @@ interface Deliverable extends FrameworkInterface {
         * @return      void
         */
        function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance);
+
+       /**
+        * Checks wether a package has been enqueued for delivery.
+        *
+        * @return      $isEnqueued             Wether a package is enqueued
+        */
+       function isPackageEnqueued ();
+
+       /**
+        * Delivers an enqueued package to the stated destination. If none is
+        * provided, the registered helper class is being iterated until no target
+        * is left. This allows that a single package is being delivered to multiple
+        * targets without enqueueing it for every target. If no target is provided
+        * or it can't be determined a NoTargetException is being thrown.
+        *
+        * @return      void
+        * @throws      NoTargetException       If no target can't be determined
+        */
+       function deliverEnqueuedPackage ();
 }
 
 // [EOF]
index 8b99955f51e415eb68c88cfbe1303f294c075534..0386ff1bc1731bfec4867e0b2cde9d9cbde75e45 100644 (file)
@@ -27,7 +27,7 @@ interface Taskable extends FrameworkInterface {
         *
         * @return      void
         */
-       function execute ();
+       function executeTask ();
 }
 
 // [EOF]
diff --git a/application/hub/main/factories/package/class_NetworkPackageFactory.php b/application/hub/main/factories/package/class_NetworkPackageFactory.php
new file mode 100644 (file)
index 0000000..43de8df
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * A factory class for network packages
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub 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 NetworkPackageFactory extends ObjectFactory {
+       /**
+        * Returns a singleton network package instance. If an instance is found in
+        * the registry it will be returned, else a new instance is created and
+        * stored in the same registry entry.
+        *
+        * @return      $packageInstance        A network package instance
+        */
+       public static final function createNetworkPackageInstance () {
+               // Do we have an instance in the registry?
+               if (Registry::getRegistry()->instanceExists('network_package')) {
+                       // Then use this instance
+                       $packageInstance = Registry::getRegistry()->getInstance('network_package');
+               } else {
+                       /**
+                        * Prepare the compressor for our package, GZIP should be fine but we
+                        * keep it open here so you can experiment with the settings and don't
+                        * need to touch any code.
+                        */
+                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('raw_package_compressor_class');
+
+                       // Prepare the decorator compressor (for later flawless and easy updates)
+                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('deco_package_compressor_class', array($compressorInstance));
+
+                       // Now prepare the network package for delivery so only need to do this
+                       // once just before the "big announcement loop".
+                       $packageInstance = ObjectFactory::createObjectByConfiguredName('network_package_class', array($compressorInstance));
+
+                       // Set the instance in registry for further use
+                       Registry::getRegistry()->addInstance('network_package', $packageInstance);
+               }
+
+               // Return the instance
+               return $packageInstance;
+       }
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+}
+?>
diff --git a/application/hub/main/factories/package/class_PackageFactory.php b/application/hub/main/factories/package/class_PackageFactory.php
deleted file mode 100644 (file)
index e0b6d7e..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-<?php
-/**
- * A factory class for packages
- *
- * @author             Roland Haeder <webmaster@ship-simu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub 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 PackageFactory extends ObjectFactory {
-       /**
-        * Returns a singleton network package instance. If an instance is found in
-        * the registry it will be returned, else a new instance is created and
-        * stored in the same registry entry.
-        *
-        * @return      $packageInstance        A network package instance
-        */
-       public static final function createPackageInstance () {
-               // Do we have an instance in the registry?
-               if (Registry::getRegistry()->instanceExists('network_package')) {
-                       // Then use this instance
-                       $packageInstance = Registry::getRegistry()->getInstance('network_package');
-               } else {
-                       /**
-                        * Prepare the compressor for our package, GZIP should be fine but we
-                        * keep it open here so you can experiment with the settings and don't
-                        * need to touch any code.
-                        */
-                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('raw_package_compressor_class');
-
-                       // Prepare the decorator compressor (for later flawless and easy updates)
-                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('deco_package_compressor_class', array($compressorInstance));
-
-                       // Now prepare the network package for delivery so only need to do this
-                       // once just before the "big announcement loop".
-                       $packageInstance = ObjectFactory::createObjectByConfiguredName('network_package_class', array($compressorInstance));
-
-                       // Set the instance in registry for further use
-                       Registry::getRegistry()->addInstance('network_package', $packageInstance);
-               }
-
-               // Return the instance
-               return $packageInstance;
-       }
-
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-       }
-}
-?>
index 7388345d9e83b8a164ac50170e79b47e4cfe552a..bedded4e7b97b5f69dd39a7f6d054efe33736122 100644 (file)
@@ -102,8 +102,8 @@ class HubDescriptorHelper extends BaseHubHelper {
                // Compile the template, this inserts the loaded node data into the gaps.
                $this->getTemplateInstance()->compileTemplate();
 
-               // Get a singleton package instance
-               $packageInstance = PackageFactory::createPackageInstance();
+               // Get a singleton network package instance
+               $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
 
                // Next, feed the content in. The network package class is a pipe-through class.
                $packageInstance->enqueueRawDataFromTemplate($this);
index 6c921f5bb2d2697bae3cfaa0d92eb5077f2a260f..16d79a93a95d4944d9fc303ec6b4c97dd236a043 100644 (file)
@@ -182,7 +182,7 @@ class BaseListener extends BaseHubSystem implements Visitable {
         *
         * @return      $socketResource         The socket resource we shall set
         */
-       protected final function getSocketResource () {
+       public final function getSocketResource () {
                return $this->socketResource;
        }
 
@@ -232,7 +232,7 @@ class BaseListener extends BaseHubSystem implements Visitable {
         */
        public function accept (Visitor $visitorInstance) {
                // Debug message
-               //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited - START');
+               //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
 
                // Visit this listener
                $visitorInstance->visitListener($this);
@@ -243,7 +243,7 @@ class BaseListener extends BaseHubSystem implements Visitable {
                } // END - if
 
                // Debug message
-               //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
+               //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');
        }
 }
 
index 2bfc9ce9334bad2cbea74d801f01cdb68da949d8..b12e631d5d40c300240918ca5b7ba00bf129122b 100644 (file)
@@ -231,7 +231,7 @@ class TcpListener extends BaseListener implements Listenable {
                $current = $this->getIteratorInstance()->current();
 
                // Handle it here, if not main socket
-               if ($current !== $this->getSocketResource()) {
+               if ($current != $this->getSocketResource()) {
                        // ... or else it will raise warnings like 'Transport endpoint is not connected'
                        $this->getPackageInstance()->processResourcePackage($current);
                } // END - if
index 764b6bac28601a70a6c00d51d2823fb914f949bc..866d4c855aed7c3c8adc47a9bffe181189b16191 100644 (file)
@@ -71,6 +71,22 @@ class UdpListener extends BaseListener implements Listenable {
                        throw new InvalidSocketException(array($this, gettype($socket), $errno, $errstr), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
+               // Now, we want non-blocking mode
+               $this->debugOutput('LISTENER: Setting non-blocking mode.');
+               if (!stream_set_blocking($socket, false)) {
+                       // Get socket error code for verification
+                       $socketError = socket_last_error($socket);
+
+                       // Get error message
+                       $errorMessage = socket_strerror($socketError);
+
+                       // Shutdown this socket
+                       $this->shutdownSocket($socket);
+
+                       // And throw again
+                       throw new InvalidSocketException(array($this, gettype($socket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
                // Remember the socket in our class
                $this->setSocketResource($socket);
 
@@ -82,10 +98,20 @@ class UdpListener extends BaseListener implements Listenable {
         * "Listens" for incoming network packages
         *
         * @return      void
-        * @todo        0% done
+        * @todo        ~50% done
         */
        public function doListen() {
-               $this->partialStub('Need to implement this method.');
+               // Read a package and determine the client
+               $pkt = trim(stream_socket_recvfrom($this->getSocketResource(), 1500, 0, $peer));
+
+               // Zero sized packages/peer names are usual in non-blocking mode
+               if ((empty($pkt)) || (trim($peer) == '')) {
+                       // Skip here
+                       return;
+               } // END - if
+
+               // Debug only
+               $this->debugOutput('LISTENER: Handling UDP package with size ' . strlen($pkt) . ' from peer ' . $peer);
        }
 }
 
diff --git a/application/hub/main/lists/hub/.htaccess b/application/hub/main/lists/hub/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/lists/hub/class_HubList.php b/application/hub/main/lists/hub/class_HubList.php
new file mode 100644 (file)
index 0000000..cd7b53c
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * A Hub list
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub 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 HubList extends BaseList implements Listable {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $listInstance   An instance a Listable class
+        */
+       public final static function createHubList () {
+               // Get new instance
+               $listInstance = new HubList();
+
+               // Add groups for e.g. connected/disconnected hubs
+               $listInstance->addGroup('connected');
+               $listInstance->addGroup('disconnected');
+
+               // Return the prepared instance
+               return $listInstance;
+       }
+}
+
+// [EOF]
+?>
index 64c84080a0586f4a69c57e9bfc9462ff5cbb0b4d..0fb71fcdaf6a8ba96e06436091f14e15b9f89a62 100644 (file)
@@ -56,6 +56,11 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
         */
        private $isActive = false;
 
+       /**
+        * List instance of all hubs
+        */
+       private $hubsInstance = false;
+
        /**
         * Protected constructor
         *
@@ -66,6 +71,9 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                // Call parent constructor
                parent::__construct($className);
 
+               // Init list for connected hubs
+               $this->hubsInstance = ObjectFactory::createObjectByConfiguredName('hub_list_class');
+
                // Init state which sets the state to 'init'
                $this->initState();
        }
index 4f6551c27801483fe163cb35f10d8b47a891f63c..11aaeff9fd5542c208ccc7a9ab3c508db5024f5a 100644 (file)
@@ -111,6 +111,33 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                        'callback'  => $helperInstance
                ));
        }
+
+       /**
+        * Checks wether a package has been enqueued for delivery.
+        *
+        * @return      $isEnqueued             Wether a package is enqueued
+        */
+       public function isPackageEnqueued () {
+               // Check wether the stacker is not empty
+               $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
+
+               // Return the result
+               return $isEnqueued;
+       }
+
+       /**
+        * Delivers an enqueued package to the stated destination. If none is
+        * provided, the registered helper class is being iterated until no target
+        * is left. This allows that a single package is being delivered to multiple
+        * targets without enqueueing it for every target. If no target is provided
+        * or it can't be determined a NoTargetException is being thrown.
+        *
+        * @return      void
+        * @throws      NoTargetException       If no target can't be determined
+        */
+       public function deliverEnqueuedPackage () {
+               $this->partialStub('Please implement this method.');
+       }
 }
 
 // [EOF]
index a0ceb977c71301f1177486d4d8d080c61045099b..f8a2338cbab62e44e4fd3e16f6195edcb5bd0f15 100644 (file)
@@ -89,8 +89,26 @@ class DefaultClientPool extends BasePool implements PoolableClient {
                // Validate the socket
                $this->validateSocket($socketResource);
 
+               // Default is this peer's IP
+               $peerName = '0.0.0.0';
+
+               // The socket resource should not match server socket
+               if ($socketResource != $this->getListenerInstance()->getSocketResource()) {
+                       // Try to determine the peer's IP number
+                       if (!socket_getpeername($socketResource, $peerName)) {
+                               // Get last error
+                               $lastError = socket_last_error($socketResource);
+
+                               // Doesn't work!
+                               throw new InvalidSocketException(array($this, gettype($socketResource), $lastError, socket_strerror($lastError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } // END - if
+               } else {
+                       // Server sockets won't work with socket_getpeername()
+                       $this->debugOutput('POOL: Socket resource is server socket. This is no bug.');
+               }
+
                // Output error message
-               $this->debugOutput('POOL: Adding client ' . $socketResource);
+               $this->debugOutput('POOL: Adding client ' . $peerName);
 
                // Add it finally to the pool
                $this->addPoolEntry($socketResource);
index 13218ea7f2efc364ac2e93dceee73dd5dbb8cee1..e123fdf560451d2ffd2b6e56865318825972a8c4 100644 (file)
@@ -62,7 +62,7 @@ class HubSelfAnnouncementTask extends BaseTask implements Taskable, Visitable {
         *
         * @return      void
         */
-       public function execute () {
+       public function executeTask () {
                // Get the node instance and announce us
                Registry::getRegistry()->getInstance('node')->announceSelfToUpperNodes($this);
        }
index 97cb9d4f345a1f1d0aedc7f4237cf8d7359c5c15..624744ed250c610be6c82524b606ee828cac7015 100644 (file)
@@ -62,7 +62,7 @@ class HubSelfConnectTask extends BaseTask implements Taskable, Visitable {
         *
         * @return      void
         */
-       public function execute () {
+       public function executeTask () {
                $this->partialStub('Unimplemented task.');
        }
 }
index 6df69fb6095d50857742c55dbbd6de0f71e7bf79..22ce92d72ff0e4536a7ca030a84e2c6435aa2718 100644 (file)
@@ -72,7 +72,7 @@ class HubPingTask extends BaseTask implements Visitable, Taskable {
         *
         * @return      void
         */
-       public function execute () {
+       public function executeTask () {
                $this->partialStub('Unimplemented task.');
        }
 }
index 5d7c810dacbfd9520e1f68c706fb6d28ac9970f3..e3b5517c9e11647dc93867559397b29a661562e8 100644 (file)
@@ -62,7 +62,7 @@ class HubUpdateCheckTask extends BaseTask implements Visitable, Taskable {
         *
         * @return      void
         */
-       public function execute () {
+       public function executeTask () {
                $this->partialStub('Unimplemented task.');
        }
 }
index 500dcf837f949d643bd5d838af1f7310c204b070..a5328581c957463f0907168126667b7280dfacb7 100644 (file)
@@ -61,7 +61,7 @@ class IdleLoopTask extends BaseTask implements Visitable, Taskable {
         *
         * @return      void
         */
-       public function execute () {
+       public function executeTask () {
                // Idle here a little (2 milliseconds)
                $this->idle(2);
        }
index ebaedf1846cbe81c6f69d72069717270b7db756f..36c926e3a45a0ad3348e2ee8d4465fdc79120eb3 100644 (file)
@@ -50,7 +50,6 @@ class NetworkPackageWriterTask extends BaseTask implements Taskable, Visitable {
         *
         * @param       $visitorInstance        An instance of a Visitor class
         * @return      void
-        * @todo        0%
         */
        public function accept (Visitor $visitorInstance) {
                // Visit this task
@@ -61,9 +60,17 @@ class NetworkPackageWriterTask extends BaseTask implements Taskable, Visitable {
         * Executes the task
         *
         * @return      void
+        * @todo        0%
         */
-       public function execute () {
-               $this->partialStub('Unimplemented task.');
+       public function executeTask () {
+               // Get a singleton network package instance
+               $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
+
+               // Do we have something to deliver?
+               if ($packageInstance->isPackageEnqueued()) {
+                       // Okay, then deliver this package
+                       $packageInstance->deliverEnqueuedPackage();
+               } // END - if
        }
 }
 
index 81717d21af87ac20ab6fb91077fe991619f2b5e3..9b645114af996eb2b91ade489b0e73505a58d766 100644 (file)
@@ -56,7 +56,9 @@ class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnect
         */
        public function visitTask (Taskable $taskInstance) {
                // Execute the task from this visitor
-               $taskInstance->execute();
+               //* DEBUG: */ $this->debugOutput('VISITOR: Visiting task ' . $taskInstance->__toString() . ' - START');
+               $taskInstance->executeTask();
+               //* DEBUG: */ $this->debugOutput('VISITOR: Visiting task ' . $taskInstance->__toString() . ' - FINISHED');
        }
 
        /**
@@ -115,7 +117,7 @@ class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnect
         * @return      void
         */
        public function visitDecorator (BaseDecorator $decoratorInstance) {
-               // A decorator itself can never bevome an active task so this method
+               // A decorator itself can never become an active task so this method
                // remains empty.
        }
 }