From: Roland Häder Date: Tue, 3 Feb 2009 21:25:15 +0000 (+0000) Subject: Hub software cleared (prepared for full rewrite) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e1202beb4aca6aca33bd821a3badc715a57aeb27;p=hub.git Hub software cleared (prepared for full rewrite) --- diff --git a/.gitattributes b/.gitattributes index e2949f98f..71e36d256 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,37 +8,10 @@ application/hub/debug.php -text application/hub/exceptions.php -text application/hub/exceptions/.htaccess -text application/hub/exceptions/class_HubException.php -text -application/hub/exceptions/client/.htaccess -text -application/hub/exceptions/client/class_HubHelloException.php -text -application/hub/exceptions/client/class_HubPeerAuthorizationException.php -text -application/hub/exceptions/client/class_HubPeerTimeoutException.php -text -application/hub/exceptions/commands/.htaccess -text -application/hub/exceptions/commands/class_UnexpectedAwaitCommandException.php -text -application/hub/exceptions/master/.htaccess -text -application/hub/exceptions/master/class_HubMasterDisconnectedException.php -text -application/hub/exceptions/socket/.htaccess -text -application/hub/exceptions/socket/class_SocketException.php -text -application/hub/exceptions/socket/connector/.htaccess -text -application/hub/exceptions/socket/connector/class_SocketAcceptException.php -text -application/hub/exceptions/socket/connector/class_SocketSelectorException.php -text -application/hub/exceptions/socket/init/.htaccess -text -application/hub/exceptions/socket/init/class_SocketBindException.php -text -application/hub/exceptions/socket/init/class_SocketConnectException.php -text -application/hub/exceptions/socket/init/class_SocketCreationException.php -text -application/hub/exceptions/socket/init/class_SocketListeningException.php -text -application/hub/exceptions/socket/init/class_SocketSetupException.php -text -application/hub/exceptions/socket/peer/.htaccess -text -application/hub/exceptions/socket/peer/class_BrokenPipeException.php -text -application/hub/exceptions/socket/peer/class_IPSpoofingException.php -text -application/hub/exceptions/socket/peer/class_PeerSocketException.php -text application/hub/init.php -text application/hub/interfaces/.htaccess -text application/hub/loader.php -text application/hub/main/.htaccess -text -application/hub/main/class_HubCommandProcessor.php -text -application/hub/main/class_HubConnector.php -text -application/hub/main/class_HubCoreLoop.php -text -application/hub/main/class_HubPeer.php -text application/hub/middleware/.htaccess -text application/hub/starter.php -text /chat-server.php -text diff --git a/application/hub/class_ApplicationHelper.php b/application/hub/class_ApplicationHelper.php index 24aab7c80..665b5489c 100644 --- a/application/hub/class_ApplicationHelper.php +++ b/application/hub/class_ApplicationHelper.php @@ -76,7 +76,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica /** * Getter for an instance of this class * - * @return $thisInstance An instance of this class + * @return $thisInstance An instance of this class */ public final static function getInstance () { // Is the instance there? @@ -99,13 +99,12 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica /** * Setter for the version number * - * @param $appVersion The application's version number + * @param $appVersion The application's version number * @return void */ public final function setAppVersion ($appVersion) { // Cast and set it - $appVersion = (string) $appVersion; - $this->appVersion = $appVersion; + $this->appVersion = (string) $appVersion; } /** @@ -120,13 +119,12 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica /** * Setter for human-readable name * - * @param $appName The application's human-readable name + * @param $appName The application's human-readable name * @return void */ public final function setAppName ($appName) { // Cast and set it - $appName = (string) $appName; - $this->appName = $appName; + $this->appName = (string) $appName;; } /** @@ -141,13 +139,12 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica /** * Setter for short uni*-like name * - * @param $shortName The application's short uni*-like name + * @param $shortName The application's short uni*-like name * @return void */ public final function setAppShortName ($shortName) { // Cast and set it - $shortName = (string) $shortName; - $this->shortName = $shortName; + $this->shortName = (string) $shortName; } /** @@ -156,29 +153,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica * @return void */ public final function entryPoint () { - // Get a core loop instance - $hubInstance = HubCoreLoop::createHubCoreLoop(); - - // Output some text - $hubInstance->outputIntro(); - - // Contact the master hub - $hubInstance->contactMasterHub(); - - // The main loop begins here - $hubInstance->coreLoop(); - - // Shutdown the hub - $hubInstance->shutdownHub(); - } - - /** - * Getter for master template name - * - * @return $masterTemplate Name of the master template - */ - public final function getMasterTemplate () { - return "hub_main"; + trigger_error(__METHOD__.": Cleared!"); } /** @@ -194,6 +169,15 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica die("MSG:".$message); } } + + /** + * Builds the master template's name + * + * @return $masterTemplateName Name of the master template + */ + public function buildMasterTemplateName () { + return "hub_main"; + } } // [EOF] diff --git a/application/hub/config.php b/application/hub/config.php index e5ca83a15..c8da2e424 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -42,62 +42,5 @@ $cfg->setConfigEntry("debug_engine", "DebugErrorLogOutput"); // CFG: WEB-CONTENT-TYPE $cfg->setConfigEntry("web_content_type", ""); -// CFG: HUB-INTRO-ENABLED -$cfg->setConfigEntry("hub_intro_enabled", "Y"); - -// CFG: HUB-MAX-AUTH-TRIES -$cfg->setConfigEntry("hub_max_auth_tries", 3); - -// CFG: HUB-MSG-AUTH-TRIES -$cfg->setConfigEntry("hub_msg_auth_tries", "AUTH_MAX_TRIES"); - -// CFG: HUB-MSG-SPOOFING -$cfg->setConfigEntry("hub_msg_spoofing", "SPOOFING"); - -// CFG: HUB-MSG-AUTH-REPLY -$cfg->setConfigEntry("hub_msg_auth_reply_timeout", "TIMEOUT_AUTH"); - -// CFG: HUB-MSG-BYE -$cfg->setConfigEntry("hub_msg_bye", "BYE"); - -// CFG: HUB-MASTER-IP -$cfg->setConfigEntry("hub_master_ip", "192.168.1.17"); - -// CFG: HUB-MASTER-PORT -$cfg->setConfigEntry("hub_master_port", 9060); - -// CFG: HUB-AUTH-REQUEST -$cfg->setConfigEntry("hub_auth_request", "AUTH"); - -// CFG: HUB-AUTH-REQUEST-TIMEOUT (5 seconds for whole auth procedure shall be fine) -$cfg->setConfigEntry("hub_auth_request_timeout", 5); - -// CFG: HUB-PEER-HELLO -$cfg->setConfigEntry("hub_peer_hello", "HELLO"); - -// CFG: HUB-HELLO-REPLY -$cfg->setConfigEntry("hub_hello_reply", "ELHO"); - -// CFG: HUB-HELLO-TIMEOUT -$cfg->setConfigEntry("hub_hello_timeout", 30); - -// CFG: HUB-HELLO-RETRIES -$cfg->setConfigEntry("hub_hello_retires", 3); - -// CFG: HUB-PEER-PING -$cfg->setConfigEntry("hub_peer_ping", "PING"); - -// CFG: HUB-PING-REPLY -$cfg->setConfigEntry("hub_ping_reply", "PONG"); - -// CFG: HUB-PING-TIMEOUT -$cfg->setConfigEntry("hub_ping_timeout", 10); - -// CFG: HUB-PING-MAXDROPS -$cfg->setConfigEntry("hub_ping_maxdrops", 3); - -// CFG: HUB-PEER-MISS-PONG -$cfg->setConfigEntry("hub_peer_miss_pong", "PONG_MISS"); - // [EOF] ?> diff --git a/application/hub/exceptions/client/.htaccess b/application/hub/exceptions/client/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/client/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/client/class_HubHelloException.php b/application/hub/exceptions/client/class_HubHelloException.php deleted file mode 100644 index 505cae52e..000000000 --- a/application/hub/exceptions/client/class_HubHelloException.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubHelloException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Peer %s has not replyed our %s request within %d retries.
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['peer']->getValidatedIP(), - $msgArray['this']->getConfigInstance()->readConfig("hub_peer_hello"), - $msgArray['this']->getConfigInstance()->readConfig("hub_hello_retires") - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/client/class_HubPeerAuthorizationException.php b/application/hub/exceptions/client/class_HubPeerAuthorizationException.php deleted file mode 100644 index 3df17bea9..000000000 --- a/application/hub/exceptions/client/class_HubPeerAuthorizationException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubPeerAuthorizationException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Peer %s has reached the maximum of %d authorization tries. Disconnecting peer....
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['peer']->getValidatedIP(), - $msgArray['max'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/client/class_HubPeerTimeoutException.php b/application/hub/exceptions/client/class_HubPeerTimeoutException.php deleted file mode 100644 index 312ea50df..000000000 --- a/application/hub/exceptions/client/class_HubPeerTimeoutException.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubPeerTimeoutException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Peer %s is no longer responding. Trying to disconnect the peer...
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['peer']->getValidatedIP() - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/commands/.htaccess b/application/hub/exceptions/commands/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/commands/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/commands/class_UnexpectedAwaitCommandException.php b/application/hub/exceptions/commands/class_UnexpectedAwaitCommandException.php deleted file mode 100644 index bbde25aa2..000000000 --- a/application/hub/exceptions/commands/class_UnexpectedAwaitCommandException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 UnexpectedAwaitCommandException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Not awaiting command %s from peer %s
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['await'], - $msgArray['this']->getPeerInstance()->getValidatedIP() - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/master/.htaccess b/application/hub/exceptions/master/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/master/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/master/class_HubMasterDisconnectedException.php b/application/hub/exceptions/master/class_HubMasterDisconnectedException.php deleted file mode 100644 index 782aa1ac2..000000000 --- a/application/hub/exceptions/master/class_HubMasterDisconnectedException.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubMasterDisconnectedException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Raw message received: %s
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['msg'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/.htaccess b/application/hub/exceptions/socket/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/socket/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/socket/class_SocketException.php b/application/hub/exceptions/socket/class_SocketException.php deleted file mode 100644 index 867c84ca3..000000000 --- a/application/hub/exceptions/socket/class_SocketException.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 . - */ -abstract class SocketException extends HubException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public function __construct ($message, $code) { - // For now just pipe all data through - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/connector/.htaccess b/application/hub/exceptions/socket/connector/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/socket/connector/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/socket/connector/class_SocketAcceptException.php b/application/hub/exceptions/socket/connector/class_SocketAcceptException.php deleted file mode 100644 index ba2d03b29..000000000 --- a/application/hub/exceptions/socket/connector/class_SocketAcceptException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketAcceptException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] socket_accept() has failed. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/connector/class_SocketSelectorException.php b/application/hub/exceptions/socket/connector/class_SocketSelectorException.php deleted file mode 100644 index 83f7d8ade..000000000 --- a/application/hub/exceptions/socket/connector/class_SocketSelectorException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketSelectorException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] socket_select() has failed. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/init/.htaccess b/application/hub/exceptions/socket/init/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/socket/init/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/socket/init/class_SocketBindException.php b/application/hub/exceptions/socket/init/class_SocketBindException.php deleted file mode 100644 index 6b71716e8..000000000 --- a/application/hub/exceptions/socket/init/class_SocketBindException.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketBindException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Socket konnte nicht an %s:%d gebunden werden. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['host'], - $msgArray['port'], - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/init/class_SocketConnectException.php b/application/hub/exceptions/socket/init/class_SocketConnectException.php deleted file mode 100644 index 879cea4b6..000000000 --- a/application/hub/exceptions/socket/init/class_SocketConnectException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketConnectException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] socket_connect() has failed. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/init/class_SocketCreationException.php b/application/hub/exceptions/socket/init/class_SocketCreationException.php deleted file mode 100644 index eaeae36a3..000000000 --- a/application/hub/exceptions/socket/init/class_SocketCreationException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketCreationException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Socket konnte nicht erstellt werden. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/init/class_SocketListeningException.php b/application/hub/exceptions/socket/init/class_SocketListeningException.php deleted file mode 100644 index f6fce58ff..000000000 --- a/application/hub/exceptions/socket/init/class_SocketListeningException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketListeningException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] socket_listen() has failed. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/init/class_SocketSetupException.php b/application/hub/exceptions/socket/init/class_SocketSetupException.php deleted file mode 100644 index 789e6d110..000000000 --- a/application/hub/exceptions/socket/init/class_SocketSetupException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 SocketSetupException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Socket konnte nicht konfiguriert werden. Reason: %s (Code: %d)", - $msgArray['this']->__toString(), - $this->getLine(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/peer/.htaccess b/application/hub/exceptions/socket/peer/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/exceptions/socket/peer/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/exceptions/socket/peer/class_BrokenPipeException.php b/application/hub/exceptions/socket/peer/class_BrokenPipeException.php deleted file mode 100644 index 4217623b6..000000000 --- a/application/hub/exceptions/socket/peer/class_BrokenPipeException.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 BrokenPipeException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Connection lost to peer %s. Error message: %s (Code: %d)
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['this']->getValidatedIP(), - socket_strerror($msgArray['code']), - $msgArray['code'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/peer/class_IPSpoofingException.php b/application/hub/exceptions/socket/peer/class_IPSpoofingException.php deleted file mode 100644 index 6dab992be..000000000 --- a/application/hub/exceptions/socket/peer/class_IPSpoofingException.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 IPSpoofingException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Peer-IP hat sich von %s auf %s geändert.
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['old_ip'], - $msgArray['new_ip'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/exceptions/socket/peer/class_PeerSocketException.php b/application/hub/exceptions/socket/peer/class_PeerSocketException.php deleted file mode 100644 index b5f40eddd..000000000 --- a/application/hub/exceptions/socket/peer/class_PeerSocketException.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 PeerSocketException extends SocketException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public final function __construct (array $msgArray, $code) { - // Construct the message - $message = sprintf("[%s:%d] Peer-Socket ist ungültig. Datentyp: %s
\n", - $msgArray['this']->__toString(), - $this->getLine(), - $msgArray['type'] - ); - - // Call parent constructor - parent::__construct($message, $code); - } -} - -// [EOF] -?> diff --git a/application/hub/main/class_HubCommandProcessor.php b/application/hub/main/class_HubCommandProcessor.php deleted file mode 100644 index 0aba7a4eb..000000000 --- a/application/hub/main/class_HubCommandProcessor.php +++ /dev/null @@ -1,190 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubCommandProcessor extends BaseFrameworkSystem { - /** - * An instance of a HubPeer class - */ - private $peerInstance = null; - - /** - * A list of sent commands but not replied (yet) - */ - private $sentCommandsAwaitingAnswer = array(); - - /** - * Queued commands from the master hub - */ - private $masterCommands = array(); - - // Exceptions - const EXCEPTION_COMMAND_AWAIT_INVALID = 0xc00; - - /** - * The private constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Tidy up a little - $this->removeSystemArray(); - $this->removeNumberFormaters(); - } - - /** - * Creates an instance of a HubCommandProcessor class by a HubPeer class - * - * @param $peerInstance An instance of a HubPeer class - * @return $command An instance of this class - */ - public final static function createHubCommandProcessor (HubPeer $peerInstance) { - // Get a new instance of this class - $command = new HubCommandProcessor(); - - // Set the hub instance - $command->setPeerInstance($peerInstance); - - // Return the instance - return $command; - } - - /** - * Setter for HubPeer instances - * - * @param $peerInstance An instance of a HubPeer class - * @return void - */ - public final function setPeerInstance(HubPeer $peerInstance) { - $this->peerInstance = $peerInstance; - } - - /** - * Getter for HubPeer instances - * - * @return $peerInstance An instance of a HubPeer class - */ - public final function getPeerInstance() { - return $this->peerInstance; - } - - /** - * Handles simple commands which require an answer command from the peer without any parameters - * - * @param $sendCommand The command we shall send to the peer - * @param $expectCommand The command we except from the peer - * @return void - */ - public function simpleExecute ($sendCommand, $expectCommand) { - // Remember the command - $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand; - - // Debug message - $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s", - $this->__toString(), - $sendCommand, - $this->getPeerInstance()->getValidatedIP() - )); - - // Execute the command - $this->getPeerInstance()->sendMessage($sendCommand); - } - - /** - * Awaits a specified command from the peer and returns TRUE if received or FALSE if not. - * - * @param $exceptCommand The command we are expecting from the peer - * @return $await Wether the awaited command has been received - * @throws UnexpectedAwaitCommandException If the awaited command is not being awaited... - */ - public function ifAwaitsCommand ($expectCommand) { - $await = false; - - // Do we wait for this command? - if (!isset($this->sentCommandsAwaitingAnswer[$expectCommand])) { - // We are not waiting for this command - throw new UnexpectedAwaitCommandException ( - array( - 'this' => $this, - 'await' => $expectCommand - ), self::EXCEPTION_COMMAND_AWAIT_INVALID - ); - } // END - if - - // Then process it... :-) - $readCommand = $this->getPeerInstance()->readFromSocket(); - - // Is the command not empty? - if (!empty($readCommand)) { - // Debug message - $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s", - $this->__toString(), - $readCommand, - $this->getPeerInstance()->getValidatedIP() - )); - } // END - if - - if ($readCommand == $expectCommand) { - // Debug message - $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s", - $this->__toString(), - $expectCommand, - $this->getPeerInstance()->getValidatedIP() - )); - - // Expected command has been received! - unset($this->sentCommandsAwaitingAnswer[$expectCommand]); - $await = true; - - } // END - if - - // Return the result - return $await; - } - - /** - * Awaits any command from the peer - * - * @return $command The sent command from the peer - */ - public function awaitAnyCommand () { - $command = false; - $readCommand = $this->getPeerInstance()->readFromSocket(); - if (!empty($readCommand)) { - // Remember this command - // TODO Add some validation here! - $this->masterCommands[] = $readCommand; - - // A command is in the queue - $command = true; - } // END - if - - // Return status - return $command; - } - -} // END - class - -// [EOF] -?> diff --git a/application/hub/main/class_HubConnector.php b/application/hub/main/class_HubConnector.php deleted file mode 100644 index d97759dae..000000000 --- a/application/hub/main/class_HubConnector.php +++ /dev/null @@ -1,179 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubConnector extends BaseFrameworkSystem { - /** - * The socket we shall use for communication - */ - private $socketResource = null; - - /** - * The peer instance (HubPeer class) - */ - private $peerInstance = null; - - /** - * The private constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Tidy up a little - $this->removeSystemArray(); - $this->removeNumberFormaters(); - } - - /** - * Creates an instance based on the (IP) address and port number of this - * class. Next it tries to connect to the specified peer and communicates - * to it over a socket - * - * @param $address The peer's IP address - * @param $port The peer's port number - * @param $hubInstance An instance of a HubCoreLoop class - * @return $connector An instance of this class - * @throws HubHelloException If HELLO retries reached maximum - */ - public final static function createHubConnectorByAddressPort ($address, $port, HubCoreLoop $hubInstance) { - // Get a new instance of this class - $connector = new HubConnector(); - - // Connect to the given address and IP number - $connector->aquireConnectionToAddress($address, $port); - - // Message to console - $hubInstance->getOutputInstance()->output(sprintf("[%s] Sending %s request to hub %s:%d...", - __METHOD__, - $connector->getConfigInstance()->readConfig("hub_peer_hello"), - $address, - $port - )); - - // Get a HubPeer instance - $peerInstance = HubPeer::createHubPeerBySocket($connector->getSocketResource(), $hubInstance); - - // Send a HELLO to the master hub - $helloed = 0; $retries = 0; - while (!$peerInstance->ifHelloReplied()) { - // Within timeout? - if ((time() - $helloed) >= $connector->getConfigInstance()->readConfig("hub_hello_timeout")) { - // Send HELLOs out in periodic times - $peerInstance->sendMessage($connector->getConfigInstance()->readConfig("hub_peer_hello")); - $helloed = time(); $retries++; - if ($retries == $connector->getConfigInstance()->readConfig("hub_hello_retires")) { - // Maximum retries reached - throw new HubHelloException( - array( - 'this' => $connector, - 'peer' => $peerInstance - ), HubCoreLoop::EXCEPTION_HELLO_TIMED_OUT - ); - } - } // END - if - } // END - while - - // Set the peer instance - $connector->setPeerInstance($peerInstance); - - // Return the instance - return $connector; - } - - /** - * Aquires a socket link to a specified IP address and port number. It - * throws subclasses of SocketException-s if the peer is down or not - * responding. If the connection is up it writes it's resource into - * the attribute $socketResource and waits for incoming data packages. - * - * @param $address The peer's IP address - * @param $port The peer's port number - * @return void - * @throws SocketCreationException If creation of the socket wents wrong - * @throws SocketConnectException If the connection was not established - */ - public function aquireConnectionToAddress ($address, $port) { - // Create a socket for binding to the address - $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - - // Was it a success? - if (!is_resource($socket)) { - // This fails! :( - throw new SocketCreationException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), HubCoreLoop::EXCEPTION_SOCKET_PROBLEM - ); - } // END - fi - - // Now connect to the peer - if (@socket_connect($socket, $address, $port) === false) { - // Something wents wrong! - throw new SocketConnectException ( - array( - 'this' => $this, - 'code' => socket_last_error() - ), HubCoreLoop::EXCEPTION_SOCKET_PROBLEM - ); - } // END - if - - // Set non-blocking mode - @socket_set_nonblock($socket); - - // Set the socket - $this->socketResource = $socket; - } - - /** - * Getter for current socket - * - * @return $socketResource The current socket resource or null if not set - */ - public final function getSocketResource() { - return $this->socketResource; - } - - /** - * Setter for a HubPeer instance - * - * @param $peerInstance An instance of a HubPeer class - * @return void - */ - public final function setPeerInstance (HubPeer $peerInstance) { - $this->peerInstance = $peerInstance; - } - - /** - * Handles any incoming master requests - * - * @return void - */ - public function handleMasterRequests () { - // Just ask the peer instance - $command = $this->peerInstance->handleMasterRequests(); - } - -} // END - class -?> diff --git a/application/hub/main/class_HubCoreLoop.php b/application/hub/main/class_HubCoreLoop.php deleted file mode 100644 index e2cc62994..000000000 --- a/application/hub/main/class_HubCoreLoop.php +++ /dev/null @@ -1,755 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubCoreLoop extends BaseFrameworkSystem { - /** - * Wether the hub is active and running - */ - private $hubActivated = false; - - /** - * Wether the hub is shutting down - */ - private $hubShutsDown = false; - - /** - * A list of all connected peers (sockets) - */ - private $connectedPeers = null; - - /** - * A console output handler - */ - private $outputInstance = null; - - /** - * Reading peers - */ - private $readPeers = array(); - - /** - * Writing peers - */ - private $writePeers = array(); - - /** - * The main socket (listening) for this hub - */ - private $main_socket = null; - - /** - * A list of authenticated peers - */ - private $authPeers = null; - - /** - * Last peer instance - */ - private $lastPeerInstance = null; - - /** - * Wether this hub is a master hub - */ - private $hubIsMaster = false; - - /** - * Maximum auth retries (cached) - */ - private $authRetries = 0; - - /** - * Auth request message - */ - private $authRequest = ""; - - /** - * Cached timeout for auth requests - */ - private $authRequestTimeout = 0; - - /** - * An instance to the HubConnector class for master hub connection - */ - private $masterConnector = null; - - // Exception codes - const EXCEPTION_SOCKET_PROBLEM = 0xb00; - const EXCEPTION_HUB_PEER_TIMEOUT = 0xb01; - const EXCEPTION_HUB_PEER_FAILED_AUTH = 0xb02; - const EXCEPTION_HELLO_TIMED_OUT = 0xb03; - - /** - * The private constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Tidy up a little - $this->removeSystemArray(); - $this->removeNumberFormaters(); - - // Init the peer list - $this->initPeerList(); - } - - /** - * Factory for main loop - * - * @return $hubInstance An instance of this class - */ - public final static function createHubCoreLoop () { - // Get an instance - $hubInstance = new HubCoreLoop(); - - // Try to setup the socket - $hubInstance->setupHub(); - - // Get the configuration variable - $outEngine = $hubInstance->getConfigInstance()->readConfig("tpl_engine"); - - // Setup the console output handler - $eval = sprintf("\$hubInstance->setOutputInstance(%s::create%s(\"%s\"));", - $outEngine, - $outEngine, - $hubInstance->getConfigInstance()->readConfig("web_content_type") - ); - - // Debug message - if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $hubInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: %s", - $hubInstance->__toString(), - htmlentities($eval) - )); - @eval($eval); - - // Return the prepared instance - return $hubInstance; - } - - /** - * Initializes the peer list - * - * @return void - */ - private final function initPeerList () { - $this->connectedPeers = new FrameworkArrayObject("FakedConnectedPeers"); - $this->authPeers = new FrameworkArrayObject("FakeAuthPeers"); - } - - /** - * Get total number of connected peers - * - * @return $num The total number of connected peers - */ - private final function getTotalConnectedPeers () { - $read = array($this->connectedPeers->getIterator()->current()); - $write = array(); - $except = array(); - - // Get socket number - $num = socket_select( - $read, - $write, - $except, - 0 - ); - - // Transfer readers and writers - $this->readPeers = $read; - $this->writePeers = $write; - - // Return the number - return $num; - } - - /** - * Handle newly connected peers - * - * @return void - */ - private final function handleNewPeers () { - // Output message - $this->getOutputInstance()->output(sprintf("[%s] Validating peer...", __METHOD__)); - - // Is the main socket in the array? - if (in_array($this->main_socket, $this->readPeers)) { - // Accept the connection and add him to the list - $peer_socket = socket_accept($this->main_socket); - - // Get a new peer instance - $this->setCurrPeerInstance(HubPeer::createHubPeerBySocket($peer_socket, $this)); - - // Register the new peer - $this->getOutputInstance()->output(sprintf("[%s] Registering new peer with IP %s.", - __METHOD__, - $this->getCurrPeerInstance()->getValidatedIP() - )); - $this->connectedPeers->append($this->lastPeerInstance); - - // A new peer has connected - $this->getOutputInstance()->output(sprintf("[%s] New peer with IP %s has been registered.", - __METHOD__, - $this->getCurrPeerInstance()->getValidatedIP() - )); - - // Remove him from the list - $key = array_search($this->main_socket, $this->readPeers); - unset($this->readPeers[$key]); - } // END - if - } - - /** - * Handles unauthenticated peers - * - * @return void - * @throws HubPeerTimeoutException If the peer times out to answer a request - * @throws HubPeerAuthorizationException If the peer fails to authorize himself (to many retries) - */ - private final function handleUnauthPeers () { - // Are there some peers? - if ($this->connectedPeers->count() > 1) { - // Iterate through all connected peers - for ($idx = $this->connectedPeers->getIterator(); $idx->valid(); $idx->next()) { - // Get current peer - $this->lastPeerInstance = $idx->current(); - - // Ignore own socket and invalid entries - if (($this->getCurrPeerInstance() !== $this->main_socket) && (is_object($this->getCurrPeerInstance())) && ($this->getCurrPeerInstance() instanceof HubPeer)) { - // Is this peer already authorized or is this the master hub? - // If this is the master hub then there is no auth required - if ((!$this->getCurrPeerInstance()->ifPeerIsAuthorized()) && (!$this->getCurrPeerInstance()->ifPeerIsLocalAdmin()) && (!$this->hubIsMaster)) { - // This peer waits for authorization, so does he have some tries left? - if ($this->getCurrPeerInstance()->getAuthRetries() <= $this->authRetries) { - // This peer is still allowed to try his authorization - if ($this->getCurrPeerInstance()->getLastSentMessage() == $this->authRequest) { - // Already asked so maybe timed out? - if ((time() - $this->getCurrPeerInstance()->getLastSentMessageStamp()) >= $this->authRequestTimeout) { - // Timed out so disconnect the peer - throw new HubPeerTimeoutException ( - array( - 'this' => $this, - 'peer' => $this->getCurrPeerInstance() - ), self::EXCEPTION_HUB_PEER_TIMEOUT - ); - } // END - if - } elseif ($this->getCurrPeerInstance()->ifHelloReceived()) { - // HELLO received so we need to sent the AUTH request - $this->getCurrPeerInstance()->askAuthorizationKey(); - } - } else { - // This peer needs disconnecting! - throw new HubPeerAuthorizationException ( - array( - 'this' => $this, - 'peer' => $this->getCurrPeerInstance(), - 'max' => $this->authRetries - ), self::EXCEPTION_HUB_PEER_FAILED_AUTH - ); - } // END - else - } elseif ((!$this->getCurrPeerInstance()->ifPeerIsAuthorized()) && ($this->getCurrPeerInstance()->ifPeerIsLocalAdmin())) { - // This peer is a local admin so he is always authorized! - $this->getCurrPeerInstance()->enableLocalAdmin(); - - // Output debug message - $this->getOutputInstance()->output(sprintf("[%s] A local admin has connected.", - $this->__toString() - )); - - // Say "hi" to the admin - $this->getCurrPeerInstance()->sayHi2Admin(); - } elseif (($this->hubIsMaster) && ($this->getCurrPeerInstance()->ifHelloReceived()) && (!$this->getCurrPeerInstance()->ifELHOsent())) { - // Is the master hub so authorize the peer here... - $this->getCurrPeerInstance()->enableIsAuthorized(); - $this->authPeers->append($this->getCurrPeerInstance()); - - // Reply the HELLO request - $this->getCurrPeerInstance()->replyHelloMessage(); - } - } // END - if - } // END - for - } // END - if - } - - /** - * Handles only authorized peers - * - * @return void - */ - public function handleAuthPeers () { - // Are there some peers? - if (($this->connectedPeers->count() > 1) && ($this->authPeers->count() > 0)) { - // Iterate through all connected peers - for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) { - // Get current peer - $this->lastPeerInstance = $idx->current(); - - // Do the ping and update our authPeer list (LATER!) - $this->lastPeerInstance->handlePingPeer(); - - // Avoids a notice... - if (is_null($this->getCurrPeerInstance())) break; - } // END - for - } // END - for - } - - /** - * Setup the hub: Create a socket for listening on incoming requests, - * try to bind to a port, etc. - * - * @return void - * @throws SocketCreationException If a socket cannot be created - * @throws SocketSetupException If a socket cannot be setuped - * @throws SocketBindException If a socket cannot be bind to - * an address and port - * @throws SocketListeningException If listening to a socket fails - */ - private final function setupHub () { - // Create a new TCP socket - $main_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - - // Was it a success? - if (!is_resource($main_socket)) { - // This fails! :( - throw new SocketCreationException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_SOCKET_PROBLEM - ); - } - - // Set socket options - if (!socket_set_option($main_socket, SOL_SOCKET, SO_REUSEADDR, 1)) { - // Close the socket - $this->closeSocket($main_socket); - - // Problems setting socket options - throw new SocketSetupException ( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_SOCKET_PROBLEM - ); - } - - // Set to non-blocking - socket_set_nonblock($main_socket); - - // Bind the socket to an address - if (!socket_bind($main_socket, $this->getConfigInstance()->readConfig("hub_listen_addr"), $this->getConfigInstance()->readConfig("hub_listen_port"))) { - // Bind failed - throw new SocketBindException ( - array( - 'this' => $this, - 'host' => $this->getConfigInstance()->readConfig("hub_listen_addr"), - 'port' => $this->getConfigInstance()->readConfig("hub_listen_port"), - 'code' => socket_last_error() - ), self::EXCEPTION_SOCKET_PROBLEM - ); - } - - // Listen to ... the socket ;-) - if (!socket_listen($main_socket)) { - // Opps, that didn't work. Next time better listen to your heart... Roxette - throw new SocketListeningException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_SOCKET_PROBLEM - ); - } - - // Ignore user abort and do not time out - @set_time_limit(0); - @ignore_user_abort(true); - - // Cache more configuration stuff - $this->authRetries = $this->getConfigInstance()->readConfig("hub_max_auth_tries"); - $this->authRequest = $this->getConfigInstance()->readConfig("hub_auth_request"); - $this->authRequestTimeout = $this->getConfigInstance()->readConfig("hub_auth_request_timeout"); - - // The hub is running now! - $this->hubActivated = true; - - // Append the main hub - $this->main_socket = $main_socket; - $this->connectedPeers->append($main_socket); - } - - /** - * Removes the current peer from the list - * - * @return void - */ - public final function removePeerFromConnectList () { - // Iterate through all connected peers - for ($idx = $this->connectedPeers->getIterator(); $idx->valid(); $idx->next()) { - // Get current peer from list - $peer = $idx->current(); - - // Is it a peer instance? - if (($peer !== $this->main_socket) && (is_object($peer)) && ($peer instanceof HubPeer)) { - // Okay, we have a peer instance so is it the same? - if ($this->getCurrPeerInstance()->equals($peer)) { - // Remove him! - $idx->offsetUnset($idx->key()); - - // Remove the last instance as well - $this->lastPeerInstance = null; - - // Remove the socket as well - $this->removeSocket($peer); - - // Stop searching here - break; - } - } - } - } - - /** - * Getter for console output handler - * - * @return $outputInstance An instance of the output handler - */ - public final function getOutputInstance () { - return $this->outputInstance; - } - - /** - * Setter for console output handler - * - * @param $outputInstance An instance of the output handler - */ - public final function setOutputInstance ($outputInstance) { - $this->outputInstance = $outputInstance; - } - - /** - * Getter for current peer instance to HubPeer class - * - * @return $lastPeerInstance Last peer instance - */ - public final function getCurrPeerInstance () { - return $this->lastPeerInstance; - } - - /** - * Setter for current peer instance to HubPeer class - * - * @param $lastPeerInstance Last peer instance - * @return void - */ - public final function setCurrPeerInstance (HubPeer $lastPeerInstance) { - $this->lastPeerInstance = $lastPeerInstance; - } - - /** - * Checks wether the hub is running and not in shutdown phase - * - * @return $isRunning If the hub is running and not in shutdown phase - */ - public function ifHubIsRunning () { - return ((($this->hubActivated) || (!$this->hubShutsDown)) && ($this->connectedPeers->count() > 0)); - } - - /** - * Output the intro text - * - * @return void - */ - public final function outputIntro () { - if ($this->getConfigInstance()->readConfig("hub_intro_enabled") == "Y") { - // Output intro text - $this->getOutputInstance()->output(sprintf("%s v%s Copyright (c) 2007, 2008 by Roland Häder", - ApplicationHelper::getInstance()->getAppName(), - ApplicationHelper::getInstance()->getAppVersion() - )); - $this->getOutputInstance()->output(""); - $this->getOutputInstance()->output("This software is free software licensed under the GNU GPL. In telnet session enter "/license" to read the license."); - $this->getOutputInstance()->output("This software uses the MXChange Framework, Copyright (c) 2007, 2008 by Roland Häder which is licensed under the GNU GPL."); - $this->getOutputInstance()->output("Enter "/framework" in telnet session to read that license."); - $this->getOutputInstance()->output(""); - $this->getOutputInstance()->output("All core systems are initialized. Input on *this* console will currently be ignored!"); - $this->getOutputInstance()->output(""); - $this->getOutputInstance()->output(sprintf("[%s] Listening on: %s:%d", - $this->__toString(), - $this->getConfigInstance()->readConfig("hub_listen_addr"), - $this->getConfigInstance()->readConfig("hub_listen_port") - )); - $this->getOutputInstance()->output("----------------------------------------------------------------------------------------------------------------------------"); - } - } - - /** - * Do the main loop - * - * @return void - * @throws SocketCreationException If the main socket is not a resource - */ - public function coreLoop () { - // Is the main socket vailid? - if (!is_resource($this->main_socket)) { - // Is not valid! - throw new SocketCreationException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_SOCKET_PROBLEM - ); - } // END - if - - // We are ready to serve requests - $this->getOutputInstance()->output(sprintf("[%s] Ready to serve requests.", - $this->__toString() - )); - - // Wait until the hub is shutting down - while ($this->ifHubIsRunning()) { - // Get number of total connected peers - $num = $this->getTotalConnectedPeers(); - - try { - // Handle the master hub connection - if ($this->masterConnector instanceof HubConnector) { - $this->masterConnector->handleMasterRequests(); - } - - // Check for unauthorized peers - $this->handleUnauthPeers(); - - // Handle authorized peers - $this->handleAuthPeers(); - } catch (HubPeerAuthorizationException $e) { - // Authorization has failed - $this->disconnectPeerWithReason("hub_msg_auth_tries"); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } catch (HubPeerTimeoutException $e) { - // Disconnect and remove the peer - $this->disconnectPeerWithReason("hub_msg_auth_reply_timeout"); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } catch (HubMasterDisconnectedException $e) { - // The master hub has disconnected us... :( - $this->masterConnector = null; - $this->getOutputInstance()->output(sprintf("[%s] The master has disconnected us. Reason given: %s", - $this->__toString(), - $e->getMessage() - )); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } catch (BrokenPipeException $e) { - // Broken pipes are bad for us... :( - $this->removePeerFromConnectList(); - $this->getOutputInstance()->output(sprintf("[%s] A peer has closed the connection unexpected: %s", - $this->__toString(), - $e->getMessage() - )); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } catch (FrameworkException $e) { - // Catch all other exceptions and output them - hub_exception_handler($e); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } - - // Are there some peers? - if ($num < 1) { - // Wait for more peers - continue; - } // END - if - - // A new peer has connected - $this->getOutputInstance()->output(sprintf("[%s] A new peer is connecting.", - $this->__toString() - )); - - try { - // Check for new peers - $this->handleNewPeers(); - } catch (IPSpoofingException $e) { - // Output message - $this->getOutputInstance()->output(sprintf("[%s] The peer's IP number has changed!", - $this->__toString() - )); - - // Output debug message - $this->getDebugInstance()->output(sprintf("[%s] Peer spoofes IP number: %s", - $this->__toString(), - $e->getMessage() - )); - - // Disconnect the peer first - $this->disconnectPeerWithReason("hub_msg_spoofing"); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } catch (FrameworkException $e) { - // Catch all exceptions and output them to avoid aborting the program unexpectly - hub_exception_handler($e); - - // Get new total connected peers - $num = $this->getTotalConnectedPeers(); - } - - } // END - while - } - - /** - * Tries to contact the master server or simply reports that we are the master server - * - * @return void - */ - public function contactMasterHub () { - // Checks wether we are the master hub - if ($_SERVER['SERVER_ADDR'] == $this->getConfigInstance()->readConfig("hub_master_ip")) { - // We are master! - $this->hubIsMaster = true; - $this->getOutputInstance()->output(sprintf("[%s] Our IP %s matches the master IP. Becoming master hub...", - $this->__toString(), - $_SERVER['SERVER_ADDR'] - )); - } else { - // A regular hub or ultra hub so let's contact the master - $this->getOutputInstance()->output(sprintf("[%s] Contacting the master hub at %s:%d...", - $this->__toString(), - $this->getConfigInstance()->readConfig("hub_master_ip"), - $this->getConfigInstance()->readConfig("hub_master_port") - )); - - // Try to aquire a connection to the master... - try { - // Announce us to the master hub - $this->announceToMasterHub(); - } catch (FrameworkException $e) { - // Catch all exceptions and output them - hub_exception_handler($e); - } - } // END - else - } - - /** - * Disconnects the current with a configurable reason - * - * @param $reasonEntry The entry with the disconnection reason aka. message to the peer - * @return void - */ - public function disconnectPeerWithReason ($reasonEntry) { - // Default is that we cannot read the peer's IP number - $ip = "0.0.0.0"; - - // Try to disconnect here - try { - // First get the raw IP number - $ip = $this->getCurrPeerInstance()->getValidatedIP(); - - // Disconnect the peer... - $this->getCurrPeerInstance()->disconnectWithReason($this->getConfigInstance()->readConfig($reasonEntry)); - } catch (FrameworkException $e) { - // Catch all exceptions and output them - hub_exception_handler($e); - } - - // Remove him from the list anyway - $this->removePeerFromConnectList(); - - // Output the message - $this->getOutputInstance()->output(sprintf("[%s] Peer %s has been disconnected.", - $this->__toString(), - $ip - )); - } - - /** - * Announces this hub to the master hub. This is being done by connecting to it and asking for auth request - * - * @return void - */ - public function announceToMasterHub () { - try { - // Create a new instance for communicating to the master hub - $this->masterConnector = HubConnector::createHubConnectorByAddressPort( - $this->getConfigInstance()->readConfig("hub_master_ip"), - $this->getConfigInstance()->readConfig("hub_master_port"), - $this - ); - - // Send all our accepted objects to the master hub - $this->masterConnector->sendAllAcceptedObjects(); - } catch (SocketConnectException $e) { - // The master hub is down! - ApplicationEntryPoint::app_die($e->getMessage()); - } - } - - /** - * Removes a given peer instance (socket) from all lists - * - * @param $peerInstance An instance of a HubPeer class - * @return void - */ - private function removeSocket (HubPeer $peerInstance) { - // Get socket from peer - $socket = $peerInstance->getPeerSocket(); - - // Search for readers - $key = array_search($socket, $this->readPeers, true); - if ($key !== false) { - // Remove from reader list - unset($this->readPeers[$key]); - } - - // Search for writers - $key = array_search($socket, $this->writePeers, true); - if ($key !== false) { - // Remove from writer list - unset($this->writePeers[$key]); - } - - // Remove from auth peers as well - for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) { - // Get current entry - $current = $idx->current(); - - // Is it the same? - if ($current->equals($peerInstance)) { - // Remove from auth (-awaiting) list - $idx->offsetUnset($idx->key()); - break; - } - } - } - -} // END - class - -// [EOF] -?> diff --git a/application/hub/main/class_HubPeer.php b/application/hub/main/class_HubPeer.php deleted file mode 100644 index c257bf2f7..000000000 --- a/application/hub/main/class_HubPeer.php +++ /dev/null @@ -1,670 +0,0 @@ - - * @version 0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software - * @license GNU GPL 3.0 or any newer version - * - * 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 HubPeer extends BaseFrameworkSystem { - /** - * This peer's socket resource - */ - private $peerSocket = null; - - /** - * Timestamp for connection - */ - private $connectTime = 0; - - /** - * Timestamp of last received message - */ - private $lastReceivedMessageStamp = 0; - - /** - * Last received message - */ - private $lastReceivedMessage = ""; - - /** - * Timestamp of last sent message - */ - private $lastSentMessageStamp = 0; - - /** - * Last sent message - */ - private $lastSentMessage = ""; - - /** - * Number of tries for authorization - */ - private $authRetries = 0; - - /** - * Wether the peer is authorized (DO NEVER SET THIS TO "true"!) - */ - private $isAuthorized = false; - - /** - * Wether this peer needs to be requested for the AUTH command - */ - private $needAuthRequest = true; - - /** - * The peer's IP number - */ - private static $peerIP = "0.0.0.0"; - - /** - * Wether the peer has "hello-ed" to the other - */ - private $helloSent = false; - - /** - * Wether the peer has replied our HELLO - */ - private $helloReplied = false; - - /** - * Wether we have sent our ELHO - */ - private $elhoSent = false; - - /** - * An instance of HubCoreLoop - */ - private $hubInstance = null; - - /** - * When the last ping was - */ - private $lastPinged = 0; - - /** - * When the last pong was - */ - private $lastPonged = 0; - - /** - * Number of missing pongs from the other peer - */ - private $missingPongs = 0; - - /** - * An instance of a HubCommandProcessor class - */ - private $commandInstance = null; - - /** - * A queue for reading data in non-blocking mode - */ - private $queues = ""; - - /** - * Line ends - */ - const LINE_END = "\n"; - - //---------------------------------------------------------- - // Exceptions - //---------------------------------------------------------- - const EXCEPTION_PEER_SOCKET_INVALID = 0x200; - const EXCEPTION_PEER_IP_CHANGED = 0x201; - const EXCEPTION_PEER_SOCKET_BROKEN = 0x202; - //---------------------------------------------------------- - - /** - * The private constructor - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Tidy up a little - $this->removeSystemArray(); - $this->removeNumberFormaters(); - - // Get a command processor - $this->commandInstance = HubCommandProcessor::createHubCommandProcessor($this); - } - - /** - * Creates an instance of a HubPeer by a provided valid socket resource - * - * @param $peerSocket The socket resource for the peer - * @param $hubInstance An instance of HubCoreLoop - * @return $peerInstance An instance of HubPeer - */ - public final static function createHubPeerBySocket ($peerSocket, HubCoreLoop $hubInstance) { - // Get a new instance - $peerInstance = new HubPeer(); - - // Is the peer socket fine? - if (!is_resource($peerSocket)) { - // There is a problem with the socket - throw new PeerSocketException ( - array( - 'this' => $peerInstance, - 'type' => gettype($peerSocket) - ), self::EXCEPTION_PEER_SOCKET_INVALID - ); - } - - // Set the socket - $peerInstance->setPeerSocket($peerSocket); - - // Set connection timestamp - $peerInstance->setConnectionTimestamp(); - - // Set the hub instance - $peerInstance->setHubInstance($hubInstance); - - // Return the instance - return $peerInstance; - } - - //---------------------------------------------------------- - // Public getter/setter - //---------------------------------------------------------- - - /** - * Setter for peer socket - * - * @param $peerSocket The peer's socket - * @return void - */ - public final function setPeerSocket ($peerSocket) { - if (is_resource($peerSocket)) { - $this->peerSocket = $peerSocket; - } else { - $this->peerSocket = null; - } - } - - /** - * Getter for peer socket - * - * @return $peerSocket The peer's socket - */ - public final function getPeerSocket () { - return $this->peerSocket; - } - - /** - * Setter for connection timestamp only once - * - * @return void - */ - public final function setConnectionTimestamp () { - if ($this->connectTime == 0) $this->connectTime = time(); - } - - /** - * Getter for a raw IP number - * - * @return $ip The peer's IP number - * @throws BrokenPipeException If a socket has lost its connection to the peer - */ - public final function getRawIP () { - if (is_resource($this->peerSocket)) { - // Get IP from socket - @socket_getpeername($this->peerSocket, $ip); - - // Connection problems? - if (socket_last_error() > 0) { - // Throw an exception - throw new BrokenPipeException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_PEER_SOCKET_BROKEN - ); - } - } else { - // Without a socket we cannot determine the right IP - $ip = "0.0.0.0"; - } - - // And return it... - return $ip; - } - - /** - * Getter for a validated peer IP - * - * @return $peerIP The peer's IP number - */ - public final function getValidatedIP() { - // Is the socket valid and IP not set? - if ((is_resource($this->peerSocket)) && (socket_last_error() == 0) && (self::$peerIP == "0.0.0.0")) { - // Get peer's IP number - self::$peerIP = $this->getRawIP(); - } elseif ((is_resource($this->peerSocket)) && (socket_last_error() == 0)) { - // Get current raw IP number for validation - $ip = $this->getRawIP(); - - // Check if the IP has changed - if ($ip !== self::$peerIP) { - // The IP number has changed! - throw new IPSpoofingException( - array( - 'this' => $this, - 'old_ip' => self::$peerIP, - 'new_ip' => $ip - ), self::EXCEPTION_PEER_IP_CHANGED - ); - } - } - - return self::$peerIP; - } - - /** - * Getter for number of authorization tries - * @return $authRetries Authorization tries - */ - public final function getAuthRetries () { - return $this->authRetries; - } - - //---------------------------------------------------------- - // Public methods - //---------------------------------------------------------- - - /** - * Setter for HubCoreLoop instances - * - * @param $hubInstance An instance of a HubCoreLoop class - * @return void - */ - public final function setHubInstance(HubCoreLoop $hubInstance) { - $this->hubInstance = $hubInstance; - } - - /** - * Getter for HubCoreLoop instances - * - * @return $hubInstance An instance of a HubCoreLoop class - */ - public final function getHubInstance() { - return $this->hubInstance; - } - - /** - * Getter for last sent message timestamp - * - * @return $lastSentMessageStamp Timestamp of last sent message - */ - public final function getLastSentMessageStamp () { - return $this->lastSentMessageStamp; - } - - /** - * Getter for last sent message - * - * @return $lastSentMessage Last sent message to this peer - */ - public final function getLastSentMessage () { - return $this->lastSentMessage; - } - - /** - * Determines wether the peer is authorized - * - * @return void - */ - public final function ifPeerIsAuthorized () { - return (($this->isAuthorized === true) && ($this->ifPeerNeedsAuthRequest() === false)); - } - - /** - * Returns wether this peer needs to receive the AUTH command - */ - public final function ifPeerNeedsAuthRequest () { - return $this->needAuthRequest; - } - - /** - * Disconnects the peer with a special reason (status) - * - * @param $reason The special reason - * @return void - */ - public final function disconnectWithReason ($reason) { - // Is the message set? - if (!empty($reason)) { - // Send the given message - $this->sendMessage($reason); - } else { - // Send default "good bye" - $this->sendMessage($this->getConfigInstance()->readConfig("hub_msg_bye")); - } - - // Disconnect the client - socket_shutdown($this->peerSocket, 2); - socket_close($this->peerSocket); - } - - /** - * Sends a specified message to the peer's socket - * - * @param $message The special message - * @return void - * @throws BrokenPipeException If a pipe to a socket peer is lost - */ - public final function sendMessage ($message) { - if (empty($message)) { - // Set default message - $message = $this->getConfigInstance()->readConfig("hub_msg_bye"); - } - - // Debug message - $this->getDebugInstance()->output(sprintf("[%s] Sending message \"%s\" to peer %s.
\n", - __METHOD__, - $message, - $this->getValidatedIP() - )); - - // Is the socket still valid? - if (!is_resource($this->peerSocket)) { - // The socket is no longer valid! - throw new BrokenPipeException ( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_PEER_SOCKET_BROKEN - ); - } - - // Send it to the peer - if (socket_write($this->peerSocket, $message."\n") !== false) { - // Set the timestamp and message - $this->lastSentMessage = $message; - $this->lastSentMessageStamp = time(); - } else { - // Message could not be sent - throw new BrokenPipeException ( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_PEER_SOCKET_BROKEN - ); - } - } - - /** - * Asks the connected new peer for the authorization key - * - * @return void - */ - public final function askAuthorizationKey () { - if ($this->ifPeerNeedsAuthRequest()) { - // This peer needs to receive the AUTH command so send it to him - $this->sendMessage($this->getConfigInstance()->readConfig("hub_auth_request")); - - // Set it to done - $this->needAuthRequest = false; - } - } - - /** - * Determines wether the peer is a local admin (localhost connection) - * - * @return $isLocalAdmin Wether the peer is a local admin - */ - public function ifPeerIsLocalAdmin () { - // Get the remote IP and extract only the first three numbers - $remoteArray = explode(".", self::$peerIP); - - // Is the peer a local admin? - return (($remoteArray[0].".".$remoteArray[1].".".$remoteArray[2]) == "127.0.0"); - } - - /** - * Enables the local admin and authorizes this peer - * - * @return void - */ - public function enableLocalAdmin() { - // Is this IP really local? - if (($this->ifPeerIsLocalAdmin()) && (!$this->ifPeerIsAuthorized())) { - // Then authorize him - $this->isAuthorized = true; - $this->needAuthRequest = false; - } // END - if - } - - /** - * Says "hi" to the local admin - * - * @return void - */ - public function sayHi2Admin () { - // Send a message to him... ;-) - $this->sendMessage("Local admin console is ready. Enter HELP for instructions."); - } - - /** - * Enables wether the peer is authorized - * - * @param $isAuthValid Wether the authorization wents fine - * @return void - */ - public function enableIsAuthorized ($isAuthValid = true) { - $this->isAuthorized = true; - } - - /** - * Checks wether a HELLO has been sent and if not it will be send to the other peer - * - * @return $helloSent Wether a HELLO has been sent - */ - public function ifHelloReceived () { - // HELLO has been sent? - if (!$this->helloSent) { - // Read some data - $read = $this->readFromSocket(); - - // Is this a HELLO? - if ($read == $this->getConfigInstance()->readConfig("hub_peer_hello")) { - // All right! A HELLO has been received - $this->helloSent = true; - $this->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Peer %s said HELLO to us.", - __METHOD__, - $this->getValidatedIP() - )); - } // END - if - } // END - if - - // Return status - return $this->helloSent; - } - - /** - * Wether this hub has replied our HELLO request - * - * @return $helloReplied Wether this hub has replied our HELLO request - */ - public function ifHelloReplied () { - if ((!$this->helloReplied) && ($this->lastSentMessage == $this->getConfigInstance()->readConfig("hub_peer_hello"))) { - // Read some data - $read = $this->readFromSocket(); - - // Is this a HELLO? - if ($read == $this->getConfigInstance()->readConfig("hub_hello_reply")) { - // Is this the master IP? - if ($this->getValidatedIP() == $this->getConfigInstance()->readConfig("hub_master_ip")) { - // All right! A HELLO has been received - $this->helloReplied = true; - $this->getHubInstance()->getOutputInstance()->output(sprintf("[%s] The master hub at %s:%d replied our %s.", - __METHOD__, - $this->getValidatedIP(), - $this->getConfigInstance()->readConfig("hub_master_port"), - $this->getConfigInstance()->readConfig("hub_peer_hello") - )); - } else { - // ELHOs from non-masters are not valid! - $this->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Peer %s replied our %s but is not the master hub!", - __METHOD__, - $this->getValidatedIP(), - $this->getConfigInstance()->readConfig("hub_peer_hello") - )); - } - } // END - if - } // END - if - - // Return status - return $this->helloReplied; - } - - /** - * Returns wether a ELHO (HELLO reply has been sent) - * - * @return $elhoSent Wether a ELHO has been sent to the peer - */ - public final function ifELHOsent () { - return $this->elhoSent; - } - - /** - * Replies a HELLO message with a ELHO message - * - * @return void - */ - public function replyHelloMessage () { - $this->sendMessage($this->getConfigInstance()->readConfig("hub_hello_reply")); - $this->elhoSent = true; - } - - /** - * Handles pinging this peer - * - * @return void - */ - public function handlePingPeer () { - $lost = false; - - // Do we need to ping? - if ((time() - $this->lastPinged) > $this->getConfigInstance()->readConfig("hub_ping_timeout")) { - // Don't ping any masters! ;-) - if ($this->getValidatedIP() != $this->getConfigInstance()->readConfig("hub_master_ip")) { - // Debug message - $this->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending ping to peer %s...", - $this->__toString(), - $this->getValidatedIP() - )); - - // Send out a PING and await a PONG - $this->commandInstance->simpleExecute( - $this->getConfigInstance()->readConfig("hub_peer_ping"), - $this->getConfigInstance()->readConfig("hub_ping_reply") - ); - - // PONG received within last ping? - if (($this->lastPonged < time()) && (($this->lastPonged - $this->lastPinged) < 0)) { - // Not replied so far - $this->missingPongs++; - - // Debug message - $this->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Ping not replied! Try: %d", - $this->__toString(), - $this->missingPongs - )); - - // Limit reached? - if ($this->missingPongs == $this->getConfigInstance()->readConfig("hub_ping_maxdrops")) { - // This peer is lost - $this->getHubInstance()->disconnectPeerWithReason("hub_peer_miss_pong"); - $lost = true; - } // END - if - } // END - if - - // Last time we pinged is now. - $this->lastPinged = time(); - - } // END - if - } // END - if - - // Connection is lost? - if ($lost === true) return false; - - // Awaiting PONG here - if ($this->commandInstance->ifAwaitsCommand($this->getConfigInstance()->readConfig("hub_ping_reply"))) { - // PONG received! :-) So reset all counters... - $this->lastPonged = time(); - $this->missingPongs = 0; - - // Notify the loop about the ping-pong-time - $this->getHubInstance()->updatePeerEntry($this, (time() - $this->lastPinged)); - } // END - if - } - - /** - * Handles any incoming commands from the master hub - * - * @return void - */ - public function handleMasterRequests () { - // Read the raw socket for data packages - if ($this->commandInstance->awaitAnyCommand()) { - // A command has been received from the master hub - $command = $this->commandInstance->pull(); - - // TODO Handle a command from the master here... - } // END - if - } - - /** - * Reads raw data from the socket and trims leading/trailing spaces away. - * Returns an empty string if no data has been received. - * - * @return $data Raw data from the underlaying socket - * @throws BrokenPipeException If a socket has lost its connection to the peer - */ - public function readFromSocket () { - $data = ""; - $read = array($this->peerSocket); - $write = null; - $except = null; - $num = socket_select($read, $write, $except, 0); - if ($num > 0) { - // Something has changed on a socket - foreach ($read as $socket) { - if (is_resource($socket)) { - $data = trim(@socket_read($socket, 1024, PHP_NORMAL_READ)); - if (socket_last_error() > 0) { - // Throw an exception - throw new BrokenPipeException( - array( - 'this' => $this, - 'code' => socket_last_error() - ), self::EXCEPTION_PEER_SOCKET_BROKEN - ); - } - break; - } // END - if - } // END - foreach - } // END - if - return $data; - } - -} // END - class - -// [EOF] -?>