40 |
|
|
41 |
import java.net.Socket; |
import java.net.Socket; |
42 |
import java.net.SocketException; |
import java.net.SocketException; |
43 |
import java.util.Hashtable; |
import java.util.HashMap; |
44 |
import java.util.Iterator; |
import java.util.Iterator; |
45 |
import java.util.Map; |
import java.util.Map; |
46 |
|
|
56 |
/** |
/** |
57 |
* The socket map. |
* The socket map. |
58 |
*/ |
*/ |
59 |
private static Hashtable sockets = new Hashtable(); |
private static HashMap sockets = new HashMap(); |
60 |
|
|
61 |
/** |
/** |
62 |
* Put a socket. This method also discards all not reusable sockets from |
* Put a socket. This method also discards all not reusable sockets from |
68 |
*/ |
*/ |
69 |
public static void put_socket(Object key, Socket s) |
public static void put_socket(Object key, Socket s) |
70 |
{ |
{ |
71 |
sockets.put(key, s); |
synchronized (sockets) |
72 |
gc(); |
{ |
73 |
|
sockets.put(key, s); |
74 |
|
gc(); |
75 |
|
} |
76 |
} |
} |
77 |
|
|
78 |
/** |
/** |
79 |
* Removes all non reusable sockets. |
* Removes all non reusable sockets. As it is private, |
80 |
|
* we know we call from the synchronized code already. |
81 |
*/ |
*/ |
82 |
public static void gc() |
private static void gc() |
83 |
{ |
{ |
84 |
Iterator iter = sockets.entrySet().iterator(); |
Iterator iter = sockets.entrySet().iterator(); |
85 |
|
|
111 |
* @param key a socket key. |
* @param key a socket key. |
112 |
* |
* |
113 |
* @return an opened socket for reuse, null if no such available or it is |
* @return an opened socket for reuse, null if no such available or it is |
114 |
* closed, its input or output has been shutown or otherwise the socket |
* closed, its input or output has been shutown or otherwise the socket is not |
115 |
* is not reuseable. |
* reuseable. |
116 |
*/ |
*/ |
117 |
public static Socket get_socket(Object key) |
public static Socket get_socket(Object key) |
118 |
{ |
{ |
119 |
if (true) |
if (true) |
120 |
return null; |
return null; |
121 |
|
|
122 |
Socket s = (Socket) sockets.get(key); |
synchronized (sockets) |
|
if (s == null) |
|
|
return null; |
|
|
|
|
|
// Ensure that the socket is fully reusable. |
|
|
else if (not_reusable(s)) |
|
|
{ |
|
|
sockets.remove(key); |
|
|
return null; |
|
|
} |
|
|
else |
|
123 |
{ |
{ |
124 |
try |
Socket s = (Socket) sockets.get(key); |
125 |
|
if (s == null) |
126 |
|
return null; |
127 |
|
|
128 |
|
// Ensure that the socket is fully reusable. |
129 |
|
else if (not_reusable(s)) |
130 |
{ |
{ |
131 |
// Set one minute time out that will be changed later. |
sockets.remove(key); |
132 |
s.setSoTimeout(60*1000); |
return null; |
133 |
} |
} |
134 |
catch (SocketException e) |
else |
135 |
{ |
{ |
136 |
s = null; |
try |
137 |
|
{ |
138 |
|
// Set one minute time out that will be changed later. |
139 |
|
s.setSoTimeout(60 * 1000); |
140 |
|
} |
141 |
|
catch (SocketException e) |
142 |
|
{ |
143 |
|
s = null; |
144 |
|
} |
145 |
|
|
146 |
|
sockets.remove(key); |
147 |
|
return s; |
148 |
} |
} |
|
|
|
|
sockets.remove(key); |
|
|
return s; |
|
149 |
} |
} |
150 |
} |
} |
151 |
} |
} |