File Source: RemoteServer.java
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.commandline;
24
25 import com.dmdirc.logger.ErrorLevel;
26 import com.dmdirc.logger.Logger;
27 import com.dmdirc.util.IrcAddress;
28
29 import java.rmi.NotBoundException;
30 import java.rmi.RemoteException;
31 import java.rmi.registry.LocateRegistry;
32 import java.rmi.registry.Registry;
33 import java.rmi.server.UnicastRemoteObject;
34 import java.util.List;
35
36 /**
37 * An RMI server that allows other clients to interact with DMDirc.
38 *
39 * @author chris
40 */
41 public class RemoteServer implements RemoteInterface {
42
43 /** The minimum port to use for RMI binding. */
44 private static final int MINPORT = 3634;
45 /** The maximum port to use for RMI binding. */
46 private static final int MAXPORT = MINPORT + 5;
47 /** The interface we're exposing. */
/*
P/P * Method: com.dmdirc.commandline.RemoteServer__static_init
*
* Postconditions:
* SERVER == &new RemoteServer(RemoteServer__static_init#1)
* new RemoteServer(RemoteServer__static_init#1) num objects == 1
*/
48 private static final RemoteServer SERVER = new RemoteServer();
49
50 /**
51 * Creates a new instance of RemoteServer.
52 */
53 public RemoteServer() {
/*
P/P * Method: void com.dmdirc.commandline.RemoteServer()
*/
54 super();
55 }
56
57 /** {@inheritDoc} */
58 @Override
59 public void connect(final List<IrcAddress> addresses) throws RemoteException {
/*
P/P * Method: void connect(List)
*
* Preconditions:
* addresses != null
*
* Presumptions:
* java.util.Iterator:next(...)@60 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@60: {0}, {1}
*/
60 for (IrcAddress address : addresses) {
61 address.connect();
62 }
63 }
64
65 /**
66 * Binds to the RMI registry so that other clients may find this remote
67 * server.
68 */
69 public static void bind() {
70 RemoteInterface stub;
71
72 try {
/*
P/P * Method: void bind()
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* java.rmi.registry.LocateRegistry:createRegistry(...)@81 != null
*/
73 stub = (RemoteInterface) UnicastRemoteObject.exportObject(SERVER, 0);
74 } catch (RemoteException ex) {
75 Logger.appError(ErrorLevel.MEDIUM, "Unable to export remote interface", ex);
76 return;
77 }
78
79 for (int port = MINPORT; port < MAXPORT; port++) {
80 try {
81 final Registry registry = LocateRegistry.createRegistry(port);
82 registry.rebind("DMDirc", stub);
83 return;
84 } catch (RemoteException ex) {
85 continue;
86 }
87 }
88 }
89
90 /**
91 * Retrieves a reference to an existing RemoteServer, if there is one.
92 * Note that this must be called before bind(), unless you want a reference
93 * to our own client for some reason.
94 *
95 * @return The RemoteServer instance, or null if none was available
96 */
97 public static RemoteInterface getServer() {
/*
P/P * Method: RemoteInterface getServer()
*
* Presumptions:
* java.rmi.registry.LocateRegistry:getRegistry(...)@100 != null
*
* Postconditions:
* init'ed(return_value)
*/
98 for (int port = MINPORT; port < MAXPORT; port++) {
99 try {
100 final Registry registry = LocateRegistry.getRegistry("localhost", port);
101 final RemoteInterface iface = (RemoteInterface) registry.lookup("DMDirc");
102
103 if (iface == null) {
104 continue;
105 } else {
106 return iface;
107 }
108 } catch (RemoteException ex) {
109 continue;
110 } catch (NotBoundException ex) {
111 continue;
112 }
113 }
114
115 // No RMI server found
116 return null;
117 }
118
119 }
SofCheck Inspector Build Version : 2.17854
| RemoteServer.java |
2009-Jun-25 01:54:24 |
| RemoteServer.class |
2009-Sep-02 17:04:16 |