3 * A factory class for peer states
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 PeerStateFactory extends ObjectFactory {
26 * Static lookup table instance
28 private static $tableInstance = NULL;
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Singleton getter for lookup table instances, kept public if we need this
42 * table somewhere else.
44 * @return $tableInstance An instance of a lookup table
46 public static final function getTableInstance () {
47 // Is the instance null?
48 if (is_null(self::$tableInstance)) {
50 self::$tableInstance = self::createObjectByConfiguredName('peer_state_lookup_db_wrapper_class');
54 return self::$tableInstance;
58 * Creates a peer state instance based on errorCode if no entry is found in the lookup table
59 * for the peer given in $packageData 'sender' element or it changes the state if it differs
62 * @param $helperInstance An instance of a ConnectionHelper class
63 * @param $packageData Raw package data
64 * @param $socketResource A valid socket resource
65 * @param $errorCode The last error code
66 * @return $stateInstance A Stateable class instance
68 public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
69 // So first we need our lookup table
70 $tableInstance = self::getTableInstance();
74 $tableInstance->purgeOldEntriesBySocketResource($socketResource);
75 } catch (InvalidSocketException $e) {
76 // Just log all errors
77 $tableInstance->debugOutput('PEER-STATE-FACTORY: Purging of old entries failed. Message from exception: ' . $e->getMessage());
80 // Do we have an entry?
81 if ($tableInstance->isSenderNewPeer($packageData)) {
83 $tableInstance->debugOutput('PEER-STATE-FACTORY: errorCode=' . $errorCode);
85 // Register the new peer with its session id
86 $tableInstance->registerPeerByPackageData($packageData, $socketResource);
89 * It is a new peer so create the state instance based on error
90 * code and get an instance from it.
92 $stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
94 // And register it with the lookup table
95 $tableInstance->registerPeerState($stateInstance, $packageData);
97 // It is a known peer, so we need to check if the state has changed
98 $tableInstance->debugBackTrace(__METHOD__ . ': Lookup!' . "\n");
102 $stateInstance->debugOutput('PEER-STATE[' . __LINE__ . ']: Has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.');
104 // Set the state in the helper
105 $helperInstance->setStateInstance($stateInstance);
107 // For any purposes, return the state instance
108 return $stateInstance;
112 * Creates an instance of a configurable peer state and sets it in the
113 * given peer instance.
115 * @param $stateName Name of the state
116 * @param $helperInstance A ConnectionHelper class instance
117 * @return $stateInstance A Stateable class instance
119 public static final function createPeerStateInstanceByName ($stateName, ConnectionHelper $helperInstance) {
120 // Then construct the class' configuraton entry
121 $className = 'peer_' . $stateName . '_state_class';
123 // Get a class from that configuration entry
124 $stateInstance = self::createObjectByConfiguredName($className, array($helperInstance));
127 $stateInstance->debugOutput('PEER-STATE[' . __LINE__ . ']: Has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
129 // Once we have that state, set it in the peer instance
130 $helperInstance->setStateInstance($stateInstance);
132 // For any purposes, return the state instance
133 return $stateInstance;