KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slave > async > AsyncTransfer


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.async;
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: AsyncTransfer.java,v 1.2 2004/05/21 18:42:01 zombiewoof64 Exp $
33  */

34 public class AsyncTransfer implements Transfer {
35     
36     private static final Logger logger = Logger.getLogger(AsyncTransfer.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 AsyncSlave _slave;
53     private AsyncCommand _cmd;
54     
55     /**
56      * Start undefined passive transfer.
57      */

58     public AsyncTransfer(AsyncSlave slave, AsyncCommand cmd)
59     {
60         Hashtable JavaDoc data = cmd._data;
61         _slave = slave;
62         _direction = Transfer.TRANSFER_UNKNOWN;
63         _conn = Long.parseLong((String JavaDoc)data.get("conn"));
64         _port = 0;
65         _addr = "";
66         if (data.containsKey("addr")) {
67             String JavaDoc tmp = (String JavaDoc)data.get("addr");
68             String JavaDoc[] items = tmp.split(":");
69             _addr = items[0];
70             _port = Integer.parseInt(items[1]);
71         }
72     }
73     
74     public long getChecksum() {
75         return _checksum;
76     }
77     
78     public char getDirection() {
79         return _direction;
80     }
81     
82     public int getLocalPort() throws RemoteException JavaDoc {
83         return _port;
84     }
85     
86     public long getID() {
87         return _conn;
88     }
89     
90     public long getTransfered() {
91         return _transfered;
92     }
93     
94     public long getElapsed() {
95         if (_finished == 0) {
96             return System.currentTimeMillis() - _started;
97         } else {
98             return _finished - _started;
99         }
100     }
101     
102     public int getXferSpeed() {
103         long elapsed = getElapsed();
104         
105         if (_transfered == 0) {
106             return 0;
107         }
108         
109         if (elapsed == 0) {
110             return 0;
111         }
112         return (int) (_transfered / ((float) elapsed / (float) 1000));
113     }
114     
115     public TransferStatus getStatus() {
116         try {
117             return new TransferStatus(getElapsed(), getTransfered(), getChecksum(), InetAddress.getLocalHost());
118         } catch (Exception JavaDoc e) {
119             return null;
120         }
121     }
122     
123     public boolean isReceivingUploading() {
124         return _direction == TRANSFER_RECEIVING_UPLOAD;
125     }
126     
127     public boolean isSendingUploading() {
128         return _direction == Transfer.TRANSFER_SENDING_DOWNLOAD;
129     }
130     
131     public TransferStatus sendFile(
132     String JavaDoc path,
133     char type,
134     long resumePosition
135     ) throws IOException JavaDoc {
136         logger.info("Send: path=" + path);
137       
138         _direction = TRANSFER_SENDING_DOWNLOAD;
139         _cmd = _slave.sendCommand(
140         "send", "\"" + path + "\" " + resumePosition + " " + _conn
141         );
142         _started = System.currentTimeMillis();
143         _slave.addTransfer(this);
144         _cmd.waitForComplete();
145         _slave.removeTransfer(this);
146         TransferStatus tmp = getStatus();
147         return tmp;
148     }
149     
150     public synchronized TransferStatus receiveFile(
151     String JavaDoc dirname,
152     char mode,
153     String JavaDoc filename,
154     long offset
155     ) throws IOException JavaDoc {
156         logger.info("Recv: path=" + dirname + "/" + filename);
157         
158         _direction = TRANSFER_RECEIVING_UPLOAD;
159         String JavaDoc args = "\"" + dirname + "/" + filename + "\" " + offset + " " + _conn;
160         _cmd = _slave.sendCommand("recv", args);
161         _started = System.currentTimeMillis();
162         _slave.addTransfer(this);
163         _cmd.waitForComplete();
164         _slave.removeTransfer(this);
165         TransferStatus tmp = getStatus();
166         return tmp;
167     }
168     
169     public void startSend(
170     String JavaDoc path,
171     char type,
172     long resumePosition
173     ) throws IOException JavaDoc {
174         logger.info("Send: path=" + path);
175       
176         _direction = TRANSFER_SENDING_DOWNLOAD;
177         _cmd = _slave.sendCommand(
178         "send", "\"" + path + "\" " + resumePosition + " " + _conn
179         );
180         _started = System.currentTimeMillis();
181         _slave.addTransfer(this);
182     }
183     public TransferStatus finishSend()
184     {
185         _slave.removeTransfer(this);
186         TransferStatus tmp = getStatus();
187         return tmp;
188     }
189     
190     public void startRecv(
191     String JavaDoc dirname,
192     char mode,
193     String JavaDoc filename,
194     long offset
195     ) throws IOException JavaDoc {
196         logger.info("Recv: path=" + dirname + "/" + filename);
197         
198         _direction = TRANSFER_RECEIVING_UPLOAD;
199         String JavaDoc args = "\"" + dirname + "/" + filename + "\" " + offset + " " + _conn;
200         AsyncCommand cmd = _slave.sendCommand("recv", args);
201         _started = System.currentTimeMillis();
202         _slave.addTransfer(this);
203     }
204     
205     public TransferStatus finishRecv()
206     {
207         _slave.removeTransfer(this);
208         TransferStatus tmp = getStatus();
209         return tmp;
210     }
211     
212     public void abort() throws RemoteException JavaDoc {
213         _abort = true;
214         _cmd.abort();
215     }
216     
217     
218     public void updateStats(String JavaDoc sta, long byt, long crc, long err, String JavaDoc addr) {
219         _status = sta;
220         _transfered = byt;
221         _checksum = crc;
222         _error = err;
223         _addr = addr;
224     }
225 }
226
Popular Tags