From: Roland Häder Date: Sun, 5 Sep 2010 08:48:11 +0000 (+0000) Subject: Now configurable static IPs are supported, Connectable rewritten, docs updated X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4166af060bf75b8fe9c645ceac1151bec0ce1784;p=hub.git Now configurable static IPs are supported, Connectable rewritten, docs updated --- diff --git a/.gitattributes b/.gitattributes index 09963e53d..f5ed204e4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -18,6 +18,8 @@ application/hub/exceptions/lists/class_ListGroupAlreadyAddedException.php -text application/hub/exceptions/lists/class_NoListGroupException.php -text application/hub/exceptions/peer/.htaccess -text application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php -text +application/hub/exceptions/resolver/.htaccess svneol=native#text/plain +application/hub/exceptions/resolver/class_NoValidHostnameException.php svneol=native#text/plain application/hub/exceptions/state/.htaccess -text application/hub/exceptions/state/class_InvalidStateException.php -text application/hub/exceptions/tags/.htaccess -text @@ -77,6 +79,7 @@ application/hub/interfaces/visitor/class_Visitable.php -text application/hub/interfaces/visitor/class_Visitor.php -text application/hub/interfaces/visitor/connector/.htaccess -text application/hub/interfaces/visitor/connector/class_QueryConnectorVisitor.php -text +application/hub/interfaces/visitor/connector/class_QueueConnectorVisitor.php svneol=native#text/plain application/hub/interfaces/visitor/decorator/.htaccess -text application/hub/interfaces/visitor/decorator/class_DecoratorVisitor.php -text application/hub/interfaces/visitor/listener/.htaccess -text @@ -87,6 +90,8 @@ application/hub/interfaces/visitor/pool/listener/.htaccess -text application/hub/interfaces/visitor/pool/listener/class_ListenerPoolVisitor.php -text application/hub/interfaces/visitor/query/.htaccess -text application/hub/interfaces/visitor/query/class_QueryVisitor.php -text +application/hub/interfaces/visitor/queue/.htaccess svneol=native#text/plain +application/hub/interfaces/visitor/queue/class_QueueVisitor.php svneol=native#text/plain application/hub/interfaces/visitor/tasks/.htaccess -text application/hub/interfaces/visitor/tasks/class_TaskVisitor.php -text application/hub/loader.php -text diff --git a/application/hub/config.php b/application/hub/config.php index 2659f0ee9..68a1b4890 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -282,6 +282,15 @@ $cfg->setConfigEntry('task_query_handler_interval_delay', 10); // CFG: TASK-QUERY-HANDLER-MAX-RUNS $cfg->setConfigEntry('task_query_handler_max_runs', 0); +// CFG: TASK-QUEUE-HANDLER-STARTUP-DELAY +$cfg->setConfigEntry('task_queue_handler_startup_delay', 1000); + +// CFG: TASK-QUEUE-HANDLER-INTERVAL-DELAY +$cfg->setConfigEntry('task_queue_handler_interval_delay', 10); + +// CFG: TASK-QUEUE-HANDLER-MAX-RUNS +$cfg->setConfigEntry('task_queue_handler_max_runs', 0); + // CFG: TASK-IDLE-LOOP-STARTUP-DELAY $cfg->setConfigEntry('task_idle_loop_startup_delay', 0); @@ -444,5 +453,8 @@ $cfg->setConfigEntry('peer_state_checked_package_class', 'NewConnectionPeerState // CFG: SESSION-ID $cfg->setConfigEntry('session_id', ''); +// CFG: EXTERNAL-IP +$cfg->setConfigEntry('external_ip', 'free-search.homelinux.org'); + // [EOF] ?> diff --git a/application/hub/exceptions/resolver/.htaccess b/application/hub/exceptions/resolver/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/exceptions/resolver/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/exceptions/resolver/class_NoValidHostnameException.php b/application/hub/exceptions/resolver/class_NoValidHostnameException.php new file mode 100644 index 000000000..a6af1e56e --- /dev/null +++ b/application/hub/exceptions/resolver/class_NoValidHostnameException.php @@ -0,0 +1,45 @@ + + * @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 NoValidHostnameException extends FrameworkException { + /** + * The super constructor for all exceptions + * + * @param $hostName Hostname:port array + * @param $code Error code + * @return void + */ + public function __construct (array $hostName, $code) { + // Construct the message + $message = sprintf("Hostname %s cannot be resolved. Port=%d", + $hostName[0], + $hostName[1] + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/application/hub/interfaces/connectors/class_Connectable.php b/application/hub/interfaces/connectors/class_Connectable.php index bd6e7c4a1..cbce5c9de 100644 --- a/application/hub/interfaces/connectors/class_Connectable.php +++ b/application/hub/interfaces/connectors/class_Connectable.php @@ -23,13 +23,13 @@ */ interface Connectable extends FrameworkInterface { /** - * Handles all pending queries. This method should be called by the - * ActiveTaskVisitor class and should use an iterator on all pending - * queries. + * Handles all pending requests/what ever. This method should be called by + * the ActiveTaskVisitor class and should use an iterator on all pending + * requests/queries/queues. * * @return void */ - function handlePendingQueries (); + function handlePanding (); } // [EOF] diff --git a/application/hub/interfaces/visitor/connector/class_QueueConnectorVisitor.php b/application/hub/interfaces/visitor/connector/class_QueueConnectorVisitor.php new file mode 100644 index 000000000..f4fb4d9d4 --- /dev/null +++ b/application/hub/interfaces/visitor/connector/class_QueueConnectorVisitor.php @@ -0,0 +1,35 @@ + + * @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 . + */ +interface QueueConnectorVisitor extends Visitor { + /** + * QueueConnector visitor method for active queries + * + * @param $connectorInstance A Connectable instance + * @return void + */ + function visitQueueConnector (Connectable $connectorInstance); +} + +// [EOF] +?> diff --git a/application/hub/interfaces/visitor/queue/.htaccess b/application/hub/interfaces/visitor/queue/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/visitor/queue/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/visitor/queue/class_QueueVisitor.php b/application/hub/interfaces/visitor/queue/class_QueueVisitor.php new file mode 100644 index 000000000..10bf7b43d --- /dev/null +++ b/application/hub/interfaces/visitor/queue/class_QueueVisitor.php @@ -0,0 +1,35 @@ + + * @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 . + */ +interface QueueVisitor extends Visitor { + /** + * Queue visitor method for active queues + * + * @param $QueueInstance A Queueable instance + * @return void + */ + function visitQueue (Queueable $queueInstance); +} + +// [EOF] +?> diff --git a/application/hub/main/connectors/query/local/class_LocalQueryConnector.php b/application/hub/main/connectors/query/local/class_LocalQueryConnector.php index a215d3a31..11577ddb5 100644 --- a/application/hub/main/connectors/query/local/class_LocalQueryConnector.php +++ b/application/hub/main/connectors/query/local/class_LocalQueryConnector.php @@ -75,7 +75,7 @@ class LocalQueryConnector extends BaseQueryConnector implements Connectable, Vis * query processor instance * * @return void - * @TODO Unfinished work here + * @TODO 0% done: Unfinished work here */ private function handleCurrentQuery () { // Is there a query available? @@ -112,7 +112,7 @@ class LocalQueryConnector extends BaseQueryConnector implements Connectable, Vis * * @return void */ - public function handlePendingQueries () { + public function handlePanding () { // Should we rewind? if (!$this->getIteratorInstance()->valid()) { // Rewind to the beginning for next loop diff --git a/application/hub/main/connectors/queues/class_ b/application/hub/main/connectors/queues/class_ index 216eaa695..e71fa1e45 100644 --- a/application/hub/main/connectors/queues/class_ +++ b/application/hub/main/connectors/queues/class_ @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ???QueueConnector extends BaseQueueConnector implements Connectable, Queueable { +class ???QueueConnector extends BaseQueueConnector implements Connectable, Queueable, Visitable { /** * Protected constructor * @@ -44,6 +44,17 @@ class ???QueueConnector extends BaseQueueConnector implements Connectable, Queue // Finally return it return $connectorInstance; } + + /** + * Accepts the visitor to process the visit "request" + * + * @param $visitorInstance An instance of a Visitor class + * @return void + */ + public function accept (Visitor $visitorInstance) { + // Visit the query connector + $visitorInstance->visitQueue($this); + } } // [EOF] diff --git a/application/hub/main/connectors/queues/local/class_LocalQueueConnector.php b/application/hub/main/connectors/queues/local/class_LocalQueueConnector.php index 30f4f91e9..c97b4cf6d 100644 --- a/application/hub/main/connectors/queues/local/class_LocalQueueConnector.php +++ b/application/hub/main/connectors/queues/local/class_LocalQueueConnector.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class LocalQueueConnector extends BaseQueueConnector implements Connectable, Queueable { +class LocalQueueConnector extends BaseQueueConnector implements Connectable, Queueable, Visitable { /** * Protected constructor * @@ -49,6 +49,59 @@ class LocalQueueConnector extends BaseQueueConnector implements Connectable, Que // Finally return it return $connectorInstance; } + + /** + * Handles the in the list avaiable queues (current) and hands it over to the + * queues processor instance + * + * @return void + * @TODO 0% done: Unfinished work here + */ + private function handleCurrentQueues () { + // Is there a queues available? + if (!$this->getIteratorInstance()->valid()) { + // Simply abort here + return; + } // END - if + + // Get the current queues + $currentQueues = $this->getIteratorInstance()->current(); + + // Only while construction, else it would output to much! + /* DEBUG: */ $this->debugOutput('CONNECTOR: Handling queue entry ' . $currentQueues); + } + + /** + * Handles all pending queues. This method should be called by the + * ActiveTaskVisitor class and should use an iterator on all pending + * queues. + * + * @return void + */ + public function handlePanding () { + // Should we rewind? + if (!$this->getIteratorInstance()->valid()) { + // Rewind to the beginning for next loop + $this->getIteratorInstance()->rewind(); + } // END - if + + // Try to execute the task + $this->handleCurrentQueue(); + + // Go to next entry + $this->getIteratorInstance()->next(); + } + + /** + * Accepts the visitor to process the visit "request" + * + * @param $visitorInstance An instance of a Visitor class + * @return void + */ + public function accept (Visitor $visitorInstance) { + // Visit the query connector + $visitorInstance->visitQueue($this); + } } // [EOF] diff --git a/application/hub/main/database/wrapper/class_PeerStateLookupDatabaseWrapper.php b/application/hub/main/database/wrapper/class_PeerStateLookupDatabaseWrapper.php index d1f44cea0..03fe0217e 100644 --- a/application/hub/main/database/wrapper/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/class_PeerStateLookupDatabaseWrapper.php @@ -78,6 +78,12 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper { // Is always new peer by default $isNewPeer = true; + // Is the package valid? + if (!isset($packageData[NetworkPackage::INDEX_PACKAGE_SENDER])) { + // Invalid package found, please report this + die('packageData='.print_r($packageData, true)); + } // END - if + // Remove session id > IP:port $ipPort = HubTools::resolveSessionId($packageData[NetworkPackage::INDEX_PACKAGE_SENDER]); diff --git a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php index d2dfcb6d2..c0d256556 100644 --- a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php +++ b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php @@ -71,8 +71,14 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable // The target 'self' is always the external IP address! case NetworkPackage::NETWORK_TARGET_SELF: - // Determine own external ip by connecting to my (coder) server at 217.172.186.31 - $ip = ConsoleTools::determineExternalIp(); + // Is the external_ip config entry set? + if ($this->getConfigInstance()->getConfigEntry('external_ip') != '') { + // Use it as external ip + $ip = $this->getConfigInstance()->getConfigEntry('external_ip'); + } else { + // Determine own external ip by connecting to my (coder) server at 217.172.186.31 + $ip = ConsoleTools::determineExternalIp(); + } // Get mode $mode = Registry::getRegistry()->getInstance('app')->getRequestInstance()->getRequestElement('mode'); diff --git a/application/hub/main/filter/task/class_TaskHandlerInitializerFilter.php b/application/hub/main/filter/task/class_TaskHandlerInitializerFilter.php index 4a2dff856..3310eca64 100644 --- a/application/hub/main/filter/task/class_TaskHandlerInitializerFilter.php +++ b/application/hub/main/filter/task/class_TaskHandlerInitializerFilter.php @@ -79,6 +79,9 @@ class TaskHandlerInitializerFilter extends BaseFilter implements Filterable { // Query handler instance $handlerInstance->registerTask('query_handler', $nodeInstance->getQueryConnectorInstance()); + // Queue handler instance + $handlerInstance->registerTask('queue_handler', $nodeInstance->getQueueConnectorInstance()); + // Put the task handler in registry Registry::getRegistry()->addInstance('task', $handlerInstance); diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index b83b93c97..469cc87ff 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -33,7 +33,12 @@ class BaseHubNode extends BaseHubSystem implements Updateable { /** * Query connector instance */ - private $connectorInstance = null; + private $queryConnectorInstance = null; + + /** + * Queue connector instance + */ + private $queueConnectorInstance = null; /** * Listener pool instance @@ -166,7 +171,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { * @return void */ private final function setQueryConnectorInstance (Connectable $connectorInstance) { - $this->connectorInstance = $connectorInstance; + $this->queryConnectorInstance = $connectorInstance; } /** @@ -175,7 +180,26 @@ class BaseHubNode extends BaseHubSystem implements Updateable { * @return $connectorInstance Our new query instance */ public final function getQueryConnectorInstance () { - return $this->connectorInstance; + return $this->queryConnectorInstance; + } + + /** + * Setter for queue instance + * + * @param $connectorInstance Our new queue instance + * @return void + */ + private final function setQueueConnectorInstance (Connectable $connectorInstance) { + $this->queueConnectorInstance = $connectorInstance; + } + + /** + * Getter for queue instance + * + * @return $connectorInstance Our new queue instance + */ + public final function getQueueConnectorInstance () { + return $this->queueConnectorInstance; } /** @@ -377,6 +401,12 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Run a test query $this->getQueryConnectorInstance()->doTestQuery(); + // Set the queue connector instance + $this->setQueueConnectorInstance(ObjectFactory::createObjectByConfiguredName('queue_connector_class', array($this))); + + // Run a test queue + $this->getQueueConnectorInstance()->doTestQueue(); + // Debug message $this->debugOutput('BOOTSTRAP: Initialize queues: FINISHED'); } diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index 4525f503e..bf11d0097 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -24,6 +24,7 @@ class HubTools extends BaseFrameworkSystem { // Constants for exceptions const EXCEPTION_SESSION_ID_IS_INVALID = 0x200; + const EXCEPTION_HOSTNAME_NOT_FOUND = 0x201; /** * Cache for session ids @@ -101,6 +102,7 @@ class HubTools extends BaseFrameworkSystem { * @param $sessionId Session id or ip:port combination * @return $recipient Recipient as ip:port combination * @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination) + * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address */ public static function resolveSessionId ($sessionId) { // Get an own instance @@ -118,6 +120,19 @@ class HubTools extends BaseFrameworkSystem { // Then use it $recipient = $selfInstance->sessionIdCache[$sessionId]; + } elseif (preg_match('/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}/', $sessionId)) { + // Hostname:port found + $hostnameArray = explode(':', $sessionId); + + // Try to resolve it and add port again + // @TODO We may want to encapsulate this PHP call into an own class + $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1]; + + // Is it valid? + if ($recipient == $sessionId) { + // Resolving hostname->IP failed! + throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND); + } // END - if } elseif (!preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) { // Invalid session id throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID); diff --git a/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php index 9b645114a..f38b600d2 100644 --- a/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php +++ b/application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnectorVisitor, PoolVisitor, ListenerVisitor, DecoratorVisitor { +class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnectorVisitor, QueueConnectorVisitor, PoolVisitor, ListenerVisitor, DecoratorVisitor { /** * Protected constructor * @@ -69,7 +69,18 @@ class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnect */ public function visitQueryConnector (Connectable $connectorInstance) { // Process all pending queries - $connectorInstance->handlePendingQueries(); + $connectorInstance->handlePanding(); + } + + /** + * Connector visitor method for active tasks + * + * @param $connectorInstance A Connectable instance + * @return void + */ + public function visitQueueConnector (Connectable $connectorInstance) { + // Process all pending queries + $connectorInstance->handlePanding(); } /** @@ -97,6 +108,17 @@ class ActiveTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnect // Empty for now... } + /** + * Queue visitor method for active queries + * + * @param $queueInstance A Queueable instance + * @return void + * @todo Does a queue needs to perform some actions as an active task? + */ + public function visitQueue (Queueable $queueInstance) { + // Empty for now... + } + /** * Visits the given listener instance * diff --git a/application/hub/main/visitor/tasks/class_ShutdownTaskVisitor.php b/application/hub/main/visitor/tasks/class_ShutdownTaskVisitor.php index cf1753f49..92ef09c87 100644 --- a/application/hub/main/visitor/tasks/class_ShutdownTaskVisitor.php +++ b/application/hub/main/visitor/tasks/class_ShutdownTaskVisitor.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ShutdownTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnectorVisitor, PoolVisitor, ListenerVisitor, DecoratorVisitor { +class ShutdownTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConnectorVisitor, QueueConnectorVisitor, PoolVisitor, ListenerVisitor, DecoratorVisitor { /** * Protected constructor * @@ -70,6 +70,17 @@ class ShutdownTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConne $connectorInstance->doShutdown(); } + /** + * Connector visitor method for active tasks + * + * @param $connectorInstance A Connectable instance + * @return void + */ + public function visitQueueConnector (Connectable $connectorInstance) { + // Shutdown the connector instance + $connectorInstance->doShutdown(); + } + /** * Pool visitor method for active tasks * @@ -93,6 +104,18 @@ class ShutdownTaskVisitor extends BaseVisitor implements TaskVisitor, QueryConne $queryInstance->doShutdown(); } + /** + * Queue visitor method for active queries + * + * @param $queueInstance A Queueable instance + * @return void + * @todo Does a queue needs to perform some actions as an active task? + */ + public function visitQueue (Queueable $queueInstance) { + // Shutdown the queue instance + $queueInstance->doShutdown(); + } + /** * Visits the given listener instance * diff --git a/docs/README b/docs/README index a2313e883..fbc106758 100644 --- a/docs/README +++ b/docs/README @@ -6,8 +6,8 @@ README -------------------------------------------------------------------------------- At the current stage of code base you need to launch the hub by entering the -following command at at command prompt (Win32) or console (Linux etc.) in the -root directory of this script: +following command at a command prompt (Win32) or console (Linux, etc.) in the +root directory: php index.php app=hub