X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fclass_BaseHubSystem.php;h=77bf1c3456bf5096e76cb203d92354e44a250413;hb=8d9a1956c3b9e27e37ba12810b165f41e7643196;hp=b0abb8c4ae30f9da24d95b953069e97e1b8366e5;hpb=d7696647c359442afca020b45dacc4c1a0f1dc18;p=hub.git diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index b0abb8c4a..77bf1c345 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -2,11 +2,11 @@ /** * A general hub system class * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.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 @@ -23,7 +23,21 @@ */ class BaseHubSystem extends BaseFrameworkSystem { // Exception codes - const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900; + const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900; + const EXCEPTION_CHUNK_ALREADY_ASSEMBLED = 0x901; + const EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED = 0x902; + const EXCEPTION_INVALID_CONNECTION_TYPE = 0x903; + const EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED = 0x904; + const EXCEPTION_BASE64_ENCODING_NOT_MODULO_4 = 0x905; + const EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING = 0x906; + const EXCEPTION_REQUEST_NOT_ACCEPTED = 0x907; + const EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED = 0x908; + const EXCEPTION_MULTIPLE_MESSAGE_SENT = 0x909; + const EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED = 0x90a; + const EXCEPTION_INVALID_UNL = 0x90b; + + // Message status codes + const MESSAGE_STATUS_CODE_OKAY = 'OKAY'; /** * Separator for all bootstrap node entries @@ -36,34 +50,34 @@ class BaseHubSystem extends BaseFrameworkSystem { private $nodeInstance = NULL; /** - * An instance of a cruncher + * A network package handler instance */ - private $cruncherInstance = NULL; + private $packageInstance = NULL; /** - * Listener instance + * A Receivable instance */ - private $listenerInstance = NULL; + private $receiverInstance = NULL; /** - * A network package handler instance + * Listener pool instance */ - private $packageInstance = NULL; + private $listenerPoolInstance = NULL; /** - * A Receivable instance + * Fragmenter instance */ - private $receiverInstance = NULL; + private $fragmenterInstance = NULL; /** - * State instance + * Assembler instance */ - private $stateInstance = NULL; + private $assemblerInstance = NULL; /** - * Listener pool instance + * Info instance */ - private $listenerPoolInstance = NULL; + private $infoInstance = NULL; /** * Protected constructor @@ -77,136 +91,199 @@ class BaseHubSystem extends BaseFrameworkSystem { } /** - * Getter for node instance + * Setter for network package handler instance * - * @return $nodeInstance An instance of a node node + * @param $packageInstance The network package instance we shall set + * @return void */ - public final function getNodeInstance () { - return $this->nodeInstance; + protected final function setPackageInstance (Deliverable $packageInstance) { + $this->packageInstance = $packageInstance; } /** - * Setter for node instance + * Getter for network package handler instance * - * @param $nodeInstance An instance of a node node + * @return $packageInstance The network package handler instance we shall set + */ + protected final function getPackageInstance () { + return $this->packageInstance; + } + + /** + * Setter for receiver instance + * + * @param $receiverInstance A Receivable instance we shall set * @return void */ - protected final function setNodeInstance (NodeHelper $nodeInstance) { - $this->nodeInstance = $nodeInstance; + protected final function setReceiverInstance (Receivable $receiverInstance) { + $this->receiverInstance = $receiverInstance; } /** - * Getter for cruncher instance + * Getter for receiver instance * - * @return $cruncherInstance An instance of a cruncher cruncher + * @return $receiverInstance A Receivable instance we shall get */ - public final function getCruncherInstance () { - return $this->cruncherInstance; + protected final function getReceiverInstance () { + return $this->receiverInstance; } /** - * Setter for cruncher instance + * Setter for listener pool instance * - * @param $cruncherInstance An instance of a cruncher cruncher + * @param $listenerPoolInstance The new listener pool instance * @return void */ - protected final function setCruncherInstance (CruncherHelper $cruncherInstance) { - $this->cruncherInstance = $cruncherInstance; + protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) { + $this->listenerPoolInstance = $listenerPoolInstance; } /** - * Setter for listener instance + * Getter for listener pool instance * - * @param $listenerInstance A Listenable instance + * @return $listenerPoolInstance Our current listener pool instance + */ + public final function getListenerPoolInstance () { + return $this->listenerPoolInstance; + } + + /** + * Setter for fragmenter instance + * + * @param $fragmenterInstance A Fragmentable instance * @return void */ - protected final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; + protected final function setFragmenterInstance (Fragmentable $fragmenterInstance) { + $this->fragmenterInstance = $fragmenterInstance; } /** - * Getter for listener instance + * Getter for fragmenter instance * - * @return $listenerInstance A Listenable instance + * @return $fragmenterInstance A Fragmentable instance */ - protected final function getListenerInstance () { - return $this->listenerInstance; + protected final function getFragmenterInstance () { + return $this->fragmenterInstance; } /** - * Setter for network package handler instance + * Setter for assembler instance * - * @param $packageInstance The network package handler instance we shall set + * @param $assemblerInstance An instance of an Assembler class * @return void */ - protected final function setPackageInstance (Networkable $packageInstance) { - $this->packageInstance = $packageInstance; + protected final function setAssemblerInstance (Assembler $assemblerInstance) { + $this->assemblerInstance = $assemblerInstance; } /** - * Getter for network package handler instance + * Getter for assembler instance * - * @return $packageInstance The network package handler instance we shall set + * @return $assemblerInstance An instance of an Assembler class */ - protected final function getPackageInstance () { - return $this->packageInstance; + protected final function getAssemblerInstance () { + return $this->assemblerInstance; } /** - * Setter for receiver instance + * Setter for info instance * - * @param $receiverInstance A Receivable instance we shall set + * @param $infoInstance A ShareableInfo instance * @return void */ - protected final function setReceiverInstance (Receivable $receiverInstance) { - $this->receiverInstance = $receiverInstance; + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; } /** - * Getter for receiver instance + * Getter for info instance * - * @return $receiverInstance A Receivable instance we shall get + * @return $infoInstance An instance of a ShareableInfo class */ - protected final function getReceiverInstance () { - return $this->receiverInstance; + public final function getInfoInstance () { + return $this->infoInstance; } /** - * Setter for state instance + * Setter for node id * - * @param $stateInstance A Stateable instance + * @param $nodeId The new node id * @return void */ - public final function setStateInstance (Stateable $stateInstance) { - $this->stateInstance = $stateInstance; + protected final function setNodeId ($nodeId) { + // Set it config now + $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId); } /** - * Getter for state instance + * Getter for node id * - * @return $stateInstance A Stateable instance + * @return $nodeId Current node id */ - public final function getStateInstance () { - return $this->stateInstance; + public final function getNodeId () { + // Get it from config + return $this->getConfigInstance()->getConfigEntry('node_id'); } /** - * Setter for listener pool instance + * Setter for private key * - * @param $listenerPoolInstance Our new listener pool instance + * @param $privateKey The new private key * @return void */ - protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) { - $this->listenerPoolInstance = $listenerPoolInstance; + protected final function setPrivateKey ($privateKey) { + // Set it config now + $this->getConfigInstance()->setConfigEntry('private_key', (string) $privateKey); } /** - * Getter for listener pool instance + * Getter for private key * - * @return $listenerPoolInstance Our current listener pool instance + * @return $privateKey Current private key */ - public final function getListenerPoolInstance () { - return $this->listenerPoolInstance; + public final function getPrivateKey () { + // Get it from config + return $this->getConfigInstance()->getConfigEntry('private_key'); + } + + /** + * Setter for private key hash + * + * @param $privateKeyHash The new private key hash + * @return void + */ + protected final function setPrivateKeyHash ($privateKeyHash) { + // Set it config now + $this->getConfigInstance()->setConfigEntry('private_key_hash', (string) $privateKeyHash); + } + + /** + * Getter for private key hash + * + * @return $privateKeyHash Current private key hash + */ + public final function getPrivateKeyHash () { + // Get it from config + return $this->getConfigInstance()->getConfigEntry('private_key_hash'); + } + + /** + * Setter for session id + * + * @param $sessionId The new session id + * @return void + */ + protected final function setSessionId ($sessionId) { + $this->getConfigInstance()->setConfigEntry('session_id', (string) $sessionId); + } + + /** + * Getter for session id + * + * @return $sessionId Current session id + */ + public final function getSessionId () { + return $this->getConfigInstance()->getConfigEntry('session_id'); } /** @@ -214,15 +291,12 @@ class BaseHubSystem extends BaseFrameworkSystem { * method is not found, a generic one is used. * * @param $errorCode Error code from socket_last_error() - * @return $methodName Call-back method name for the error handler + * @return $handlerName Call-back method name for the error handler * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented */ protected function getSocketErrorHandlerFromCode ($errorCode) { - // Set NULL, so everyone is forced to implement socket error handlers - $handlerName = NULL; - - // Temporary create a possible name from translated error code - $handlerName = 'socketError' . $this->convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; + // Create a name from translated error code + $handlerName = 'socketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; // Is the call-back method there? if (!method_exists($this, $handlerName)) { @@ -240,21 +314,26 @@ class BaseHubSystem extends BaseFrameworkSystem { * but assumes valid data in array $recipientData, except that * count($recipientData) is always 2. * + * @param $method Value of __METHOD__ from calling method + * @param $line Value of __LINE__ from calling method * @param $socketResource A valid socket resource - * @param $recipientData An array with two elements: 0=IP number, 1=port number + * @param $unlData A valid UNL data array * @return void * @throws InvalidSocketException If $socketResource is no socket resource * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back */ - protected final function handleSocketError ($socketResource, array $recipientData) { + protected final function handleSocketError ($method, $line, $socketResource, array $unlData) { // This method handles only socket resources if (!is_resource($socketResource)) { // No resource, abort here throw new InvalidSocketException(array($this, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if - // Check count of array, should be two - assert(count($recipientData) == 2); + // Check UNL array + //* DEBUG-DIE: */ die(__METHOD__ . ':unlData=' . print_r($unlData, TRUE)); + assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); + assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS])); + assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT])); // Get error code for first validation (0 is not an error) $errorCode = socket_last_error($socketResource); @@ -269,7 +348,7 @@ class BaseHubSystem extends BaseFrameworkSystem { $handlerName = $this->getSocketErrorHandlerFromCode($errorCode); // Call-back the error handler method - call_user_func(array($this, $handlerName), $socketResource); + call_user_func_array(array($this, $handlerName), array($socketResource, $unlData)); // Finally clear the error because it has been handled socket_clear_error($socketResource); @@ -295,6 +374,14 @@ class BaseHubSystem extends BaseFrameworkSystem { $errorName = BaseRawDataHandler::SOCKET_ERROR_RESOURCE_UNAVAILABLE; break; + case 32: // "Broken pipe" + $errorName = BaseRawDataHandler::SOCKET_ERROR_BROKEN_PIPE; + break; + + case 104: // "Connection reset by peer" + $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_RESET_BY_PEER; + break; + case 107: // "Transport end-point not connected" case 134: // On some (?) systems for 'transport end-point not connected' // @TODO On some systems it is 134, on some 107? @@ -323,7 +410,7 @@ class BaseHubSystem extends BaseFrameworkSystem { default: // Everything else <> 0 // Unhandled error code detected, so first debug it because we may want to handle it like the others - $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode)); + self::createDebugInstance(__CLASS__)->debugOutput('BASE-HUB[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode)); // Change it only in this class $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN; @@ -343,7 +430,7 @@ class BaseHubSystem extends BaseFrameworkSystem { */ public function shutdownSocket ($socketResource) { // Debug message - $this->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); // Set socket resource $this->setSocketResource($socketResource); @@ -351,6 +438,9 @@ class BaseHubSystem extends BaseFrameworkSystem { // Get a visitor instance $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class'); + // Debug output + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); + // Call the visitor $this->accept($visitorInstance); } @@ -364,7 +454,7 @@ class BaseHubSystem extends BaseFrameworkSystem { */ public function halfShutdownSocket ($socketResource) { // Debug message - $this->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); // Set socket resource $this->setSocketResource($socketResource); @@ -372,6 +462,9 @@ class BaseHubSystem extends BaseFrameworkSystem { // Get a visitor instance $visitorInstance = ObjectFactory::createObjectByConfiguredName('half_shutdown_socket_visitor_class'); + // Debug output + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); + // Call the visitor $this->accept($visitorInstance); }