]> git.mxchange.org Git - hub.git/blob - application/hub/main/resolver/state/network/class_NetworkStateResolver.php
'public static final' is the right thing, some variables renamed to make clear what...
[hub.git] / application / hub / main / resolver / state / network / class_NetworkStateResolver.php
1 <?php
2 /**
3  * A resolver for resolving states locally
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class NetworkStateResolver extends BaseStateResolver implements StateResolver {
25         /**
26          * Last successfull resolved state (name)
27          */
28         private $lastStateName = '';
29
30         /**
31          * Last successfull resolved state (instance)
32          */
33         private $lastStateInstance = null;
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Set prefix to 'Network'
45                 $this->setStatePrefix('Network');
46         }
47
48         /**
49          * Creates an instance of a resolver class with a given state
50          *
51          * @return      $resolverInstance       The prepared state resolver instance
52          */
53         public static final function createNetworkStateResolver () {
54                 // Create the new instance
55                 $resolverInstance = new NetworkStateResolver();
56
57                 // Return the prepared instance
58                 return $resolverInstance;
59         }
60
61         /**
62          * Returns an state instance for a given package class
63          *
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
68          * @todo        ~30% done
69          */
70         public function resolveStateByPackage (Networkable $packageInstance, array $packageData, $socketResource) {
71                 // Init state instance
72                 $stateInstance = null;
73
74                 // Get error code
75                 $errorCode = $packageInstance->getErrorCode();
76
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));
85
86                         // Change it only in this class
87                         $errorCode = BaseNetworkPackageHandler::SOCKET_ERROR_UNKNOWN;
88                 } // END - if
89
90                 // Create a state instance based on $errorCode. This factory does the hard work for us
91                 $stateInstance = PeerStateFactory::createPeerStateInstanceByErrorCode($errorCode, $packageData, $socketResource);
92
93                 // Return the prepared instance
94                 return $stateInstance;
95         }
96 }
97
98 // [EOF]
99 ?>