KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > SocketConnection


1 /*
2  * SocketConnection.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: SocketConnection.java,v 1.1.1.1 2002/09/24 16:07:54 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.net.ConnectException JavaDoc;
25 import java.net.NoRouteToHostException JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28
29 public final class SocketConnection
30 {
31     private final String JavaDoc hostName;
32     private final int port;
33     private final int timeout; // milliseconds
34
private final int checkInterval; // milliseconds
35
private final Cancellable client;
36
37     private Socket JavaDoc socket;
38     private String JavaDoc errorText;
39
40     public SocketConnection(String JavaDoc hostName, int port, int timeout,
41         int checkInterval, Cancellable client)
42     {
43         this.hostName = hostName;
44         this.port = port;
45         this.timeout = timeout;
46         this.checkInterval = checkInterval;
47         this.client = client;
48     }
49
50     public final String JavaDoc getErrorText()
51     {
52         return errorText;
53     }
54
55     private final void setErrorText(String JavaDoc s)
56     {
57         errorText = s;
58     }
59
60     public Socket JavaDoc connect()
61     {
62         socket = null;
63         long start = System.currentTimeMillis();
64         connectThread.start();
65         while (System.currentTimeMillis() - start < timeout) {
66             try {
67                 connectThread.join(checkInterval);
68             }
69             catch (InterruptedException JavaDoc e) {
70                 Log.error(e);
71                 setErrorText(e.toString());
72                 return null;
73             }
74             if (client != null && client.cancelled()) {
75                 Log.debug("cancelled!");
76                 return null;
77             }
78             if (!connectThread.isAlive())
79                 break;
80         }
81         if (socket == null && connectThread.isAlive())
82             setErrorText("Timed out");
83         return socket;
84     }
85
86     private final Thread JavaDoc connectThread = new Thread JavaDoc("connect") {
87         public void run()
88         {
89             try {
90                 socket = new Socket JavaDoc(hostName, port);
91             }
92             catch (NoRouteToHostException JavaDoc e) {
93                 setErrorText("No route to host " + hostName);
94             }
95             catch (UnknownHostException JavaDoc e) {
96                 setErrorText("Unknown host " + hostName);
97             }
98             catch (ConnectException JavaDoc e) {
99                 setErrorText("Connection refused");
100             }
101             catch (Exception JavaDoc e) {
102                 Log.error(e);
103                 setErrorText("Unable to connect to " + hostName);
104             }
105         }
106     };
107 }
108
Popular Tags