1 18 package net.sf.drftpd.slave; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.Socket ; 28 import java.rmi.RemoteException ; 29 import java.rmi.server.UnicastRemoteObject ; 30 import java.util.zip.CRC32 ; 31 import java.util.zip.CheckedInputStream ; 32 import java.util.zip.CheckedOutputStream ; 33 34 import net.sf.drftpd.FileExistsException; 35 import net.sf.drftpd.util.AddAsciiOutputStream; 36 37 import org.apache.log4j.Logger; 38 39 43 public class TransferImpl extends UnicastRemoteObject implements Transfer { 44 private static final Logger logger = Logger.getLogger(TransferImpl.class); 45 private boolean _abort = false; 46 private CRC32 _checksum = null; 47 private Connection _conn; 48 private char _direction; 49 50 private InputStream _in; 51 private OutputStream _out; 52 private char _mode = 'I'; 53 private Socket _sock; 54 55 private long _started = 0; 56 private long _finished = 0; 57 58 private long _transfered = 0; 59 private SlaveImpl _slave; 60 63 public TransferImpl(Connection conn, SlaveImpl slave) 64 throws RemoteException { 65 super(0); 66 _slave = slave; 67 _direction = Transfer.TRANSFER_UNKNOWN; 68 _conn = conn; 69 } 70 71 public void abort() throws RemoteException { 72 _abort = true; 73 if (_conn != null) 74 _conn.abort(); 75 if (_sock == null ) 76 return; 78 try { 79 _sock.close(); 80 } catch (IOException e) { 81 logger.warn("abort() failed to close() the socket", e); 82 } 83 } 84 85 public TransferStatus sendFile( 86 String path, 87 char type, 88 long resumePosition) 89 throws IOException { 90 _direction = TRANSFER_SENDING_DOWNLOAD; 91 92 _in = new FileInputStream (_slave.getRoots().getFile(path)); 93 if (_slave.getDownloadChecksums()) { 94 _checksum = new CRC32 (); 95 _in = new CheckedInputStream (_in, _checksum); 96 } 97 _in.skip(resumePosition); 98 99 System.out.println("DL:" + path); 100 transfer(); 101 return getStatus(); 102 } 103 104 public long getChecksum() { 105 if (_checksum == null) 106 return 0; 107 return _checksum.getValue(); 108 } 109 110 public char getDirection() { 111 return _direction; 112 } 113 114 public int getLocalPort() throws RemoteException { 115 if (_conn instanceof PassiveConnection) { 116 return ((PassiveConnection) _conn).getLocalPort(); 117 } else { 118 throw new IllegalStateException ("getLocalPort() called on a non-passive transfer"); 119 } 120 } 121 122 public long getTransfered() { 123 return _transfered; 124 } 125 126 public long getElapsed() { 127 if (_finished == 0) { 128 return System.currentTimeMillis() - _started; 129 } else { 130 return _finished - _started; 131 } 132 } 133 134 public int getXferSpeed() { 135 long elapsed = getElapsed(); 136 137 if (_transfered == 0) { 138 return 0; 139 } 140 141 if (elapsed == 0) { 142 return 0; 143 } 144 return (int) (_transfered / ((float) elapsed / (float) 1000)); 145 } 146 147 public TransferStatus getStatus() { 148 return new TransferStatus(getElapsed(), getTransfered(), getChecksum(), _sock.getInetAddress()); 149 } 150 151 public boolean isReceivingUploading() { 152 return _direction == TRANSFER_RECEIVING_UPLOAD; 153 } 154 155 public boolean isSendingUploading() { 156 return _direction == Transfer.TRANSFER_SENDING_DOWNLOAD; 157 } 158 159 186 private TransferStatus transfer() throws IOException { 187 _started = System.currentTimeMillis(); 188 _sock = _conn.connect(); 189 _conn = null; 190 int bufsize = _slave.getBufferSize(); 191 if (_in == null) { 192 if(bufsize > 0) _sock.setReceiveBufferSize(bufsize); 193 _in = _sock.getInputStream(); 194 } else if (_out == null) { 195 if(bufsize > 0) _sock.setSendBufferSize(bufsize); 196 _out = _sock.getOutputStream(); 197 } else { 198 throw new IllegalStateException ("neither in or out was null"); 199 } 200 if (_mode == 'A') { 201 _out = new AddAsciiOutputStream(_out); 202 } 203 204 _slave.addTransfer(this); 205 TransferStatus status; 206 try { 207 byte[] buff = new byte[Math.max(_slave.getBufferSize(), 65535)]; 208 int count; 209 try { 210 while ((count = _in.read(buff)) != -1 && !_abort) { 211 _transfered += count; 212 _out.write(buff, 0, count); 213 } 214 _out.flush(); 215 } catch (IOException e) { 216 throw new TransferFailedException(e, getStatus()); 217 } 218 } finally { 219 _finished = System.currentTimeMillis(); 220 status = getStatus(); 221 _slave.removeTransfer(this); 222 223 _in.close(); 224 _out.close(); 225 _sock.close(); 226 227 _in = null; 228 _out = null; 229 } 231 if (_abort) 232 throw new TransferFailedException("Transfer was aborted", getStatus()); 233 return status; 234 } 235 236 public TransferStatus receiveFile( 237 String dirname, 238 char mode, 239 String filename, 240 long offset) 241 throws IOException { 242 _direction = TRANSFER_RECEIVING_UPLOAD; 243 try { 244 _slave.getRoots().getFile(dirname + File.separator + filename); 245 throw new FileExistsException("File exists"); 246 } catch (FileNotFoundException ex) { 247 } 248 249 String root = _slave.getRoots().getARootFileDir(dirname).getPath(); 250 251 _out = new FileOutputStream (root + File.separator + filename); 252 253 if (_slave.getUploadChecksums()) { 254 _checksum = new CRC32 (); 255 _out = new CheckedOutputStream (_out, _checksum); 256 } 257 System.out.println("UL:" + dirname + File.separator + filename); 258 try { 259 return transfer(); 260 } catch (IOException e) { 261 throw e; } 265 } 266 } 267 | Popular Tags |