* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 * 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 BaseHubSystem extends BaseFrameworkSystem { // Exception codes const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900; /** * Seperator for all bootstrap node entries */ const BOOTSTRAP_NODES_SEPERATOR = ';'; /** * An instance of a node */ private $nodeInstance = NULL; /** * An instance of a cruncher */ private $cruncherInstance = NULL; /** * Listener instance */ private $listenerInstance = NULL; /** * A network package handler instance */ private $packageInstance = NULL; /** * A Receivable instance */ private $receiverInstance = NULL; /** * State instance */ private $stateInstance = NULL; /** * Listener pool instance */ private $listenerPoolInstance = NULL; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Getter for node instance * * @return $nodeInstance An instance of a node node */ public final function getNodeInstance () { return $this->nodeInstance; } /** * Setter for node instance * * @param $nodeInstance An instance of a node node * @return void */ protected final function setNodeInstance (NodeHelper $nodeInstance) { $this->nodeInstance = $nodeInstance; } /** * 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; } /** * 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; } /** * Setter for network package handler instance * * @param $packageInstance The network package handler instance we shall set * @return void */ protected final function setPackageInstance (Networkable $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 Our 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; } /** * 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 $methodName 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->translateErrorCodeToName($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 $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) { // 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); // 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($this, $handlerName), $socketResource); // Finally clear the error because it has been handled socket_clear_error($socketResource); } /** * 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 translateErrorCodeToName ($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 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; 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)); // 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 $this->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'); // 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; } } // [EOF] ?>