* @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 NetworkStateResolver extends BaseStateResolver implements StateResolver { /** * Last successfull resolved state (name) */ private $lastStateName = ''; /** * Last successfull resolved state (instance) */ private $lastStateInstance = null; /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Set prefix to 'Network' $this->setStatePrefix('Network'); } /** * Creates an instance of a resolver class with a given state * * @return $resolverInstance The prepared state resolver instance */ public static final function createNetworkStateResolver () { // Create the new instance $resolverInstance = new NetworkStateResolver(); // Return the prepared instance return $resolverInstance; } /** * Returns an state instance for a given package class * * @param $packageInstance An instance of a package class * @param $packageData Raw package data * @param $socketResource A valid socket resource * @return $stateInstance An instance of the resolved state * @todo ~30% done */ public function resolveStateByPackage (Networkable $packageInstance, array $packageData, $socketResource) { // Init state instance $stateInstance = null; // Get error code $errorCode = $packageInstance->getErrorCode(); // Is the code a number, then we have to change it if (($errorCode == 134) || ($errorCode == 107)) { // Transport endpoint not connected, should be handled else! // @TODO On some systems it is 134, on some 107? $errorCode = BaseNetworkPackageHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT; } elseif (is_int($errorCode)) { // 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 $errorCode = BaseNetworkPackageHandler::SOCKET_ERROR_UNKNOWN; } // END - if // Create a state instance based on $errorCode. This factory does the hard work for us $stateInstance = PeerStateFactory::createPeerStateInstanceByErrorCode($errorCode, $packageData, $socketResource); // Return the prepared instance return $stateInstance; } } // [EOF] ?>