KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > TimedWriter


1 // $Id: TimedWriter.java,v 1.3 2004/07/05 14:17:35 belaban Exp $
2

3 package org.jgroups.util;
4
5
6
7 import java.io.DataOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.OutputStream JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.Socket JavaDoc;
12
13 /**
14    Waits until the buffer has been written to the output stream, or until timeout msecs have elapsed,
15    whichever comes first.
16    TODO: make it more generic, so all sorts of timed commands should be executable. Including return
17    values, exceptions and Timeout exception. Also use ReusableThread instead of creating a new threa
18    each time.
19
20    @author Bela Ban
21 */

22
23
24 public class TimedWriter {
25     Thread JavaDoc thread=null;
26     long timeout=2000;
27     boolean completed=true;
28     Exception JavaDoc write_ex=null;
29     Socket JavaDoc sock=null;
30
31
32     class Timeout extends Exception JavaDoc {
33     public String JavaDoc toString() {
34         return "TimedWriter.Timeout";
35     }
36     }
37
38
39
40     class WriterThread extends Thread JavaDoc {
41     DataOutputStream JavaDoc out=null;
42     byte[] buf=null;
43     int i=0;
44
45     
46     public WriterThread(OutputStream JavaDoc out, byte[] buf) {
47         this.out=new DataOutputStream JavaDoc(out);
48         this.buf=buf;
49         setName("TimedWriter.WriterThread");
50     }
51     
52     public WriterThread(OutputStream JavaDoc out, int i) {
53         this.out=new DataOutputStream JavaDoc(out);
54         this.i=i;
55         setName("TimedWriter.WriterThread");
56     }
57
58     public void run() {
59         try {
60         if(buf != null)
61             out.write(buf);
62         else {
63             out.writeInt(i);
64         }
65             
66         }
67         catch(IOException JavaDoc e) {
68         write_ex=e;
69         }
70         completed=true;
71     }
72     }
73
74
75     class SocketCreator extends Thread JavaDoc {
76     InetAddress JavaDoc local=null, remote=null;
77     int peer_port=0;
78
79     
80     public SocketCreator(InetAddress JavaDoc local, InetAddress JavaDoc remote, int peer_port) {
81         this.local=local;
82         this.remote=remote;
83         this.peer_port=peer_port;
84     }
85
86
87     public void run() {
88         completed=false;
89         sock=null;
90
91         try {
92         sock=new Socket JavaDoc(remote, peer_port, local, 0); // 0 means choose any port
93
}
94         catch(IOException JavaDoc io_ex) {
95         write_ex=io_ex;
96         }
97         completed=true;
98     }
99     }
100
101
102
103
104     void start(InetAddress JavaDoc local, InetAddress JavaDoc remote, int peer_port) {
105     stop();
106     thread=new SocketCreator(local, remote, peer_port);
107     thread.start();
108     }
109
110
111     void start(OutputStream JavaDoc out, byte[] buf) {
112     stop();
113     thread=new WriterThread(out, buf);
114     thread.start();
115     }
116
117
118     void start(OutputStream JavaDoc out, int i) {
119     stop();
120     thread=new WriterThread(out, i);
121     thread.start();
122     }
123
124
125
126     void stop() {
127     if(thread != null && thread.isAlive()) {
128         thread.interrupt();
129         try {thread.join(timeout);}
130         catch(Exception JavaDoc e) {}
131     }
132     }
133
134     
135     /**
136        Writes data to an output stream. If the method does not return within timeout milliseconds,
137        a Timeout exception will be thrown.
138      */

139     public synchronized void write(OutputStream JavaDoc out, byte[] buf, long timeout)
140     throws Exception JavaDoc, Timeout, InterruptedException JavaDoc {
141     if(out == null || buf == null) {
142         System.err.println("TimedWriter.write(): output stream or buffer is null, ignoring write");
143         return;
144     }
145
146     try {
147         this.timeout=timeout;
148         completed=false;
149         start(out, buf);
150         if(thread == null)
151         return;
152         
153         thread.join(timeout);
154
155         if(completed == false) {
156         throw new Timeout();
157         }
158         if(write_ex != null) {
159         Exception JavaDoc tmp=write_ex;
160         write_ex=null;
161         throw tmp;
162         }
163     }
164     finally { // stop the thread in any case
165
stop();
166     }
167     }
168
169
170     public synchronized void write(OutputStream JavaDoc out, int i, long timeout)
171     throws Exception JavaDoc, Timeout, InterruptedException JavaDoc {
172     if(out == null) {
173         System.err.println("TimedWriter.write(): output stream is null, ignoring write");
174         return;
175     }
176
177     try {
178         this.timeout=timeout;
179         completed=false;
180         start(out, i);
181         if(thread == null)
182         return;
183         
184         thread.join(timeout);
185         if(completed == false) {
186         throw new Timeout();
187         }
188         if(write_ex != null) {
189         Exception JavaDoc tmp=write_ex;
190         write_ex=null;
191         throw tmp;
192         }
193     }
194     finally { // stop the thread in any case
195
stop();
196     }
197     }
198
199
200     /** Tries to create a socket to remote_peer:remote_port. If not sucessful within timeout
201     milliseconds, throws the Timeout exception. Otherwise, returns the socket or throws an
202     IOException. */

203     public synchronized Socket JavaDoc createSocket(InetAddress JavaDoc local, InetAddress JavaDoc remote, int port, long timeout)
204     throws Exception JavaDoc, Timeout, InterruptedException JavaDoc {
205     Socket JavaDoc ret=null;
206     
207     try {
208         this.timeout=timeout;
209         completed=false;
210         start(local, remote, port);
211         if(thread == null)
212         return null;
213         
214         thread.join(timeout);
215         if(completed == false) {
216         throw new Timeout();
217         }
218         if(write_ex != null) {
219         Exception JavaDoc tmp=write_ex;
220         write_ex=null;
221         throw tmp;
222         }
223         return sock;
224     }
225     finally { // stop the thread in any case
226
stop();
227     }
228     }
229
230
231
232
233     public static void main(String JavaDoc[] args) {
234     TimedWriter w=new TimedWriter();
235     InetAddress JavaDoc local=null;
236     InetAddress JavaDoc remote=null;
237     int port=0;
238     Socket JavaDoc sock=null ;
239     
240     if(args.length != 3) {
241         System.err.println("TimedWriter <local host> <remote host> <remote port>");
242         return;
243     }
244
245     try {
246         local=InetAddress.getByName(args[0]);
247         remote=InetAddress.getByName(args[1]);
248         port=Integer.parseInt(args[2]);
249     }
250     catch(Exception JavaDoc e) {
251         System.err.println("Could find host " + remote);
252         return;
253     }
254
255     while(true) {
256         
257         try {
258         sock=w.createSocket(local, remote, port, 3000);
259         if(sock != null) {
260             System.out.println("Connection created");
261             return;
262         }
263         }
264         catch(TimedWriter.Timeout timeout) {
265         System.err.println("Timed out creating socket");
266         }
267         catch(Exception JavaDoc io_ex) {
268         System.err.println("Connection could not be created, retrying");
269         Util.sleep(2000);
270         }
271     }
272
273     }
274 }
275
Popular Tags