X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fclass_BaseHubSystem.php;h=507d4855d5a26b2d1948a5a537ae40efe306aff3;hb=86642f37458f91f78f50b5c7f063e737a5c478ce;hp=75ce417ac20c73e31ac9f13d4737280e2c73d5db;hpb=c398e32345a4b8a025682e4cc9c215079b3da24c;p=hub.git diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index 75ce417ac..507d4855d 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 - 2012 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,11 +23,18 @@ */ class BaseHubSystem extends BaseFrameworkSystem { // Exception codes - 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_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'; @@ -43,14 +50,9 @@ class BaseHubSystem extends BaseFrameworkSystem { private $nodeInstance = NULL; /** - * An instance of a cruncher + * An instance of a communicator */ - private $cruncherInstance = NULL; - - /** - * Listener instance - */ - private $listenerInstance = NULL; + private $communicatorInstance = NULL; /** * A network package handler instance @@ -78,14 +80,19 @@ class BaseHubSystem extends BaseFrameworkSystem { private $fragmenterInstance = NULL; /** - * Decoder instance + * Assembler instance */ - private $decoderInstance = NULL; + private $assemblerInstance = NULL; /** - * Assembler instance + * Info instance */ - private $assemblerInstance = NULL; + private $infoInstance = NULL; + + /** + * Name of used protocol + */ + private $protocolName = 'invalid'; /** * Protected constructor @@ -118,41 +125,22 @@ class BaseHubSystem extends BaseFrameworkSystem { } /** - * Getter for cruncher instance + * Getter for communicator instance * - * @return $cruncherInstance An instance of a cruncher cruncher + * @return $communicatorInstance An instance of a Communicator class */ - public final function getCruncherInstance () { - return $this->cruncherInstance; + public final function getCommunicatorInstance () { + return $this->communicatorInstance; } /** - * Setter for cruncher instance + * Setter for communicator instance * - * @param $cruncherInstance An instance of a cruncher cruncher + * @param $communicatorInstance An instance of a Communicator class * @return void */ - protected final function setCruncherInstance (CruncherHelper $cruncherInstance) { - $this->cruncherInstance = $cruncherInstance; - } - - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - protected final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - protected final function getListenerInstance () { - return $this->listenerInstance; + protected final function setCommunicatorInstance (Communicator $communicatorInstance) { + $this->communicatorInstance = $communicatorInstance; } /** @@ -251,41 +239,41 @@ class BaseHubSystem extends BaseFrameworkSystem { } /** - * Setter for decoder instance + * Setter for assembler instance * - * @param $decoderInstance A Decodeable instance + * @param $assemblerInstance An instance of an Assembler class * @return void */ - protected final function setDecoderInstance (Decodeable $decoderInstance) { - $this->decoderInstance = $decoderInstance; + protected final function setAssemblerInstance (Assembler $assemblerInstance) { + $this->assemblerInstance = $assemblerInstance; } /** - * Getter for decoder instance + * Getter for assembler instance * - * @return $decoderInstance A Decodeable instance + * @return $assemblerInstance An instance of an Assembler class */ - protected final function getDecoderInstance () { - return $this->decoderInstance; + protected final function getAssemblerInstance () { + return $this->assemblerInstance; } /** - * Setter for assembler instance + * Setter for info instance * - * @param $assemblerInstance A Decodeable instance + * @param $infoInstance A ShareableInfo instance * @return void */ - protected final function setAssemblerInstance (Assembler $assemblerInstance) { - $this->assemblerInstance = $assemblerInstance; + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; } /** - * Getter for assembler instance + * Getter for info instance * - * @return $assemblerInstance A Decodeable instance + * @return $infoInstance An instance of a ShareableInfo class */ - protected final function getAssemblerInstance () { - return $this->assemblerInstance; + public final function getInfoInstance () { + return $this->infoInstance; } /** @@ -354,7 +342,7 @@ class BaseHubSystem extends BaseFrameworkSystem { /** * Setter for session id * - * @param $sessionId The new session id + * @param $sessionId The new session id * @return void */ protected final function setSessionId ($sessionId) { @@ -364,12 +352,31 @@ class BaseHubSystem extends BaseFrameworkSystem { /** * Getter for session id * - * @return $sessionId Current session id + * @return $sessionId Current session id */ public final function getSessionId () { return $this->getConfigInstance()->getConfigEntry('session_id'); } + /** + * Getter for protocol name + * + * @return $protocolName Name of used protocol + */ + public final function getProtocolName () { + return $this->protocolName; + } + + /** + * Setter for protocol name + * + * @param $protocolName Name of used protocol + * @return void + */ + protected final function setProtocolName ($protocolName) { + $this->protocolName = $protocolName; + } + /** * Constructs a callable method name from given socket error code. If the * method is not found, a generic one is used. @@ -399,22 +406,25 @@ class BaseHubSystem extends BaseFrameworkSystem { * count($recipientData) is always 2. * * @param $method Value of __METHOD__ from calling method - * @param $line Source code line where this method was called + * @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 ($method, $line, $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); @@ -429,42 +439,12 @@ 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); } - /** - * Checks whether the final (last) chunk is valid - * - * @param $chunks An array with chunks and (hopefully) a valid final chunk - * @return $isValid Whether the final (last) chunk is valid - */ - protected function isValidFinalChunk (array $chunks) { - // Default is all fine - $isValid = true; - - // Split the (possible) EOP chunk - $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]); - - // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler) - //* NOISY-DEBUG: */ $this->debugOutput('eopChunk=' . $chunks[count($chunks) - 1] . ',chunkSplits=' . print_r($chunkSplits,true)); - assert(count($chunkSplits) == 3); - - // Validate final chunk - if (substr($chunkSplits[ChunkHandler::CHUNK_SPLITS_INDEX_RAW_DATA], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) { - // Not fine - $isValid = false; - } elseif (substr_count($chunkSplits[ChunkHandler::CHUNK_SPLITS_INDEX_RAW_DATA], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) { - // CHUNK_HASH_SEPARATOR shall only be found once - $isValid = false; - } - - // Return status - return $isValid; - } - /** * Translates socket error codes into our own internal names which can be * used for call-backs. @@ -485,6 +465,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? @@ -513,7 +501,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; @@ -533,7 +521,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); @@ -542,7 +530,7 @@ class BaseHubSystem extends BaseFrameworkSystem { $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class'); // Debug output - $this->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); // Call the visitor $this->accept($visitorInstance); @@ -557,7 +545,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); @@ -566,7 +554,7 @@ class BaseHubSystem extends BaseFrameworkSystem { $visitorInstance = ObjectFactory::createObjectByConfiguredName('half_shutdown_socket_visitor_class'); // Debug output - $this->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString()); // Call the visitor $this->accept($visitorInstance);