40 |
|
|
41 |
import java.net.Socket; |
import java.net.Socket; |
42 |
import java.net.SocketException; |
import java.net.SocketException; |
43 |
|
import java.util.Hashtable; |
44 |
import java.util.HashMap; |
import java.util.Iterator; |
45 |
|
import java.util.Map; |
46 |
|
|
47 |
/** |
/** |
48 |
* This class caches the opened sockets that are reused during the |
* This class caches the opened sockets that are reused during the |
56 |
/** |
/** |
57 |
* The socket map. |
* The socket map. |
58 |
*/ |
*/ |
59 |
private static HashMap sockets = new HashMap(); |
private static Hashtable sockets = new Hashtable(); |
60 |
|
|
61 |
/** |
/** |
62 |
* Put a socket. |
* Put a socket. This method also discards all not reusable sockets from |
63 |
|
* the map. |
64 |
* |
* |
65 |
* @param key as socket key. |
* @param key as socket key. |
66 |
* |
* |
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); |
sockets.put(key, s); |
72 |
|
gc(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
|
* Removes all non reusable sockets. |
77 |
|
*/ |
78 |
|
public static void gc() |
79 |
|
{ |
80 |
|
Iterator iter = sockets.entrySet().iterator(); |
81 |
|
|
82 |
|
Map.Entry e; |
83 |
|
Socket sx; |
84 |
|
|
85 |
|
while (iter.hasNext()) |
86 |
|
{ |
87 |
|
e = (Map.Entry) iter.next(); |
88 |
|
sx = (Socket) e.getValue(); |
89 |
|
|
90 |
|
if (not_reusable(sx)) |
91 |
|
iter.remove(); |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
|
* Return true if the socket is no longer reusable. |
97 |
|
*/ |
98 |
|
static boolean not_reusable(Socket s) |
99 |
|
{ |
100 |
|
return (s.isClosed() || !s.isBound() || !s.isConnected() || |
101 |
|
s.isInputShutdown() || s.isOutputShutdown()); |
102 |
} |
} |
103 |
|
|
104 |
/** |
/** |
107 |
* @param key a socket key. |
* @param key a socket key. |
108 |
* |
* |
109 |
* @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 |
110 |
* closed. |
* closed, its input or output has been shutown or otherwise the socket |
111 |
|
* is not reuseable. |
112 |
*/ |
*/ |
113 |
public static Socket get_socket(Object key) |
public static Socket get_socket(Object key) |
114 |
{ |
{ |
115 |
|
if (true) |
116 |
|
return null; |
117 |
|
|
118 |
Socket s = (Socket) sockets.get(key); |
Socket s = (Socket) sockets.get(key); |
119 |
if (s == null) |
if (s == null) |
120 |
return null; |
return null; |
121 |
else if (s.isClosed()) |
|
122 |
|
// Ensure that the socket is fully reusable. |
123 |
|
else if (not_reusable(s)) |
124 |
{ |
{ |
125 |
sockets.remove(key); |
sockets.remove(key); |
126 |
return null; |
return null; |
127 |
} |
} |
128 |
else |
else |
129 |
{ |
{ |
|
sockets.remove(key); |
|
130 |
try |
try |
131 |
{ |
{ |
132 |
// Set one minute time out that will be changed later. |
// Set one minute time out that will be changed later. |
136 |
{ |
{ |
137 |
s = null; |
s = null; |
138 |
} |
} |
139 |
|
|
140 |
|
sockets.remove(key); |
141 |
return s; |
return s; |
142 |
} |
} |
143 |
} |
} |