1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.core.BasicClass; 18 import org.as2lib.env.except.IllegalArgumentException; 19 import org.as2lib.io.conn.core.server.ServerRegistry; 20 import org.as2lib.io.conn.core.server.ReservedHostException; 21 import org.as2lib.io.conn.local.core.EnhancedLocalConnection; 22 23 /** 24 * {@code LocalServerRegistry} keeps track of running servers. 25 * 26 * <p>This means that servers are registered to be aware of all servers that are 27 * currently running. The servers are registered in a kind of gloabl registry, even 28 * if you instantiate different instances of this class and in different swfs. 29 * 30 * <p>Servers are expected to register themselves at this registry when they start 31 * running and unregister themselves when they stop running. 32 * 33 * <p>Example: 34 * <code> 35 * var serverRegistry:LocalServerRegistry = new LocalServerRegistry(); 36 * serverRegistry.registerServer("local.as2lib.org"); 37 * serverRegistry.containsServer("local.as2lib.org"); 38 * serverRegistry.removeServer("local.as2lib.org"); 39 * </code> 40 * 41 * <p>As you can see in the above example, not servers are registered but hosts, 42 * that represent servers. 43 * 44 * @author Simon Wacker 45 * @author Christoph Atteneder 46 */ 47 class org.as2lib.io.conn.local.server.LocalServerRegistry extends BasicClass implements ServerRegistry { 48 49 /** Contains all registered Servers. */ 50 private var serverRegistry:Object; 51 52 /** 53 * Constructs a new {@code LocalServerRegistry} instance. 54 */ 55 public function LocalServerRegistry(Void) { 56 serverRegistry = new Object(); 57 } 58 59 /** 60 * Returns whether a server with the passed-in {@code host} is registered in some 61 * registry. 62 * 63 * <p>This does not mean that the server is registered in this registry. It can be 64 * registered in another registry, even in another swf. 65 * 66 * @param host the host that acts as an identifier for the server 67 * @return {@code true} if the server with the given {@code host} is regitered else 68 * {@code false} 69 */ 70 public function containsServer(host:String):Boolean { 71 return EnhancedLocalConnection.connectionExists(host); 72 } 73 74 /** 75 * Registers the server with the given {@code host} at this server registry. 76 * 77 * <p>The server is registered gloablly, that means that registries in other swfs 78 * can check whether the server is registered. 79 * 80 * @param host the host that acts as an identifier for the server 81 * @throws IllegalArgumentException if {@code host} is {@code null}, {@code undefined} 82 * or an empty string 83 * @throws ReservedHostException if a server with the passed-in {@code host} is already 84 * running 85 */ 86 public function registerServer(host:String):Void { 87 if (!host) throw new IllegalArgumentException("Host must not be null, undefined or a blank string.", this, arguments); 88 var connection:EnhancedLocalConnection = new EnhancedLocalConnection(); 89 try { 90 connection.connect(host); 91 } catch(exception:org.as2lib.io.conn.local.core.ReservedConnectionException) { 92 // without braces around "new ReservedHostException.." not MTASC compatible 93 throw (new ReservedHostException("Server with host [" + host + "] is already running.", this, arguments)).initCause(exception); 94 } 95 serverRegistry[host] = connection; 96 } 97 98 /** 99 * Removes the server with the given {@code host} from this server registry. 100 * 101 * <p>Only servers that have been registered directly at this registry can be removed. 102 * 103 * @param host the host that acts as an identifier for the server to remove 104 * @throws IllegalArgumentException if you try to unregister a server that has not 105 * been registered directly at this registry but at another one 106 */ 107 public function removeServer(host:String):Void { 108 if (serverRegistry[host]) { 109 var connection:EnhancedLocalConnection = serverRegistry[host]; 110 connection.close(); 111 serverRegistry[host] = undefined; 112 return; 113 } 114 if (containsServer(host)) { 115 throw new IllegalArgumentException("Local server registry can only remove servers that have been registered directly at it. Host [" + host + "] has been registered at another registry.", this, arguments); 116 } 117 } 118 119 }