]> git.mxchange.org Git - hub.git/blob - application/hub/main/factories/states/peer/class_PeerStateFactory.php
Renamed a lot 'Peer' classes to 'Node' or 'Client' depending on what they are written for
[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@ship-simu.org>
6  * @version             0.0.0
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
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          * Creates a peer state instance based on errorCode if no entry is found in the lookup table
42          * for the peer given in $packageData 'sender' element or it changes the state if it differs
43          * from current state.
44          *
45          * @param       $errorCode                      The last error code
46          * @param       $packageData            Raw package data
47          * @param       $socketResource         A valid socket resource
48          * @return      $stateInstance          A Stateable class instance
49          */
50         public static final function createPeerStateInstanceByErrorCode ($errorCode, array $packageData, $socketResource) {
51                 // So first we need our lookup table
52                 $tableInstance = self::getTableInstance();
53
54                 // Purge old entries
55                 $tableInstance->purgeOldEntriesBySocketResource($socketResource);
56
57                 // Do we have an entry?
58                 if ($tableInstance->isSenderNewPeer($packageData)) {
59                         // Is a new peer so create the state instance based on error code, first we need a config entry
60                         $configEntry = 'peer_state_' . $errorCode . '_class';
61
62                         // Register the new peer with its session id
63                         $tableInstance->registerPeerByPackageData($packageData, $socketResource);
64
65                         // Then get it
66                         $stateInstance = ObjectFactory::createObjectByConfiguredName($configEntry);
67
68                         // And register it with the lookup table
69                         $tableInstance->registerPeerState($stateInstance, $packageData);
70                 } else {
71                         // It is a known peer, so we need to check if the state has changed
72                         die(__METHOD__ . ': Lookup!' . "\n");
73                 }
74
75                 // For any purposes, return the state instance
76                 return $stateInstance;
77         }
78
79         /**
80          * Singleton getter for lookup table instances, kept public if we need this
81          * table somewhere else.
82          *
83          * @return      $tableInstance  An instance of a lookup table
84          */
85         public static final function getTableInstance () {
86                 // Is the instance null?
87                 if (is_null(self::$tableInstance)) {
88                         // Get a new one
89                         self::$tableInstance = ObjectFactory::createObjectByConfiguredName('node_state_lookup_table_class');
90                 } // END - if
91
92                 // Return it
93                 return self::$tableInstance;
94         }
95 }
96
97 // [EOF]
98 ?>