1 /* UnicastConnectionManager.java --
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu.java.rmi.server;
42 import java.io.IOException;
43 import java.io.ObjectInput;
44 import java.io.ObjectOutput;
45 import java.net.InetAddress;
46 import java.net.ServerSocket;
47 import java.net.Socket;
48 import java.net.UnknownHostException;
49 import java.rmi.RemoteException;
50 import java.rmi.server.RMIClientSocketFactory;
51 import java.rmi.server.RMIServerSocketFactory;
52 import java.rmi.server.RMISocketFactory;
53 import java.util.ArrayList;
54 import java.util.ConcurrentModificationException;
55 import java.util.Hashtable;
56 import java.util.Iterator;
58 public class UnicastConnectionManager
59 implements Runnable, ProtocolConstants {
61 private static String localhost;
62 // use different maps for server/client type UnicastConnectionManager
63 private static Hashtable servers = new Hashtable();
64 // Package-private to avoid trampolines.
65 static Hashtable clients = new Hashtable();
66 ArrayList connections; //client connection pool
68 // make serverThread volatile for poll
69 private volatile Thread serverThread;
70 private ServerSocket ssock;
74 // Package-private to avoid a trampoline.
75 static Thread scavenger;
77 // If client and server are in the same VM, serverobj represents server
80 private static RMISocketFactory defaultSocketFactory = RMISocketFactory.getSocketFactory();
81 private RMIServerSocketFactory serverFactory;
82 private RMIClientSocketFactory clientFactory;
84 // The following is for debug
85 private static int ncsock = 0; //count of client socket
86 private static int nssock = 0; //count of server socket
87 private static int ncmanager = 0; //count of client manager
88 private static int nsmanager = 0; //count of server manager
90 private static final boolean debug = false;
92 private static final Object GLOBAL_LOCK = new Object();
96 //Use host address instead of host name to avoid name resolving issues
97 //localhost = InetAddress.getLocalHost().getHostName();
98 localhost = InetAddress.getLocalHost().getHostAddress();
100 catch (UnknownHostException _) {
101 localhost = "localhost";
107 //Only one scavenger thread running globally
108 private static void startScavenger(){
109 scavenger = new Thread(new Runnable(){
111 if (debug) System.out.println("************* start scavenger.");
112 boolean liveon = true;
114 // Sleep for the expire timeout
116 Thread.sleep(UnicastConnection.CONNECTION_TIMEOUT);
117 }catch(InterruptedException _ie){
121 // Scavenge all clients' connections that're expired
122 Iterator iter = clients.values().iterator();
123 long l = System.currentTimeMillis();
125 while(iter.hasNext()){
126 UnicastConnectionManager man = (UnicastConnectionManager)iter.next();
127 ArrayList conns = man.connections;
128 synchronized(conns) { // is the lock a little coarser?
129 for (int last = conns.size() - 1;
133 UnicastConnection conn = (UnicastConnection)conns.get(last);
134 if (UnicastConnection.isExpired(conn, l)){
139 liveon = true; //there're still live connections
143 }catch(ConcurrentModificationException cme) {
149 if (debug) System.out.println("************* exit scavenger.");
152 // As it is used for client connection, we may put this thread
153 // in daemon state to prevent the VM from blocking when exiting.
154 scavenger.setDaemon(true);
159 * Client UnicastConnectionManager constructor
161 private UnicastConnectionManager(String host, int port, RMIClientSocketFactory csf) {
165 serverFactory = null;
167 connections = new ArrayList();
171 * Server UnicastConnectionManager constructor
173 private UnicastConnectionManager(int port, RMIServerSocketFactory ssf) throws RemoteException {
176 ssock = ssf.createServerSocket(port);
177 serverPort = ssock.getLocalPort();
179 catch (IOException ioex) {
182 throw new java.rmi.server.ExportException("can not create Server Socket on port " + port,ioex);
184 // Note that for compatibility the serverName is "localhost",
185 // not UnicastConnectionManager.localhost, which is the name
186 // of the local box. A server listening on localhost:port is
187 // listening on the loopback interface, 127.0.0.1, but
188 // UnicastConnectionManager.localhost is an externally
189 // accessible IP address.
190 serverName = "localhost";
192 clientFactory = null;
196 * Return a client connection manager which will connect to the given
199 public static synchronized UnicastConnectionManager getInstance(String host, int port, RMIClientSocketFactory csf) {
200 //System.out.println("getInstance: " + host + "," + port + "," + csf);
202 csf = defaultSocketFactory;
204 // change host name to host address to avoid name resolving issues
206 host = InetAddress.getByName(host).getHostAddress();
207 }catch(Exception _){}
209 TripleKey key = new TripleKey(host, port, csf);
210 UnicastConnectionManager man = (UnicastConnectionManager)clients.get(key);
212 man = new UnicastConnectionManager(host, port, csf);
215 System.out.println("\n\n ====== " + ncmanager + " client managers.\n\n");
217 clients.put(key, man);
219 // Detect if client and server are in the same VM, i.e., their keys are equal
220 UnicastConnectionManager svrman = (UnicastConnectionManager)servers.get(key);
221 if(svrman != null){ // server and client are in the same VM
222 man.serverobj = svrman.serverobj;
229 * Return a server connection manager which will accept connection on the
232 public static synchronized UnicastConnectionManager getInstance(int port, RMIServerSocketFactory ssf) throws RemoteException {
233 //System.out.println("getInstance: " + port + "," + ssf);
235 ssf = defaultSocketFactory;
237 TripleKey key = new TripleKey(localhost, port, ssf);
238 UnicastConnectionManager man = (UnicastConnectionManager)servers.get(key);
240 man = new UnicastConnectionManager(port, ssf);
243 System.out.println("\n\n ****** " + nsmanager + " server managers.\n\n");
245 // The provided port might not be the set port.
246 key.port = man.serverPort;
247 servers.put(key, man);
253 * Get a connection from this manager.
255 public UnicastConnection getConnection() throws IOException {
257 return (getClientConnection());
260 return (getServerConnection());
265 * Accept a connection to this server.
267 private UnicastConnection getServerConnection() throws IOException {
268 Socket sock = ssock.accept();
269 sock.setTcpNoDelay(true); //??
270 UnicastConnection conn = new UnicastConnection(this, sock);
271 conn.acceptConnection();
274 System.out.println("\n\n ****** " + nssock + " server socks.\n\n");
276 //System.out.println("Server connection " + sock);
281 * Make a conection from this client to the server.
283 private UnicastConnection getClientConnection() throws IOException {
284 ArrayList conns = connections;
285 UnicastConnection conn;
287 synchronized(conns) {
288 int nconn = conns.size() - 1;
290 // if there're free connections in connection pool
292 conn = (UnicastConnection)conns.get(nconn);
293 //Should we check if conn is alive using Ping??
296 // Check if the connection is already expired
297 long l = System.currentTimeMillis();
298 if (!UnicastConnection.isExpired(conn, l)){
307 Socket sock = clientFactory.createSocket(serverName, serverPort);
308 conn = new UnicastConnection(this, sock);
309 conn.makeConnection(DEFAULT_PROTOCOL);
313 System.out.println("\n\n ====== " + ncsock + " client socks.\n\n");
320 * Get the string representation, describing the connection.
322 public String toString()
324 return serverName+":"+serverPort+" ("+serverobj+")";
328 * Discard a connection when we're done with it - maybe it can be
331 public void discardConnection(UnicastConnection conn) {
332 //System.out.println("Discarding connection " + conn);
334 if (ssock != null) //server connection
337 // To client connection, we'd like to return back to pool
338 UnicastConnection.resetTime(conn);
339 //Ensure there're only one scavenger globally
340 synchronized(GLOBAL_LOCK) {
341 connections.add(conn); //borrow this lock to garantee thread safety
342 if (scavenger == null)
349 * Start a server on this manager if it's a server socket and we've not
350 * already got one running.
352 public void startServer() {
354 if (ssock == null || serverThread != null) {
357 serverThread = new Thread(this);
358 // The following is not necessary when java.lang.Thread's constructor do this.
359 // serverThread.setContextClassLoader(Thread.currentThread().getContextClassLoader());
361 serverThread.start();
365 * Stop a server on this manager
367 public void stopServer() {
369 if(serverThread != null){
373 }catch(Exception _){}
379 * Server thread for connection manager.
382 for (;serverThread != null;) { // if serverThread==null, then exit thread
384 //System.out.println("Waiting for connection on " + serverPort);
385 UnicastConnection conn = getServerConnection();
387 // get address of remote host for the RMIIncomingThread object
388 String remoteHost = null;
389 if (conn.sock != null) {
390 remoteHost = conn.sock.getInetAddress().getHostAddress();
393 // use a thread pool to improve performance
394 //ConnectionRunnerPool.dispatchConnection(conn);
395 (new RMIIncomingThread(conn, remoteHost)).start();
396 // (new Thread(conn)).start();
398 catch (Exception e) {
405 * Serialization routine.
407 void write(ObjectOutput out) throws IOException {
408 out.writeUTF(serverName);
409 out.writeInt(serverPort);
413 * Serialization routine.
415 static UnicastConnectionManager read(ObjectInput in) throws IOException {
416 String host = in.readUTF();
417 int port = in.readInt();
418 //RMIClientSocketFactory csf = ((RMIObjectInputStream)in).manager.clientFactory;
419 //return (getInstance(host, port, csf));
420 return (getInstance(host, port, null));
426 * This is use as the hashkey for the client/server connections.
434 TripleKey(String host, int port, Object other) {
441 * Hash code just include the host and other - we ignore the port since
442 * this has unusual matching behaviour.
444 public int hashCode() {
445 return (host.hashCode() ^ other.hashCode());
448 public boolean equals(Object obj) {
449 if (obj instanceof TripleKey) {
450 TripleKey other = (TripleKey)obj;
451 if (this.host.equals(other.host) &&
452 this.other == other.other &&
453 (this.port == other.port /* || this.port == 0 || other.port == 0*/)) {
461 * Get the string representation, describing the connection.
463 public String toString()
465 return host+":"+port+" ("+other+")";