KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slave > socket > SocketTransferImpl


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.slave.socket;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import net.sf.drftpd.slave.Transfer;
26 import net.sf.drftpd.slave.TransferStatus;
27
28 import org.apache.log4j.Logger;
29
30 /**
31  * @author mog
32  * @version $Id: SocketTransferImpl.java,v 1.1 2004/05/20 20:17:24 zombiewoof64 Exp $
33  */

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 JavaDoc _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 JavaDoc _status = "";
51         
52     private SocketSlaveImpl _slave;
53
54         /**
55      * Start undefined passive transfer.
56      */

57     public SocketTransferImpl(SocketSlaveImpl slave, Hashtable JavaDoc data)
58         throws RemoteException JavaDoc
59         {
60         _slave = slave;
61         _direction = Transfer.TRANSFER_UNKNOWN;
62         _conn = Long.parseLong((String JavaDoc)data.get("conn"));
63                 _port = 0;
64                 _addr = "";
65                 if (data.containsKey("addr")) {
66                     String JavaDoc tmp = (String JavaDoc)data.get("addr");
67                     String JavaDoc[] 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 JavaDoc {
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 JavaDoc 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 JavaDoc path,
132         char type,
133         long resumePosition
134         ) throws IOException JavaDoc {
135             Hashtable JavaDoc args = new Hashtable JavaDoc();
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 JavaDoc 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 JavaDoc e) {}
149                 }
150                 _slave.removeTransfer(this);
151             }
152             TransferStatus tmp = getStatus();
153             return tmp;
154     }
155
156     public synchronized TransferStatus receiveFile(
157         String JavaDoc dirname,
158         char mode,
159         String JavaDoc filename,
160         long offset
161         ) throws IOException JavaDoc {
162             Hashtable JavaDoc args = new Hashtable JavaDoc();
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 JavaDoc 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 JavaDoc e) {}
177                 }
178                 _slave.removeTransfer(this);
179             }
180             TransferStatus tmp = getStatus();
181             return tmp;
182     }
183
184         public void abort() throws RemoteException JavaDoc {
185             _abort = true;
186             Hashtable JavaDoc args = new Hashtable JavaDoc();
187             args.put("conn", Long.toString(_conn));
188             Hashtable JavaDoc data = _slave.doCommand("abrt", args);
189     }
190
191
192         public void updateStats(String JavaDoc sta, long byt, long crc, long err, String JavaDoc addr)
193         {
194             _status = sta;
195             _transfered = byt;
196             _checksum = crc;
197             _error = err;
198         _addr = addr;
199         }
200 }
201
Popular Tags