1 16 package net.sf.jftp.net; 17 18 import java.io.BufferedOutputStream ; 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.PrintStream ; 23 import java.net.InetAddress ; 24 import java.net.Socket ; 25 26 import net.sf.jftp.config.Settings; 27 import net.sf.jftp.system.logging.Log; 28 29 30 35 public class JConnection implements Runnable 36 { 37 private int timeout = Settings.connectionTimeout; 38 private String host; 39 private int port; 40 private PrintStream out; 41 private BufferedReader in; 42 private Socket s; 43 private boolean isOk = false; 44 private boolean established = false; 45 private boolean reciever = false; 46 private Thread runner; 47 private int localPort = -1; 48 private int time = 0; 49 50 55 public JConnection(String host, int port) 56 { 57 this.host = host; 58 this.port = port; 59 60 runner = new Thread (this); 61 runner.start(); 62 } 63 64 public JConnection(String host, int port, int time) 65 { 66 this.host = host; 67 this.port = port; 68 this.timeout = time; 69 this.time = time; 70 71 runner = new Thread (this); 72 runner.start(); 73 } 74 75 82 public void run() 83 { 84 try 85 { 86 132 s = new Socket (host, port); 133 134 localPort = s.getLocalPort(); 135 136 out = new PrintStream (new BufferedOutputStream (s.getOutputStream(), 138 Settings.bufferSize)); 139 in = new BufferedReader (new InputStreamReader (s.getInputStream()), 140 Settings.bufferSize); 141 isOk = true; 142 143 } 145 catch(Exception ex) 146 { 147 ex.printStackTrace(); 148 Log.out("WARNING: connection closed due to exception (" + host + 149 ":" + port + ")"); 150 isOk = false; 151 152 try 153 { 154 if((s != null) && !s.isClosed()) 155 { 156 s.close(); 157 } 158 159 if(out != null) 160 { 161 out.close(); 162 } 163 164 if(in != null) 165 { 166 in.close(); 167 } 168 } 169 catch(Exception ex2) 170 { 171 ex2.printStackTrace(); 172 Log.out("WARNING: got more errors trying to close socket and streams"); 173 } 174 } 175 176 established = true; 177 } 178 179 public boolean isThere() 180 { 181 int cnt = 0; 182 183 while(!established && (cnt < timeout)) 184 { 185 pause(10); 186 cnt = cnt + 10; 187 188 } 190 191 return isOk; 192 } 193 194 public void send(String data) 195 { 196 try 197 { 198 out.print(data); 200 out.print("\r\n"); 201 out.flush(); 202 203 if(data.startsWith("PASS")) 204 { 205 Log.debug("> PASS ****"); 206 } 207 else 208 { 209 Log.debug("> " + data); 210 } 211 } 212 catch(Exception ex) 213 { 214 ex.printStackTrace(); 215 } 216 } 217 218 public PrintStream getInetOutputStream() 219 { 220 return out; 221 } 222 223 public BufferedReader getReader() 224 { 225 return in; 226 } 227 228 public int getLocalPort() 229 { 230 return localPort; 231 } 232 233 public InetAddress getLocalAddress() throws IOException 234 { 235 return s.getLocalAddress(); 236 } 237 238 private void pause(int time) 239 { 240 try 241 { 242 Thread.sleep(time); 243 } 244 catch(Exception ex) 245 { 246 } 247 } 248 249 public BufferedReader getIn() { 250 return in; 251 } 252 253 public void setIn(BufferedReader in) { 254 this.in = in; 255 } 256 257 public PrintStream getOut() { 258 return out; 259 } 260 261 public void setOut(PrintStream out) { 262 this.out = out; 263 } 264 } 265 | Popular Tags |