From 3354cef4df30f6b239c92ec1bda67e5125ae5849 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 12 Apr 2010 20:25:14 +0000 Subject: [PATCH] Package class renamed, continued (sorry for lame description) - 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 --- .gitattributes | 1 + application/hub/config.php | 3 ++ .../interfaces/package/class_Deliverable.php | 19 +++++++ .../hub/interfaces/tasks/class_Taskable.php | 2 +- ...ry.php => class_NetworkPackageFactory.php} | 6 +-- .../class_HubDescriptorHelper.php | 4 +- .../hub/main/listener/class_BaseListener.php | 6 +-- .../main/listener/tcp/class_TcpListener.php | 2 +- .../main/listener/udp/class_UdpListener.php | 30 ++++++++++- application/hub/main/lists/hub/.htaccess | 1 + .../hub/main/lists/hub/class_HubList.php | 54 +++++++++++++++++++ .../hub/main/nodes/class_BaseHubNode.php | 8 +++ .../hub/main/package/class_NetworkPackage.php | 27 ++++++++++ .../pools/client/class_DefaultClientPool.php | 20 ++++++- .../class_HubSelfAnnouncementTask.php | 2 +- .../tasks/hub/class_HubSelfConnectTask.php | 2 +- .../main/tasks/hub/ping/class_HubPingTask.php | 2 +- .../hub/update/class_HubUpdateCheckTask.php | 2 +- .../main/tasks/idle/class_IdleLoopTask.php | 2 +- .../class_NetworkPackageWriterTask.php | 13 +++-- .../visitor/tasks/class_ActiveTaskVisitor.php | 6 ++- 21 files changed, 189 insertions(+), 23 deletions(-) rename application/hub/main/factories/package/{class_PackageFactory.php => class_NetworkPackageFactory.php} (94%) create mode 100644 application/hub/main/lists/hub/.htaccess create mode 100644 application/hub/main/lists/hub/class_HubList.php diff --git a/.gitattributes b/.gitattributes index 84a41d66e..17550b988 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/application/hub/config.php b/application/hub/config.php index 99d368310..261a6f7e4 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -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] ?> diff --git a/application/hub/interfaces/package/class_Deliverable.php b/application/hub/interfaces/package/class_Deliverable.php index 072bcd765..a3bd1ad73 100644 --- a/application/hub/interfaces/package/class_Deliverable.php +++ b/application/hub/interfaces/package/class_Deliverable.php @@ -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] diff --git a/application/hub/interfaces/tasks/class_Taskable.php b/application/hub/interfaces/tasks/class_Taskable.php index 8b99955f5..0386ff1bc 100644 --- a/application/hub/interfaces/tasks/class_Taskable.php +++ b/application/hub/interfaces/tasks/class_Taskable.php @@ -27,7 +27,7 @@ interface Taskable extends FrameworkInterface { * * @return void */ - function execute (); + function executeTask (); } // [EOF] diff --git a/application/hub/main/factories/package/class_PackageFactory.php b/application/hub/main/factories/package/class_NetworkPackageFactory.php similarity index 94% rename from application/hub/main/factories/package/class_PackageFactory.php rename to application/hub/main/factories/package/class_NetworkPackageFactory.php index e0b6d7e41..43de8df49 100644 --- a/application/hub/main/factories/package/class_PackageFactory.php +++ b/application/hub/main/factories/package/class_NetworkPackageFactory.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class PackageFactory extends ObjectFactory { +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 @@ -29,7 +29,7 @@ class PackageFactory extends ObjectFactory { * * @return $packageInstance A network package instance */ - public static final function createPackageInstance () { + public static final function createNetworkPackageInstance () { // Do we have an instance in the registry? if (Registry::getRegistry()->instanceExists('network_package')) { // Then use this instance diff --git a/application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php b/application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php index 7388345d9..bedded4e7 100644 --- a/application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php +++ b/application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php @@ -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); diff --git a/application/hub/main/listener/class_BaseListener.php b/application/hub/main/listener/class_BaseListener.php index 6c921f5bb..16d79a93a 100644 --- a/application/hub/main/listener/class_BaseListener.php +++ b/application/hub/main/listener/class_BaseListener.php @@ -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'); } } diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 2bfc9ce93..b12e631d5 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -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 diff --git a/application/hub/main/listener/udp/class_UdpListener.php b/application/hub/main/listener/udp/class_UdpListener.php index 764b6bac2..866d4c855 100644 --- a/application/hub/main/listener/udp/class_UdpListener.php +++ b/application/hub/main/listener/udp/class_UdpListener.php @@ -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 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/lists/hub/.htaccess @@ -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 index 000000000..cd7b53cee --- /dev/null +++ b/application/hub/main/lists/hub/class_HubList.php @@ -0,0 +1,54 @@ + + * @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 . + */ +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] +?> diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index 64c84080a..0fb71fcda 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -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(); } diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 4f6551c27..11aaeff9f 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -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] diff --git a/application/hub/main/pools/client/class_DefaultClientPool.php b/application/hub/main/pools/client/class_DefaultClientPool.php index a0ceb977c..f8a2338cb 100644 --- a/application/hub/main/pools/client/class_DefaultClientPool.php +++ b/application/hub/main/pools/client/class_DefaultClientPool.php @@ -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); diff --git a/application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php b/application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php index 13218ea7f..e123fdf56 100644 --- a/application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php +++ b/application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php @@ -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); } diff --git a/application/hub/main/tasks/hub/class_HubSelfConnectTask.php b/application/hub/main/tasks/hub/class_HubSelfConnectTask.php index 97cb9d4f3..624744ed2 100644 --- a/application/hub/main/tasks/hub/class_HubSelfConnectTask.php +++ b/application/hub/main/tasks/hub/class_HubSelfConnectTask.php @@ -62,7 +62,7 @@ class HubSelfConnectTask extends BaseTask implements Taskable, Visitable { * * @return void */ - public function execute () { + public function executeTask () { $this->partialStub('Unimplemented task.'); } } diff --git a/application/hub/main/tasks/hub/ping/class_HubPingTask.php b/application/hub/main/tasks/hub/ping/class_HubPingTask.php index 6df69fb60..22ce92d72 100644 --- a/application/hub/main/tasks/hub/ping/class_HubPingTask.php +++ b/application/hub/main/tasks/hub/ping/class_HubPingTask.php @@ -72,7 +72,7 @@ class HubPingTask extends BaseTask implements Visitable, Taskable { * * @return void */ - public function execute () { + public function executeTask () { $this->partialStub('Unimplemented task.'); } } diff --git a/application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php b/application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php index 5d7c810da..e3b5517c9 100644 --- a/application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php +++ b/application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php @@ -62,7 +62,7 @@ class HubUpdateCheckTask extends BaseTask implements Visitable, Taskable { * * @return void */ - public function execute () { + public function executeTask () { $this->partialStub('Unimplemented task.'); } } diff --git a/application/hub/main/tasks/idle/class_IdleLoopTask.php b/application/hub/main/tasks/idle/class_IdleLoopTask.php index 500dcf837..a5328581c 100644 --- a/application/hub/main/tasks/idle/class_IdleLoopTask.php +++ b/application/hub/main/tasks/idle/class_IdleLoopTask.php @@ -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); } diff --git a/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php b/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php index ebaedf184..36c926e3a 100644 --- a/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php +++ b/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php @@ -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 } } diff --git a/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php index 81717d21a..9b645114a 100644 --- a/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php +++ b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php @@ -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. } } -- 2.39.2