Updated 'core'.
[hub.git] / application / hub / main / factories / states / peer / class_PeerStateFactory.php
1 <?php
2 /**
3  * A factory class for peer states
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 PeerStateFactory extends ObjectFactory {
25         /**
26          * Static lookup table instance
27          */
28         private static $tableInstance = NULL;
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Singleton getter for lookup table instances, kept public if we need this
42          * table somewhere else.
43          *
44          * @return      $tableInstance  An instance of a lookup table
45          */
46         public static final function getTableInstance () {
47                 // Is the instance null?
48                 if (is_null(self::$tableInstance)) {
49                         // Get a new one
50                         self::$tableInstance = self::createObjectByConfiguredName('peer_state_lookup_db_wrapper_class');
51                 } // END - if
52
53                 // Return it
54                 return self::$tableInstance;
55         }
56
57         /**
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
60          * from current state.
61          *
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
67          */
68         public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
69                 // Init state instance, this is better coding practice
70                 $stateInstance = NULL;
71
72                 // So first we need our lookup table
73                 $tableInstance = self::getTableInstance();
74
75                 /*
76                  * Now try to purge old entries before looking an entry up. This shall
77                  * make it sure that only accurate entries can be found.
78                  */
79                 try {
80                         // Purge old entries
81                         $tableInstance->purgeOldEntriesBySocketResource($socketResource);
82                 } catch (InvalidSocketException $e) {
83                         // Just log all errors
84                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Purging of old entries failed. Message from exception: ' . $e->getMessage());
85                 }
86
87                 // Do we have an entry?
88                 if ($tableInstance->isSenderNewPeer($packageData)) {
89                         // Debug output
90                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] errorCode=' . $errorCode);
91
92                         // Register the new peer with its session id
93                         $tableInstance->registerPeerByPackageData($packageData, $socketResource);
94
95                         /*
96                          * It is a new peer so create the state instance based on error
97                          * code and get an instance from it.
98                          */
99                         $stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
100
101                         // And register it with the lookup table
102                         $tableInstance->registerPeerState($stateInstance, $packageData);
103                 } elseif ($tableInstance->isSamePeerState($helperInstance, $packageData)) {
104                         // Debug output
105                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state unchanged, re-generating old state ...');
106
107                         /*
108                          * The peer's state has noot changed, still we have to return a
109                          * state instance, so generate it here.
110                          */
111                         $stateInstance = self::createPeerStateInstanceByName($helperInstance->getPrintableState(), $helperInstance);
112                 } else {
113                         // Debug output
114                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Updating peer state ...');
115
116                         /*
117                          * It is an already known peer but with a changed state. So first
118                          * get an instance of the state.
119                          */
120                         $stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
121
122                         // The peer's state has changed, update database now
123                         $tableInstance->registerPeerState($stateInstance, $packageData);
124                 }
125
126                 // Debug message
127                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
128
129                 // Set the state in the helper
130                 $helperInstance->setStateInstance($stateInstance);
131
132                 // For any purposes, return the state instance
133                 return $stateInstance;
134         }
135
136         /**
137          * Creates an instance of a configurable peer state and sets it in the
138          * given peer instance.
139          *
140          * @param       $stateName                      Name of the state
141          * @param       $helperInstance         A ConnectionHelper class instance
142          * @return      $stateInstance          A Stateable class instance
143          */
144         public static final function createPeerStateInstanceByName ($stateName, ConnectionHelper $helperInstance) {
145                 // Get a class from a configuration entry
146                 $stateInstance = self::createObjectByConfiguredName('peer_' . $stateName . '_state_class', array($helperInstance));
147
148                 // Debug message
149                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
150
151                 // Once we have that state, set it in the peer instance
152                 $helperInstance->setStateInstance($stateInstance);
153
154                 // For any purposes, return the state instance
155                 return $stateInstance;
156         }
157 }
158
159 // [EOF]
160 ?>