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 SocketRegistry extends BaseRegistry implements Register, RegisterableSocket {
25 // Exception constants
26 const SOCKET_NOT_REGISTERED = 0xd00;
29 * Instance of this class
31 private static $registryInstance = NULL;
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates a singleton instance of this registry class
46 * @return $registryInstance An instance of this class
48 public static final function createSocketRegistry () {
49 // Is an instance there?
50 if (is_null(self::$registryInstance)) {
51 // Not yet, so create one
52 self::$registryInstance = new SocketRegistry();
55 // Return the instance
56 return self::$registryInstance;
60 * "Getter" to get a string respresentation for a key for the sub-registry
61 * in this format: class:protocol:port
63 * @param $protocolInstance An instance of a ProtocolHandler class
64 * @return $key A string representation of the socket for the registry
66 private function getSubRegistryKey (ProtocolHandler $protocolInstance) {
67 // Get protocol and port number and add both together
68 $key = sprintf("%s:%s:%s",
69 $protocolInstance->__toString(),
70 $protocolInstance->getProtocol(),
71 $protocolInstance->getPort()
74 // Return resulting key
79 * "Getter" to get a string respresentation of the protocol
81 * @param $protocolInstance An instance of a ProtocolHandler class
82 * @return $key A string representation of the protocol for the registry
84 private function getRegistryKeyFromProtocol (ProtocolHandler $protocolInstance) {
86 $key = $protocolInstance->getProtocol();
88 // Return resulting key
93 * Checks wether the given protocol is registered
95 * @param $protocolInstance An instance of a ProtocolHandler class
96 * @return $isRegistered Wether the protocol is registered
98 private function isProtocolRegistered (ProtocolHandler $protocolInstance) {
100 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
103 $isRegistered = $this->instanceExists($key);
106 return $isRegistered;
110 * Checks wether given socket resource is registered. If $socketResource is
111 * false only the instance will be checked.
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
117 public function isSocketRegistered (ProtocolHandler $protocolInstance, $socketResource) {
118 // Default is not registered
119 $isRegistered = false;
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);
127 $registryInstance = $this->getInstance($key);
129 // "Get" a key for the socket
130 $socketKey = $this->getSubRegistryKey($protocolInstance);
133 $isRegistered = $registryInstance->instanceExists($socketKey);
137 return $isRegistered;
141 * Registeres given socket for listener or throws an exception if it is already registered
143 * @param $protocolInstance An instance of a ProtocolHandler class
144 * @param $socketResource A valid socket resource
145 * @param $packageData Optional raw package data
146 * @throws SocketAlreadyRegisteredException If the given socket is already registered
149 public function registerSocket (ProtocolHandler $protocolInstance, $socketResource, array $packageData = array()) {
150 // Is the socket already registered?
151 if ($this->isSocketRegistered($protocolInstance, $socketResource)) {
152 // Throw the exception
153 throw new SocketAlreadyRegisteredException($protocolInstance, BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
156 // Does the instance exist?
157 if (!$this->isProtocolRegistered($protocolInstance)) {
158 // No, not found so we create a sub registry (not needed to configure!)
159 $registryInstance = SubRegistry::createSubRegistry();
161 // Now we can create the sub-registry for this protocol
162 $this->addInstance($this->getRegistryKeyFromProtocol($protocolInstance), $registryInstance);
164 // Get the sub-registry back
165 $registryInstance = $this->getInstance($this->getRegistryKeyFromProtocol($protocolInstance));
168 // Get a key for sub-registries
169 $socketKey = $this->getSubRegistryKey($protocolInstance);
171 // Get a socket container
172 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $protocolInstance, $packageData));
174 // We have a sub-registry, the socket key and the socket, now we need to put all together
175 /* DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource=' . $socketResource);
176 $registryInstance->addInstance($socketKey, $socketInstance);
180 * Getter for given listener's socket resource
182 * @param $protocolInstance An instance of a ProtocolHandler class
183 * @return $socketResource A valid socket resource
184 * @throws NoSocketRegisteredException If the requested socket is not registered
186 public function getRegisteredSocketResource (ProtocolHandler $protocolInstance) {
187 // The socket must be registered before we can return it
188 if (!$this->isSocketRegistered($protocolInstance, false)) {
189 // Throw the exception
190 throw new NoSocketRegisteredException ($protocolInstance, self::SOCKET_NOT_REGISTERED);
193 // Now get the key from the protocol
194 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
196 // And get the registry
197 $registryInstance = $this->getInstance($key);
200 $socketKey = $this->getSubRegistryKey($protocolInstance);
202 // And the final socket resource
203 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
205 // Return the resource
206 return $socketResource;
210 * "Getter" for protocol/connection instance from given package data
212 * @param $packageData Raw package data
213 * @return $protocolInstance An instance of a ProtocolHandler class
215 public function getHandlerInstanceFromPackageData (array $packageData) {
216 // Init protocol instance
217 $protocolInstance = NULL;
219 // Get all keys and check them
220 foreach ($this->getInstanceRegistry() as $key=>$registryInstance) {
221 // This is always a SubRegistry instance
222 foreach ($registryInstance->getInstanceRegistry() as $subKey=>$containerInstance) {
223 // This is a SocketContainer instance, so does the recipient match?
224 if ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) {
225 // Found one, so get the protocol instance and abort any further search
226 $protocolInstance = $containerInstance->getProtocolInstance();
232 // Return the protocol instance
233 return $protocolInstance;