* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @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 * 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 PeerStateResolver extends BaseStateResolver implements StateResolver { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Set prefix to 'Peer' $this->setStatePrefix('Peer'); } /** * Creates an instance of a resolver class with a given state * * @return $resolverInstance The prepared state resolver instance */ public static final function createPeerStateResolver () { // Create the new instance $resolverInstance = new PeerStateResolver(); // Return the prepared instance return $resolverInstance; } /** * Returns an state instance for a given raw package data and socket resource * * @param $helperInstance An instance of a ConnectionHelper class * @param $packageData Raw package data * @param $socketResource A valid socket resource * @return $stateInstance An instance of the resolved state * @throws InvalidSocketException If socketResource, even from getSocketResource() is no valid resource * @todo ~30% done */ public static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, $socketResource) { // Get temporary resolver instance $resolverInstance = self::createPeerStateResolver(); // Init state instance $stateInstance = NULL; // Is the socket resource valid? if (!is_resource($socketResource)) { // No, so get socket resource from helper $socketResource = $helperInstance->getSocketResource(); // Still no socket resource? if (!is_resource($socketResource)) { // Then abort here with an exception (may happen after socket_shutdown()) throw new InvalidSocketException(array($helperInstance, $socketResource, 'unknown', 'unknown'), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if } // END - if // Get error code from it $errorCode = socket_last_error($socketResource); // Translate the error code to an own name $errorCode = $helperInstance->translateSocketErrorCodeToName($errorCode); // Create a state instance based on $errorCode. This factory does the hard work for us $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageData, $socketResource, $errorCode); // Return the prepared instance return $stateInstance; } } // [EOF] ?>