X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fclass_BaseHubSystem.php;h=a14384e1cfa5d9db6e036d168645a412dedb38c5;hb=fbea3b373356393e0146fee27d22caa1daf80f45;hp=ca002ebda6a727b153d792e9cfd32fed54f771f5;hpb=fd7231cc8372a5289c405fe7ad2f8227a86362fd;p=hub.git diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index ca002ebda..a14384e1c 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 - 2012 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,18 @@ */ 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 SOCKET_ERROR_CONNECTION_RESET_BY_PEER = 0x908; + + // Message status codes + const MESSAGE_STATUS_CODE_OKAY = 'OKAY'; /** * Separator for all bootstrap node entries @@ -65,6 +76,21 @@ class BaseHubSystem extends BaseFrameworkSystem { */ private $listenerPoolInstance = NULL; + /** + * Fragmenter instance + */ + private $fragmenterInstance = NULL; + + /** + * Decoder instance + */ + private $decoderInstance = NULL; + + /** + * Assembler instance + */ + private $assemblerInstance = NULL; + /** * Protected constructor * @@ -136,10 +162,10 @@ class BaseHubSystem extends BaseFrameworkSystem { /** * Setter for network package handler instance * - * @param $packageInstance The network package handler instance we shall set + * @param $packageInstance The network package instance we shall set * @return void */ - protected final function setPackageInstance (Networkable $packageInstance) { + protected final function setPackageInstance (Deliverable $packageInstance) { $this->packageInstance = $packageInstance; } @@ -193,7 +219,7 @@ class BaseHubSystem extends BaseFrameworkSystem { /** * Setter for listener pool instance * - * @param $listenerPoolInstance Our new listener pool instance + * @param $listenerPoolInstance The new listener pool instance * @return void */ protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) { @@ -209,6 +235,145 @@ class BaseHubSystem extends BaseFrameworkSystem { return $this->listenerPoolInstance; } + /** + * Setter for fragmenter instance + * + * @param $fragmenterInstance A Fragmentable instance + * @return void + */ + protected final function setFragmenterInstance (Fragmentable $fragmenterInstance) { + $this->fragmenterInstance = $fragmenterInstance; + } + + /** + * Getter for fragmenter instance + * + * @return $fragmenterInstance A Fragmentable instance + */ + protected final function getFragmenterInstance () { + return $this->fragmenterInstance; + } + + /** + * Setter for decoder instance + * + * @param $decoderInstance A Decodeable instance + * @return void + */ + protected final function setDecoderInstance (Decodeable $decoderInstance) { + $this->decoderInstance = $decoderInstance; + } + + /** + * Getter for decoder instance + * + * @return $decoderInstance A Decodeable instance + */ + protected final function getDecoderInstance () { + return $this->decoderInstance; + } + + /** + * Setter for assembler instance + * + * @param $assemblerInstance A Decodeable instance + * @return void + */ + protected final function setAssemblerInstance (Assembler $assemblerInstance) { + $this->assemblerInstance = $assemblerInstance; + } + + /** + * Getter for assembler instance + * + * @return $assemblerInstance A Decodeable instance + */ + protected final function getAssemblerInstance () { + return $this->assemblerInstance; + } + + /** + * Setter for node id + * + * @param $nodeId The new node id + * @return void + */ + protected final function setNodeId ($nodeId) { + // Set it config now + $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId); + } + + /** + * Getter for node id + * + * @return $nodeId Current node id + */ + public final function getNodeId () { + // Get it from config + return $this->getConfigInstance()->getConfigEntry('node_id'); + } + + /** + * Setter for private key + * + * @param $privateKey The new private key + * @return void + */ + protected final function setPrivateKey ($privateKey) { + // Set it config now + $this->getConfigInstance()->setConfigEntry('private_key', (string) $privateKey); + } + + /** + * Getter for private key + * + * @return $privateKey Current private key + */ + 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'); + } + /** * Constructs a callable method name from given socket error code. If the * method is not found, a generic one is used. @@ -218,10 +383,7 @@ class BaseHubSystem extends BaseFrameworkSystem { * @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 + // Create a name from translated error code $handlerName = 'socketError' . $this->convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; // Is the call-back method there? @@ -240,13 +402,15 @@ 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 * @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 $recipientData) { // This method handles only socket resources if (!is_resource($socketResource)) { // No resource, abort here @@ -269,12 +433,42 @@ 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, $recipientData)); // 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: */ self::createDebugInstance(__CLASS__)->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. @@ -295,6 +489,10 @@ class BaseHubSystem extends BaseFrameworkSystem { $errorName = BaseRawDataHandler::SOCKET_ERROR_RESOURCE_UNAVAILABLE; 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 +521,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('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode)); // Change it only in this class $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN; @@ -343,7 +541,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 +549,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 +565,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 +573,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); }