1 3 package org.jgroups.util; 4 5 6 7 import java.io.DataOutputStream ; 8 import java.io.IOException ; 9 import java.io.OutputStream ; 10 import java.net.InetAddress ; 11 import java.net.Socket ; 12 13 22 23 24 public class TimedWriter { 25 Thread thread=null; 26 long timeout=2000; 27 boolean completed=true; 28 Exception write_ex=null; 29 Socket sock=null; 30 31 32 class Timeout extends Exception { 33 public String toString() { 34 return "TimedWriter.Timeout"; 35 } 36 } 37 38 39 40 class WriterThread extends Thread { 41 DataOutputStream out=null; 42 byte[] buf=null; 43 int i=0; 44 45 46 public WriterThread(OutputStream out, byte[] buf) { 47 this.out=new DataOutputStream (out); 48 this.buf=buf; 49 setName("TimedWriter.WriterThread"); 50 } 51 52 public WriterThread(OutputStream out, int i) { 53 this.out=new DataOutputStream (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 e) { 68 write_ex=e; 69 } 70 completed=true; 71 } 72 } 73 74 75 class SocketCreator extends Thread { 76 InetAddress local=null, remote=null; 77 int peer_port=0; 78 79 80 public SocketCreator(InetAddress local, InetAddress 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 (remote, peer_port, local, 0); } 94 catch(IOException io_ex) { 95 write_ex=io_ex; 96 } 97 completed=true; 98 } 99 } 100 101 102 103 104 void start(InetAddress local, InetAddress remote, int peer_port) { 105 stop(); 106 thread=new SocketCreator(local, remote, peer_port); 107 thread.start(); 108 } 109 110 111 void start(OutputStream out, byte[] buf) { 112 stop(); 113 thread=new WriterThread(out, buf); 114 thread.start(); 115 } 116 117 118 void start(OutputStream 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 e) {} 131 } 132 } 133 134 135 139 public synchronized void write(OutputStream out, byte[] buf, long timeout) 140 throws Exception , Timeout, InterruptedException { 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 tmp=write_ex; 160 write_ex=null; 161 throw tmp; 162 } 163 } 164 finally { stop(); 166 } 167 } 168 169 170 public synchronized void write(OutputStream out, int i, long timeout) 171 throws Exception , Timeout, InterruptedException { 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 tmp=write_ex; 190 write_ex=null; 191 throw tmp; 192 } 193 } 194 finally { stop(); 196 } 197 } 198 199 200 203 public synchronized Socket createSocket(InetAddress local, InetAddress remote, int port, long timeout) 204 throws Exception , Timeout, InterruptedException { 205 Socket 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 tmp=write_ex; 220 write_ex=null; 221 throw tmp; 222 } 223 return sock; 224 } 225 finally { stop(); 227 } 228 } 229 230 231 232 233 public static void main(String [] args) { 234 TimedWriter w=new TimedWriter(); 235 InetAddress local=null; 236 InetAddress remote=null; 237 int port=0; 238 Socket 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 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 io_ex) { 268 System.err.println("Connection could not be created, retrying"); 269 Util.sleep(2000); 270 } 271 } 272 273 } 274 } 275 | Popular Tags |