SocketRegistry basicly finished:
[core.git] / inc / classes / main / registry / socket / class_SocketRegistry.php
1 <?php
2 /**
3  * A Socket registry
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core 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 SocketRegistry extends BaseRegistry implements Register, RegisterableSocket {
25         // Exception constants
26         const SOCKET_NOT_REGISTERED = 0xd00;
27
28         /**
29          * Instance of this class
30          */
31         private static $registryInstance = null;
32
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Singleton getter for self instance. This class has no factory pattern
45          * because here is no need for special parameters.
46          *
47          * @return      $registryInstance       Instance of this class
48          */
49         public final static function createSocketRegistry () {
50                 // Is an instance there?
51                 if (is_null(self::$registryInstance)) {
52                         // Not yet, so create one
53                         self::$registryInstance = new SocketRegistry();
54                 } // END - if
55
56                 // Return the instance
57                 return self::$registryInstance;
58         }
59
60         /**
61          * "Getter" to get a string respresentation for a key for the sub-registry
62          * in this format: protocol:port
63          *
64          * @param       $protocolInstance       An instance of a ProtocolHandler class
65          * @return      $key                            A string representation of the socket for the registry
66          */
67         private function getSubRegistryKey (ProtocolHandler $protocolInstance) {
68                 // Get protocol and port number and add both together
69                 $key = sprintf("%s:%s",
70                         $protocolInstance->getProtocol(),
71                         $protocolInstance->getPort()
72                 );
73
74                 // Return resulting key
75                 return $key;
76         }
77
78         /**
79          * "Getter" to get a string respresentation of the protocol
80          *
81          * @param       $protocolInstance       An instance of a ProtocolHandler class
82          * @return      $key                            A string representation of the protocol for the registry
83          */
84         private function getRegistryKeyFromProtocol (ProtocolHandler $protocolInstance) {
85                 // Get the key
86                 $key = $protocolInstance->getProtocol();
87
88                 // Return resulting key
89                 return $key;
90         }
91
92         /**
93          * Checks wether the given protocol is registered
94          *
95          * @param       $protocolInstance       An instance of a ProtocolHandler class
96          * @return      $isRegistered           Wether the protocol is registered
97          */
98         private function isProtocolRegistered (ProtocolHandler $protocolInstance) {
99                 // Get the key
100                 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
101
102                 // Determine it
103                 $isRegistered = $this->instanceExists($key);
104
105                 // Return result
106                 return $isRegistered;
107         }
108
109         /**
110          * Checks wether given socket resource is registered. If $socketResource is
111          * false only the instance will be checked.
112          *
113          * @param       $protocolInstance       An instance of a ProtocolHandler class
114          * @param       $socketResource         A valid socket resource
115          * @return      $isRegistered           Wether the given socket resource is registered
116          */
117         public function isSocketRegistered (ProtocolHandler $protocolInstance, $socketResource) {
118                 // Default is not registered
119                 $isRegistered = false;
120
121                 // First, check for the instance, there can be only once
122                 if ($this->isProtocolRegistered($protocolInstance)) {
123                         // That one is found so "get" a registry key from it
124                         $key = $this->getRegistryKeyFromProtocol($protocolInstance);
125
126                         // Get the registry
127                         $registryInstance = $this->getInstance($key);
128
129                         // "Get" a key for the socket
130                         $socketKey = $this->getSubRegistryKey($protocolInstance);
131
132                         // And simply ask it
133                         $isRegistered = $registryInstance->instanceExists($socketKey);
134                 } // END - if
135
136                 // Return the result
137                 return $isRegistered;
138         }
139
140         /**
141          * Registeres given socket for listener or throws an exception if it is already registered
142          *
143          * @param       $protocolInstance       An instance of a ProtocolHandler class
144          * @param       $socketResource         A valid socket resource
145          * @throws      SocketAlreadyRegisteredException        If the given socket is already registered
146          * @return      void
147          */
148         public function registerSocket (ProtocolHandler $protocolInstance, $socketResource) {
149                 // Is the socket already registered?
150                 if ($this->isSocketRegistered($protocolInstance, $socketResource)) {
151                         // Throw the exception
152                         throw new SocketAlreadyRegisteredException($protocolInstance, BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
153                 } // END - if
154
155                 // Does the instance exist?
156                 if (!$this->isProtocolRegistered($protocolInstance)) {
157                         // No, not found so we create a sub registry (not needed to configure!)
158                         $registryInstance = SubRegistry::createSubRegistry();
159
160                         // Now we can create the sub-registry for this protocol
161                         $this->addInstance($this->getRegistryKeyFromProtocol($protocolInstance), $registryInstance);
162                 } else {
163                         // Get the sub-registry back
164                         $registryInstance = $this->getInstance($this->getRegistryKeyFromProtocol($protocolInstance));
165                 }
166
167                 // Get a key for sub-registries
168                 $socketKey = $this->getSubRegistryKey($protocolInstance);
169
170                 // Get a socket container
171                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource));
172
173                 // We have a sub-registry, the socket key and the socket, now we need to put all together
174                 $registryInstance->addInstance($socketKey, $socketInstance);
175         }
176
177         /**
178          * Getter for given listener's socket resource
179          *
180          * @param       $protocolInstance       An instance of a ProtocolHandler class
181          * @return      $socketResource         A valid socket resource
182          * @throws      NoSocketRegisteredException             If the requested socket is not registered
183          */
184         public function getRegisteredSocketResource (ProtocolHandler $protocolInstance) {
185                 // The socket must be registered before we can return it
186                 if (!$this->isSocketRegistered($protocolInstance, false)) {
187                         // Throw the exception
188                         throw new NoSocketRegisteredException ($protocolInstance, self::SOCKET_NOT_REGISTERED);
189                 } // END - if
190
191                 // Now get the key from the protocol
192                 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
193
194                 // And get the registry
195                 $registryInstance = $this->getInstance($key);
196
197                 // Get a socket key
198                 $socketKey = $this->getSubRegistryKey($protocolInstance);
199
200                 // And the final socket resource
201                 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
202
203                 // Return the resource
204                 return $socketResource;
205         }
206 }
207
208 // [EOF]
209 ?>