comparison src/com/go/trove/net/SocketConnector.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /* ====================================================================
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3 * ====================================================================
4 * The Tea Software License, Version 1.1
5 *
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Walt Disney Internet Group (http://opensource.go.com/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact opensource@dig.com.
31 *
32 * 5. Products derived from this software may not be called "Tea",
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35 * written permission of the Walt Disney Internet Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * For more information about Tea, please see http://opensource.go.com/.
51 */
52
53 package com.go.trove.net;
54
55 import java.io.*;
56 import java.net.*;
57 import java.util.*;
58 import com.go.trove.util.*;
59
60 /******************************************************************************
61 * Allows client socket connections to be established with a timeout.
62 *
63 * @author Brian S O'Neill
64 * @version
65 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 00/12/05 <!-- $-->
66 */
67 public class SocketConnector {
68 // Limit the number of threads that may simultaneously connect to a
69 // specific destination.
70 private static final int CONNECT_THREAD_MAX = 5;
71
72 // Maps address:port pairs to ThreadPools for connecting.
73 private static Map mConnectors =
74 Collections.synchronizedMap(new SoftHashMap());
75
76 /**
77 * @param timeout Max time to wait for new connection. If negative, wait
78 * is infinite.
79 * @return null if couldn't connect in time.
80 */
81 public static Socket connect(String host, int port, long timeout)
82 throws SocketException
83 {
84 return connect((Object)host, port, timeout);
85 }
86
87
88 /**
89 * @param timeout Max time to wait for new connection. If negative, wait
90 * is infinite.
91 * @return null if couldn't connect in time.
92 */
93 public static Socket connect(InetAddress address, int port, long timeout)
94 throws SocketException
95 {
96 return connect((Object)address, port, timeout);
97 }
98
99 /**
100 * @param address either a string or InetAddress.
101 * @param timeout Max time to wait for new connection. If negative, wait
102 * is infinite.
103 * @return null if couldn't connect in time
104 */
105 private static Socket connect(Object address, int port, long timeout)
106 throws SocketException
107 {
108 Key key = new Key(address, port);
109 ThreadPool pool;
110 synchronized (mConnectors) {
111 pool = (ThreadPool)mConnectors.get(key);
112 if (pool == null) {
113 pool = new ThreadPool
114 ("SocketConnector[" + key + ']', CONNECT_THREAD_MAX);
115 pool.setIdleTimeout(10000);
116 mConnectors.put(key, pool);
117 }
118 }
119
120 Connector connector = new Connector(key);
121 Thread thread;
122
123 long start;
124 if (timeout > 0) {
125 start = System.currentTimeMillis();
126 }
127 else {
128 start = 0;
129 }
130
131 try {
132 thread = pool.start(connector, timeout);
133 }
134 catch (InterruptedException e) {
135 return null;
136 }
137
138 if (timeout > 0) {
139 timeout = timeout - (System.currentTimeMillis() - start);
140 if (timeout < 0) {
141 timeout = 0;
142 }
143 }
144
145 try {
146 Socket socket = connector.connect(timeout);
147 if (socket != null) {
148 return socket;
149 }
150 }
151 catch (InterruptedException e) {
152 }
153
154 thread.interrupt();
155 return null;
156 }
157
158 private SocketConnector() {
159 }
160
161 private static class Key {
162 final Object mAddress;
163 final int mPort;
164
165 Key(Object address, int port) {
166 mAddress = address;
167 mPort = port;
168 }
169
170 public boolean equals(Object obj) {
171 if (obj instanceof Key) {
172 Key key = (Key)obj;
173 return key.mAddress.equals(mAddress) && key.mPort == mPort;
174 }
175 return false;
176 }
177
178 public int hashCode() {
179 return mAddress.hashCode() + mPort;
180 }
181
182 public String toString() {
183 if (mAddress instanceof InetAddress) {
184 return ((InetAddress)mAddress).getHostAddress() + ':' + mPort;
185 }
186 else {
187 return String.valueOf(mAddress) + ':' + mPort;
188 }
189 }
190 }
191
192 private static class Connector implements Runnable {
193 private final Key mKey;
194 private Object mSocketOrException;
195 private boolean mDoneWaiting;
196
197 public Connector(Key key) {
198 mKey = key;
199 }
200
201 public synchronized Socket connect(long timeout)
202 throws SocketException, InterruptedException
203 {
204 try {
205 if (mSocketOrException == null) {
206 if (timeout < 0) {
207 wait();
208 }
209 else if (timeout > 0) {
210 wait(timeout);
211 }
212 else {
213 return null;
214 }
215 }
216 }
217 finally {
218 mDoneWaiting = true;
219 }
220
221 if (mSocketOrException instanceof Socket) {
222 return (Socket)mSocketOrException;
223 }
224 else if (mSocketOrException instanceof InterruptedIOException) {
225 throw new InterruptedException();
226 }
227 else if (mSocketOrException instanceof Exception) {
228 throw new SocketException
229 ("Unable to connect to " + mKey + ", " +
230 ((Exception)mSocketOrException).getMessage());
231 }
232
233 return null;
234 }
235
236 public void run() {
237 try {
238 Socket socket;
239 Object address = mKey.mAddress;
240 if (address instanceof InetAddress) {
241 socket = new Socket((InetAddress)address, mKey.mPort);
242 }
243 else {
244 socket = new Socket(String.valueOf(address), mKey.mPort);
245 }
246
247 synchronized (this) {
248 if (mDoneWaiting) {
249 try {
250 socket.close();
251 }
252 catch (IOException e) {
253 }
254 }
255 else {
256 mSocketOrException = socket;
257 notify();
258 }
259 }
260 }
261 catch (Exception e) {
262 synchronized (this) {
263 mSocketOrException = e;
264 notify();
265 }
266 }
267 }
268 }
269 }