瀏覽代碼

Added basic RMI support

git-svn-id: http://svn.dmdirc.com/trunk@2777 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Chris Smith 16 年之前
父節點
當前提交
eabc506aee

+ 23
- 0
src/com/dmdirc/commandline/CommandLineParser.java 查看文件

@@ -27,6 +27,7 @@ import com.dmdirc.util.IrcAddress;
27 27
 import com.dmdirc.Main;
28 28
 import com.dmdirc.config.IdentityManager;
29 29
 import com.dmdirc.util.resourcemanager.ResourceManager;
30
+import java.rmi.RemoteException;
30 31
 
31 32
 import java.util.ArrayList;
32 33
 import java.util.List;
@@ -46,6 +47,7 @@ public class CommandLineParser {
46 47
     private static final Object[][] ARGUMENTS = new Object[][]{
47 48
         {'c', "connect", "Connect to the specified server", Boolean.TRUE},
48 49
         {'d', "directory", "Use the specified configuration directory", Boolean.TRUE},
50
+        {'e', "existing", "Try to use an existing instance of DMDirc (use with -c)", Boolean.FALSE},
49 51
         {'h', "help", "Show command line options and exit", Boolean.FALSE},
50 52
         {'p', "portable", "Enable portable mode", Boolean.FALSE},
51 53
         {'r', "disable-reporting", "Disable automatic error reporting", Boolean.FALSE},
@@ -58,6 +60,9 @@ public class CommandLineParser {
58 60
     /** Whether to disable error reporting or not. */
59 61
     private boolean disablereporting = false;
60 62
     
63
+    /** Whether or not to try and use an existing client. */
64
+    private boolean useExisting = false;
65
+    
61 66
     /**
62 67
      * Creates a new instance of CommandLineParser.
63 68
      *
@@ -87,6 +92,21 @@ public class CommandLineParser {
87 92
         if (inArg) {
88 93
             doUnknownArg("Missing parameter for argument: " + previousArg);
89 94
         }
95
+        
96
+        if (useExisting) {
97
+            final RemoteInterface server = RemoteServer.getServer();
98
+            
99
+            if (server != null) {
100
+                try {
101
+                    server.connect(addresses);
102
+                    System.exit(0);
103
+                } catch (RemoteException ex) {
104
+                    // Do nothing
105
+                }
106
+            }
107
+        }
108
+        
109
+        RemoteServer.bind();
90 110
     }
91 111
     
92 112
     /**
@@ -168,6 +188,9 @@ public class CommandLineParser {
168 188
         case 'd':
169 189
             doDirectory(param);
170 190
             break;
191
+        case 'e':
192
+            useExisting = true;
193
+            break;
171 194
         case 'h':
172 195
             doHelp();
173 196
             break;

+ 39
- 0
src/com/dmdirc/commandline/RemoteInterface.java 查看文件

@@ -0,0 +1,39 @@
1
+/*
2
+ * Copyright (c) 2006-2007 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.util.IrcAddress;
26
+
27
+import java.rmi.Remote;
28
+import java.rmi.RemoteException;
29
+import java.util.List;
30
+
31
+/**
32
+ *
33
+ * @author chris
34
+ */
35
+public interface RemoteInterface extends Remote {
36
+    
37
+    void connect(List<IrcAddress> addresses) throws RemoteException;
38
+
39
+}

+ 85
- 0
src/com/dmdirc/commandline/RemoteServer.java 查看文件

@@ -0,0 +1,85 @@
1
+/*
2
+ * Copyright (c) 2006-2007 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.util.IrcAddress;
26
+
27
+import java.rmi.NotBoundException;
28
+import java.rmi.RemoteException;
29
+import java.rmi.registry.LocateRegistry;
30
+import java.rmi.registry.Registry;
31
+import java.rmi.server.UnicastRemoteObject;
32
+import java.util.List;
33
+
34
+/**
35
+ *
36
+ * @author chris
37
+ */
38
+public class RemoteServer implements RemoteInterface {
39
+    
40
+    public RemoteServer() {
41
+        super();
42
+    }
43
+    
44
+    @Override
45
+    public void connect(List<IrcAddress> addresses) throws RemoteException {
46
+        for (IrcAddress address : addresses) {
47
+            address.connect();
48
+        }
49
+    }    
50
+    
51
+    /**
52
+     * Binds to the RMI registry so that other clients may find this remote
53
+     * server.
54
+     */
55
+    public static void bind() {
56
+        try {
57
+            final RemoteServer server = new RemoteServer();
58
+            RemoteInterface stub =
59
+                (RemoteInterface) UnicastRemoteObject.exportObject(server, 0);
60
+            Registry registry = LocateRegistry.createRegistry(1099);
61
+            registry.rebind("DMDirc", stub);
62
+        } catch (RemoteException ex) {
63
+            // Do nothing
64
+        }
65
+    }
66
+    
67
+    /**
68
+     * Retrieves a reference to an existing RemoteServer, if there is one.
69
+     * Note that this must be called before bind(), unless you want a reference
70
+     * to our own client for some reason.
71
+     * 
72
+     * @return The RemoteServer instance, or null if none was available
73
+     */
74
+    public static RemoteInterface getServer() {
75
+        try {
76
+            Registry registry = LocateRegistry.getRegistry();
77
+            return (RemoteInterface) registry.lookup("DMDirc");
78
+        } catch (RemoteException ex) {
79
+            return null;
80
+        } catch (NotBoundException ex) {
81
+            return null;
82
+        }
83
+    }
84
+
85
+}

+ 4
- 1
src/com/dmdirc/util/IrcAddress.java 查看文件

@@ -27,6 +27,7 @@ import com.dmdirc.ServerManager;
27 27
 import com.dmdirc.config.Identity;
28 28
 import com.dmdirc.config.IdentityManager;
29 29
 
30
+import java.io.Serializable;
30 31
 import java.util.ArrayList;
31 32
 import java.util.List;
32 33
 
@@ -36,7 +37,9 @@ import java.util.List;
36 37
  * 
37 38
  * @author Chris
38 39
  */
39
-public class IrcAddress {
40
+public class IrcAddress implements Serializable {
41
+    
42
+    private final static long serialVersionUID = 1;
40 43
 
41 44
     /** Whether or not this address uses SSL. */
42 45
     private boolean usesSSL;

Loading…
取消
儲存