1 17 package org.alfresco.filesys.ftp; 18 19 import java.net.*; 20 import java.io.*; 21 22 33 public class FTPDataSession implements Runnable 34 { 35 36 38 private FTPSrvSession m_cmdSess; 39 40 42 private InetAddress m_clientAddr; 43 private int m_clientPort; 44 45 47 private int m_localPort; 48 49 51 private Socket m_activeSock; 52 53 55 private ServerSocket m_passiveSock; 56 57 59 private InetAddress m_bindAddr; 60 61 63 private boolean m_transfer; 64 private boolean m_abort; 65 66 68 private long m_bytCount; 69 70 78 protected FTPDataSession(FTPSrvSession sess) throws IOException 79 { 80 81 83 m_cmdSess = sess; 84 85 87 m_passiveSock = new ServerSocket(0, 1, null); 88 } 89 90 101 protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress bindAddr) throws IOException 102 { 103 104 106 m_cmdSess = sess; 107 108 111 m_localPort = localPort; 112 m_passiveSock = new ServerSocket(localPort, 1, bindAddr); 113 } 114 115 125 protected FTPDataSession(FTPSrvSession sess, InetAddress bindAddr) throws IOException 126 { 127 128 130 m_cmdSess = sess; 131 132 135 m_passiveSock = new ServerSocket(0, 1, bindAddr); 136 } 137 138 147 protected FTPDataSession(FTPSrvSession sess, InetAddress addr, int port) 148 { 149 150 152 m_cmdSess = sess; 153 154 157 m_clientAddr = addr; 158 m_clientPort = port; 159 } 160 161 172 protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress addr, int port) 173 { 174 175 177 m_cmdSess = sess; 178 179 181 m_localPort = localPort; 182 183 186 m_clientAddr = addr; 187 m_clientPort = port; 188 } 189 190 195 public final FTPSrvSession getCommandSession() 196 { 197 return m_cmdSess; 198 } 199 200 205 public final int getLocalPort() 206 { 207 if (m_passiveSock != null) 208 return m_passiveSock.getLocalPort(); 209 else if (m_activeSock != null) 210 return m_activeSock.getLocalPort(); 211 return -1; 212 } 213 214 219 protected final int getAllocatedPort() 220 { 221 return m_localPort; 222 } 223 224 229 public final InetAddress getPassiveAddress() 230 { 231 if (m_passiveSock != null) 232 { 233 234 236 InetAddress addr = m_passiveSock.getInetAddress(); 237 if (addr.getHostAddress().compareTo("0.0.0.0") == 0) 238 { 239 try 240 { 241 addr = InetAddress.getLocalHost(); 242 } 243 catch (UnknownHostException ex) 244 { 245 } 246 } 247 return addr; 248 } 249 return null; 250 } 251 252 257 public final int getPassivePort() 258 { 259 if (m_passiveSock != null) 260 return m_passiveSock.getLocalPort(); 261 return -1; 262 } 263 264 269 public final boolean isTransferActive() 270 { 271 return m_transfer; 272 } 273 274 277 public final void abortTransfer() 278 { 279 m_abort = true; 280 } 281 282 287 public final synchronized long getTransferByteCount() 288 { 289 return m_bytCount; 290 } 291 292 298 public final Socket getSocket() throws IOException 299 { 300 301 303 if (m_passiveSock != null) 304 m_activeSock = m_passiveSock.accept(); 305 else 306 { 307 if (m_localPort != 0) 308 { 309 310 312 m_activeSock = new Socket(m_clientAddr, m_clientPort, null, m_localPort); 313 } 314 else 315 m_activeSock = new Socket(m_clientAddr, m_clientPort); 316 } 317 318 320 m_activeSock.setSoLinger(false, 0); 321 m_activeSock.setTcpNoDelay(true); 322 323 325 return m_activeSock; 326 } 327 328 331 public final void closeSession() 332 { 333 334 336 if (m_activeSock != null) 337 { 338 try 339 { 340 m_activeSock.close(); 341 } 342 catch (Exception ex) 343 { 344 } 345 m_activeSock = null; 346 } 347 348 350 if (m_passiveSock != null) 351 { 352 try 353 { 354 m_passiveSock.close(); 355 } 356 catch (Exception ex) 357 { 358 } 359 m_passiveSock = null; 360 } 361 } 362 363 366 public void run() 367 { 368 } 369 } 370 | Popular Tags |