X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fclass_BaseHubSystem.php;h=885abc5d08b959e66b8611b317fd8cfba71a7e14;hb=424faec1c51eb20cadc5149777f08fd1befa4fd0;hp=deee535e78deb15942b36067b103f3717c456f51;hpb=b1b77d5d19b8cd81457c19d885baa949652f2c68;p=hub.git diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index deee535e7..885abc5d0 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 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 @@ -22,15 +22,97 @@ * along with this program. If not, see . */ 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_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 + */ + const BOOTSTRAP_NODES_SEPARATOR = ';'; + /** * An instance of a node */ - private $nodeInstance = null; + private $nodeInstance = NULL; + + /** + * An instance of a communicator + */ + private $communicatorInstance = NULL; + + /** + * An instance of a crawler + */ + private $crawlerInstance = NULL; + + /** + * An instance of a cruncher + */ + private $cruncherInstance = NULL; + + /** + * An instance of a miner + */ + private $minerInstance = NULL; + + /** + * A network package handler instance + */ + private $packageInstance = NULL; /** - * Listener instance + * A Receivable instance */ - private $listenerInstance = null; + private $receiverInstance = NULL; + + /** + * State instance + */ + private $stateInstance = NULL; + + /** + * Listener pool instance + */ + private $listenerPoolInstance = NULL; + + /** + * Fragmenter instance + */ + private $fragmenterInstance = NULL; + + /** + * Decoder instance + */ + private $decoderInstance = NULL; + + /** + * Assembler instance + */ + private $assemblerInstance = NULL; + + /** + * Info instance + */ + private $infoInstance = NULL; + + /** + * Name of used protocol + */ + private $protocolName = 'invalid'; /** * Protected constructor @@ -41,48 +123,589 @@ class BaseHubSystem extends BaseFrameworkSystem { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + } - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); + /** + * Getter for node instance + * + * @return $nodeInstance An instance of a node node + */ + public final function getNodeInstance () { + return $this->nodeInstance; } /** - * Setter for listener instance + * Setter for node instance * - * @param $listenerInstance A Listenable instance + * @param $nodeInstance An instance of a node node * @return void */ - protected final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; + protected final function setNodeInstance (NodeHelper $nodeInstance) { + $this->nodeInstance = $nodeInstance; } /** - * Getter for listener instance + * Getter for communicator instance * - * @return $listenerInstance A Listenable instance + * @return $communicatorInstance An instance of a Communicator class */ - protected final function getListenerInstance () { - return $this->listenerInstance; + public final function getCommunicatorInstance () { + return $this->communicatorInstance; } /** - * Setter for node instance + * Setter for communicator instance * - * @param $nodeInstance An instance of a node node + * @param $communicatorInstance An instance of a Communicator class * @return void */ - protected final function setNodeInstance (NodeHelper $nodeInstance) { - $this->nodeInstance = $nodeInstance; + protected final function setCommunicatorInstance (Communicator $communicatorInstance) { + $this->communicatorInstance = $communicatorInstance; } /** - * Getter for node instance + * Getter for crawler instance * - * @return $nodeInstance An instance of a node node + * @return $crawlerInstance An instance of a Crawler class */ - public final function getNodeInstance () { - return $this->nodeInstance; + public final function getCrawlerInstance () { + return $this->crawlerInstance; + } + + /** + * Setter for crawler instance + * + * @param $crawlerInstance An instance of a Crawler class + * @return void + */ + protected final function setCrawlerInstance (Crawler $crawlerInstance) { + $this->crawlerInstance = $crawlerInstance; + } + + /** + * Getter for cruncher instance + * + * @return $cruncherInstance An instance of a cruncher cruncher + */ + public final function getCruncherInstance () { + return $this->cruncherInstance; + } + + /** + * Setter for cruncher instance + * + * @param $cruncherInstance An instance of a cruncher cruncher + * @return void + */ + protected final function setCruncherInstance (CruncherHelper $cruncherInstance) { + $this->cruncherInstance = $cruncherInstance; + } + + /** + * Getter for miner instance + * + * @return $minerInstance An instance of a miner miner + */ + public final function getMinerInstance () { + return $this->minerInstance; + } + + /** + * Setter for miner instance + * + * @param $minerInstance An instance of a miner miner + * @return void + */ + protected final function setMinerInstance (MinerHelper $minerInstance) { + $this->minerInstance = $minerInstance; + } + + /** + * Setter for network package handler instance + * + * @param $packageInstance The network package instance we shall set + * @return void + */ + protected final function setPackageInstance (Deliverable $packageInstance) { + $this->packageInstance = $packageInstance; + } + + /** + * Getter for network package handler instance + * + * @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 setReceiverInstance (Receivable $receiverInstance) { + $this->receiverInstance = $receiverInstance; + } + + /** + * Getter for receiver instance + * + * @return $receiverInstance A Receivable instance we shall get + */ + protected final function getReceiverInstance () { + return $this->receiverInstance; + } + + /** + * Setter for state instance + * + * @param $stateInstance A Stateable instance + * @return void + */ + public final function setStateInstance (Stateable $stateInstance) { + $this->stateInstance = $stateInstance; + } + + /** + * Getter for state instance + * + * @return $stateInstance A Stateable instance + */ + public final function getStateInstance () { + return $this->stateInstance; + } + + /** + * Setter for listener pool instance + * + * @param $listenerPoolInstance The new listener pool instance + * @return void + */ + protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) { + $this->listenerPoolInstance = $listenerPoolInstance; + } + + /** + * Getter for listener pool 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 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 info instance + * + * @param $infoInstance A ShareableInfo instance + * @return void + */ + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; + } + + /** + * Getter for info instance + * + * @return $infoInstance A Decodeable instance + */ + public final function getInfoInstance () { + return $this->infoInstance; + } + + /** + * 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'); + } + + /** + * 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. + * + * @param $errorCode Error code from socket_last_error() + * @return $handlerName Call-back method name for the error handler + * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented + */ + protected function getSocketErrorHandlerFromCode ($errorCode) { + // Create a name from translated error code + $handlerName = 'socketError' . $this->convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; + + // Is the call-back method there? + if (!method_exists($this, $handlerName)) { + // Please implement this + throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), self::EXCEPTION_UNSUPPORTED_ERROR_HANDLER); + } // END - if + + // Return it + return $handlerName; + } + + /** + * Handles socket error for given socket resource and peer data. This method + * validates $socketResource if it is a valid resource (see is_resource()) + * 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 $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 $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 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); + + // If the error code is zero, someone called this method without an error + if ($errorCode == 0) { + // No error detected (or previously cleared outside this method) + throw new NoSocketErrorDetectedException(array($this, $socketResource), BaseListener::EXCEPTION_NO_SOCKET_ERROR); + } // END - if + + // Get handler (method) name + $handlerName = $this->getSocketErrorHandlerFromCode($errorCode); + + // Call-back the error handler method + 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: */ 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. + * + * @param $errorCode The error code from socket_last_error() to be translated + * @return $errorName The translated name (all lower-case, with underlines) + */ + public function translateSocketErrorCodeToName ($errorCode) { + // Nothing bad happened by default + $errorName = BaseRawDataHandler::SOCKET_CONNECTED; + + // Is the code a number, then we have to change it + switch ($errorCode) { + case 0: // Silently ignored, the socket is connected + break; + + case 11: // "Resource temporary unavailable" + $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? + $errorName = BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT; + break; + + case 110: // "Connection timed out" + $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_TIMED_OUT; + break; + + case 111: // "Connection refused" + $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED; + break; + + case 113: // "No route to host" + $errorName = BaseRawDataHandler::SOCKET_ERROR_NO_ROUTE_TO_HOST; + break; + + case 114: // "Operation already in progress" + $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS; + break; + + case 115: // "Operation now in progress" + $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_IN_PROGRESS; + break; + + default: // Everything else <> 0 + // Unhandled error code detected, so first debug it because we may want to handle it like the others + 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; + break; + } + + // Return translated name + return $errorName; + } + + /** + * Shuts down a given socket resource. This method does only ease calling + * the right visitor. + * + * @param $socketResource A valid socket resource + * @return void + */ + public function shutdownSocket ($socketResource) { + // Debug message + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); + + // Set socket resource + $this->setSocketResource($socketResource); + + // 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); + } + + /** + * Half-shuts down a given socket resource. This method does only ease calling + * an other visitor than shutdownSocket() does. + * + * @param $socketResource A valid socket resource + * @return void + */ + public function halfShutdownSocket ($socketResource) { + // Debug message + self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...'); + + // Set socket resource + $this->setSocketResource($socketResource); + + // 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); + } + + /** + * "Getter" for a printable state name + * + * @return $stateName Name of the node's state in a printable format + */ + public final function getPrintableState () { + // Default is 'null' + $stateName = 'null'; + + // Get the state instance + $stateInstance = $this->getStateInstance(); + + // Is it an instance of Stateable? + if ($stateInstance instanceof Stateable) { + // Then use that state name + $stateName = $stateInstance->getStateName(); + } // END - if + + // Return result + return $stateName; } }