File Source: ServerManager.java
/*
P/P * Method: com.dmdirc.ServerManager__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc;
24
25 import com.dmdirc.config.IdentityManager;
26 import com.dmdirc.ui.interfaces.Window;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32 * The ServerManager maintains a list of all servers, and provides methods to
33 * search or iterate over them.
34 *
35 * @author chris
36 */
37 public final class ServerManager {
38
39 /** Singleton instance of ServerManager. */
40 private static ServerManager me;
41
42 /** All servers that currently exist. */
43 private final List<Server> servers = new ArrayList<Server>();
44
45 /**
46 * Creates a new instance of ServerManager.
47 */
/*
P/P * Method: void com.dmdirc.ServerManager()
*
* Postconditions:
* this.servers == &new ArrayList(ServerManager#1)
* new ArrayList(ServerManager#1) num objects == 1
*/
48 private ServerManager() {
49 }
50
51 /**
52 * Returns the singleton instance of ServerManager.
53 *
54 * @return Instance of ServerManager
55 */
56 public static synchronized ServerManager getServerManager() {
/*
P/P * Method: ServerManager getServerManager()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ServerManager(getServerManager#1)}
* me != null
* return_value == me
* new ArrayList(ServerManager#1) num objects <= 1
* new ServerManager(getServerManager#1) num objects <= 1
* new ServerManager(getServerManager#1).servers == &new ArrayList(ServerManager#1)
*
* Test Vectors:
* me: Inverse{null}, Addr_Set{null}
*/
57 if (me == null) {
58 me = new ServerManager();
59 }
60 return me;
61 }
62
63 /**
64 * Registers a new server with the manager.
65 *
66 * @param server The server to be registered
67 */
68 public void registerServer(final Server server) {
/*
P/P * Method: void registerServer(Server)
*
* Preconditions:
* this.servers != null
*/
69 synchronized (servers) {
70 servers.add(server);
71 }
72 }
73
74 /**
75 * Unregisters a server from the manager. The request is ignored if the
76 * ServerManager is in the process of closing all servers.
77 *
78 * @param server The server to be unregistered
79 */
80 public void unregisterServer(final Server server) {
/*
P/P * Method: void unregisterServer(Server)
*
* Preconditions:
* this.servers != null
*/
81 synchronized (servers) {
82 servers.remove(server);
83 }
84 }
85
86 /**
87 * Returns a list of all servers.
88 *
89 * @return A list of all servers
90 */
91 public List<Server> getServers() {
/*
P/P * Method: List getServers()
*
* Postconditions:
* return_value == &new ArrayList(getServers#1)
* new ArrayList(getServers#1) num objects == 1
*/
92 return new ArrayList<Server>(servers);
93 }
94
95 /**
96 * Makes all servers disconnected with the specified quit message.
97 *
98 * @param message The quit message to send to the IRC servers
99 */
100 public void disconnectAll(final String message) {
/*
P/P * Method: void disconnectAll(String)
*
* Preconditions:
* this.servers != null
* (soft) init'ed(com.dmdirc.Server$4__static_init.new int[](Server$4__static_init#1)[...])
*
* Presumptions:
* java.util.Iterator:next(...)@102 != null
* server.channels@102 != null
* server.config@102 != null
* server.invites@102 != null
* server.listeners@102 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@102: {1}, {0}
*/
101 synchronized (servers) {
102 for (Server server : servers) {
103 server.disconnect(message);
104 }
105 }
106 }
107
108 /**
109 * Closes all servers with a default quit message.
110 */
111 public void closeAll() {
/*
P/P * Method: void closeAll()
*
* Preconditions:
* this.servers != null
* (soft) init'ed(com.dmdirc.Server$4__static_init.new int[](Server$4__static_init#1)[...])
*
* Presumptions:
* java.util.Iterator:next(...)@113 != null
* server.channels@113 != null
* server.config@113 != null
* server.invites@113 != null
* server.listeners@113 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@113: {1}, {0}
*/
112 synchronized (servers) {
113 for (Server server : servers) {
114 server.disconnect();
115 server.close();
116 }
117 }
118 }
119
120 /**
121 * Closes all servers with the specified quit message.
122 *
123 * @param message The quit message to send to the IRC servers
124 */
125 public void closeAll(final String message) {
/*
P/P * Method: void closeAll(String)
*
* Preconditions:
* this.servers != null
* (soft) init'ed(com.dmdirc.Server$4__static_init.new int[](Server$4__static_init#1)[...])
*
* Presumptions:
* java.util.Iterator:next(...)@127 != null
* server.channels@127 != null
* server.config@127 != null
* server.invites@127 != null
* server.listeners@127 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@127: {1}, {0}
*/
126 synchronized (servers) {
127 for (Server server : servers) {
128 server.disconnect(message);
129 server.close();
130 }
131 }
132 }
133
134 /**
135 * Returns the number of servers that are registered with the manager.
136 *
137 * @return number of registered servers
138 */
139 public int numServers() {
/*
P/P * Method: int numServers()
*
* Preconditions:
* this.servers != null
*
* Postconditions:
* init'ed(return_value)
*/
140 return servers.size();
141 }
142
143 /**
144 * Returns the server instance that owns the specified internal frame.
145 *
146 * @param active The internal frame to check
147 * @return The server associated with the internal frame
148 */
149 public Server getServerFromFrame(final Window active) {
/*
P/P * Method: Server getServerFromFrame(Window)
*
* Preconditions:
* this.servers != null
*
* Presumptions:
* java.util.Iterator:next(...)@151 != null
* server.channels@151 != null
* server.queries@151 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@151: {1}, {0}
*/
150 synchronized (servers) {
151 for (Server server : servers) {
152 if (server.ownsFrame(active)) {
153 return server;
154 }
155 }
156 }
157
158 return null;
159 }
160
161 /**
162 * Retrieves a list of servers connected to the specified network.
163 *
164 * @param network The network to search for
165 * @return A list of servers connected to the network
166 */
167 public List<Server> getServersByNetwork(final String network) {
/*
P/P * Method: List getServersByNetwork(String)
*
* Preconditions:
* this.servers != null
*
* Presumptions:
* java.util.Iterator:next(...)@171 != null
*
* Postconditions:
* return_value == &new ArrayList(getServersByNetwork#1)
* new ArrayList(getServersByNetwork#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@171: {1}, {0}
*/
168 final List<Server> res = new ArrayList<Server>();
169
170 synchronized (servers) {
171 for (Server server : servers) {
172 if (server.isNetwork(network)) {
173 res.add(server);
174 }
175 }
176 }
177
178 return res;
179 }
180
181 /**
182 * Retrieves a list of servers connected to the specified address.
183 *
184 * @param address The address to search for
185 * @return A list of servers connected to the network
186 */
187 public List<Server> getServersByAddress(final String address) {
/*
P/P * Method: List getServersByAddress(String)
*
* Preconditions:
* this.servers != null
*
* Presumptions:
* com.dmdirc.parser.irc.ServerInfo:getHost(...)@862 != null
* java.util.Iterator:next(...)@191 != null
* server.serverInfo@191 != null
*
* Postconditions:
* return_value == &new ArrayList(getServersByAddress#1)
* new ArrayList(getServersByAddress#1) num objects == 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@192: {0}, {1}
* java.util.Iterator:hasNext(...)@191: {1}, {0}
*/
188 final List<Server> res = new ArrayList<Server>();
189
190 synchronized (servers) {
191 for (Server server : servers) {
192 if (server.getName().equalsIgnoreCase(address)) {
193 res.add(server);
194 }
195 }
196 }
197
198 return res;
199 }
200
201 /**
202 * Connects the user to Quakenet if neccessary and joins #DMDirc.
203 */
204 public void joinDevChat() {
/*
P/P * Method: void joinDevChat()
*
* Preconditions:
* this.servers != null
* (soft) init'ed(com/dmdirc/actions/wrappers/AliasWrapper.me)
* (soft) init'ed(me)
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getProfiles(...)@224 != null
* connectedServer.autochannels@209 != null
* connectedServer.channels@209 != null
* connectedServer.converter@209 != null
* connectedServer.invites@209 != null
* ...
*
* Postconditions:
* com/dmdirc/actions/wrappers/AliasWrapper.me == old com/dmdirc/actions/wrappers/AliasWrapper.me
* me == old me
* new AliasWrapper(getAliasWrapper#1) num objects == 0, if init'ed
* new AliasWrapper(getAliasWrapper#1).actions == null
* new AliasWrapper(getAliasWrapper#1).aliases == null
* new AliasWrapper(getAliasWrapper#1).author == null
* new AliasWrapper(getAliasWrapper#1).component == 0, if init'ed
* new AliasWrapper(getAliasWrapper#1).description == null
* new AliasWrapper(getAliasWrapper#1).name == null
* new AliasWrapper(getAliasWrapper#1).settings == null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@209: {0}, {1}
* java.util.Map:containsKey(...)@445: {0}, {1}
* server.myState.state@209: Inverse{&com.dmdirc.ServerState__static_init.new ServerState(ServerState__static_init#3)}, Addr_Set{&com.dmdirc.ServerState__static_init.new ServerState(ServerState__static_init#3)}
*/
205 final List<Server> qnetServers = getServersByNetwork("Quakenet");
206
207 Server connectedServer = null;
208
209 for (Server server : qnetServers) {
210 if (server.getState() == ServerState.CONNECTED) {
211 connectedServer = server;
212
213 if (server.hasChannel("#DMDirc")) {
214 server.join("#DMDirc");
215 return;
216 }
217 }
218 }
219
220 if (connectedServer == null) {
221 final List<String> channels = new ArrayList<String>();
222 channels.add("#DMDirc");
223
224 new Server("irc.quakenet.org", 6667, "", false,
225 IdentityManager.getProfiles().get(0), channels);
226 } else {
227 connectedServer.join("#DMDirc");
228 }
229 }
230
231 }
SofCheck Inspector Build Version : 2.17854
| ServerManager.java |
2009-Jun-25 01:54:24 |
| ServerManager.class |
2009-Sep-02 17:04:13 |