1 18 package org.drftpd.slave.socket; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.rmi.RemoteException ; 23 import java.util.Hashtable ; 24 25 import net.sf.drftpd.slave.Transfer; 26 import net.sf.drftpd.slave.TransferStatus; 27 28 import org.apache.log4j.Logger; 29 30 34 public class SocketTransferImpl implements Transfer { 35 36 private static final Logger logger = Logger.getLogger(SocketTransferImpl.class); 37 38 private long _conn; 39 private String _addr; 40 private int _port; 41 42 private boolean _abort = false; 43 44 private char _direction; 45 private long _started = 0; 46 private long _finished = 0; 47 private long _transfered = 0; 48 private long _checksum = 0; 49 private long _error = 0; 50 private String _status = ""; 51 52 private SocketSlaveImpl _slave; 53 54 57 public SocketTransferImpl(SocketSlaveImpl slave, Hashtable data) 58 throws RemoteException 59 { 60 _slave = slave; 61 _direction = Transfer.TRANSFER_UNKNOWN; 62 _conn = Long.parseLong((String )data.get("conn")); 63 _port = 0; 64 _addr = ""; 65 if (data.containsKey("addr")) { 66 String tmp = (String )data.get("addr"); 67 String [] items = tmp.split(":"); 68 _addr = items[0]; 69 _port = Integer.parseInt(items[1]); 70 } 71 } 72 73 public long getChecksum() { 74 return _checksum; 75 } 76 77 public char getDirection() { 78 return _direction; 79 } 80 81 public int getLocalPort() throws RemoteException { 82 return _port; 83 } 84 85 public long getID() { 86 return _conn; 87 } 88 89 public long getTransfered() { 90 return _transfered; 91 } 92 93 public long getElapsed() { 94 if (_finished == 0) { 95 return System.currentTimeMillis() - _started; 96 } else { 97 return _finished - _started; 98 } 99 } 100 101 public int getXferSpeed() { 102 long elapsed = getElapsed(); 103 104 if (_transfered == 0) { 105 return 0; 106 } 107 108 if (elapsed == 0) { 109 return 0; 110 } 111 return (int) (_transfered / ((float) elapsed / (float) 1000)); 112 } 113 114 public TransferStatus getStatus() { 115 try { 116 return new TransferStatus(getElapsed(), getTransfered(), getChecksum(), InetAddress.getLocalHost()); 117 } catch (Exception e) { 118 return null; 119 } 120 } 121 122 public boolean isReceivingUploading() { 123 return _direction == TRANSFER_RECEIVING_UPLOAD; 124 } 125 126 public boolean isSendingUploading() { 127 return _direction == Transfer.TRANSFER_SENDING_DOWNLOAD; 128 } 129 130 public TransferStatus sendFile( 131 String path, 132 char type, 133 long resumePosition 134 ) throws IOException { 135 Hashtable args = new Hashtable (); 136 137 logger.info("Send: path=" + path); 138 139 _direction = TRANSFER_SENDING_DOWNLOAD; 140 args.put("path", path); 141 args.put("offs", Long.toString(resumePosition)); 142 args.put("conn", Long.toString(_conn)); 143 Hashtable data = _slave.doCommand("send", args); 144 _started = System.currentTimeMillis(); 145 if (data != null) { 146 _slave.addTransfer(this); 147 while (!_status.equals("C")) { 148 try { wait(200); } catch (Exception e) {} 149 } 150 _slave.removeTransfer(this); 151 } 152 TransferStatus tmp = getStatus(); 153 return tmp; 154 } 155 156 public synchronized TransferStatus receiveFile( 157 String dirname, 158 char mode, 159 String filename, 160 long offset 161 ) throws IOException { 162 Hashtable args = new Hashtable (); 163 164 logger.info("Recv: path=" + dirname + "/" + filename); 165 166 _direction = TRANSFER_RECEIVING_UPLOAD; 167 args.put("path", dirname + "/" + filename); 168 args.put("offs", Long.toString(offset)); 169 args.put("conn", Long.toString(_conn)); 170 Hashtable data = _slave.doCommand("recv", args); 171 _started = System.currentTimeMillis(); 172 if (data != null) { 173 _status = "I"; 174 _slave.addTransfer(this); 175 while (!_status.equals("C")) { 176 try { wait(200); } catch (Exception e) {} 177 } 178 _slave.removeTransfer(this); 179 } 180 TransferStatus tmp = getStatus(); 181 return tmp; 182 } 183 184 public void abort() throws RemoteException { 185 _abort = true; 186 Hashtable args = new Hashtable (); 187 args.put("conn", Long.toString(_conn)); 188 Hashtable data = _slave.doCommand("abrt", args); 189 } 190 191 192 public void updateStats(String sta, long byt, long crc, long err, String addr) 193 { 194 _status = sta; 195 _transfered = byt; 196 _checksum = crc; 197 _error = err; 198 _addr = addr; 199 } 200 } 201 | Popular Tags |