3 * A resolver for resolving states locally
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class NetworkStateResolver extends BaseStateResolver implements StateResolver {
26 * Last successfull resolved state (name)
28 private $lastStateName = '';
31 * Last successfull resolved state (instance)
33 private $lastStateInstance = null;
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
44 // Set prefix to 'Network'
45 $this->setStatePrefix('Network');
49 * Creates an instance of a resolver class with a given state
51 * @return $resolverInstance The prepared state resolver instance
53 public static final function createNetworkStateResolver () {
54 // Create the new instance
55 $resolverInstance = new NetworkStateResolver();
57 // Return the prepared instance
58 return $resolverInstance;
62 * Returns an state instance for a given package class
64 * @param $packageInstance An instance of a package class
65 * @param $packageData Raw package data
66 * @param $socketResource A valid socket resource
67 * @return $stateInstance An instance of the resolved state
70 public function resolveStateByPackage (Networkable $packageInstance, array $packageData, $socketResource) {
71 // Init state instance
72 $stateInstance = null;
75 $errorCode = $packageInstance->getErrorCode();
77 // Is the code a number, then we have to change it
78 if (($errorCode == 134) || ($errorCode == 107)) {
79 // Transport endpoint not connected, should be handled else!
80 // @TODO On some systems it is 134, on some 107?
81 $errorCode = BaseNetworkPackageHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT;
82 } elseif (is_int($errorCode)) {
83 // Debug output (because we might want to handle it like the above(s)
84 $this->debugOutput(__METHOD__ . ': UNKNOWN ERROR CODE = ' . $errorCode, ', MESSAGE = ' . socket_strerror($errorCode));
86 // Change it only in this class
87 $errorCode = BaseNetworkPackageHandler::SOCKET_ERROR_UNKNOWN;
90 // Create a state instance based on $errorCode. This factory does the hard work for us
91 $stateInstance = PeerStateFactory::createPeerStateInstanceByErrorCode($errorCode, $packageData, $socketResource);
93 // Return the prepared instance
94 return $stateInstance;