1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.waarp.ftp.client;
19
20 import java.io.IOException;
21 import java.util.Timer;
22 import java.util.TimerTask;
23
24 import org.waarp.common.logging.WaarpLogger;
25 import org.waarp.common.logging.WaarpLoggerFactory;
26
27 import it.sauronsoftware.ftp4j.FTPClient;
28 import it.sauronsoftware.ftp4j.FTPDataTransferListener;
29 import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
30
31
32
33
34
35
36
37 public class DataTimeOutListener implements FTPDataTransferListener {
38
39
40
41 private static final WaarpLogger logger = WaarpLoggerFactory.getLogger(DataTimeOutListener.class);
42
43 private final FTPClient client;
44 private Timer timer;
45 private long timeout = 10000;
46 private long last = System.currentTimeMillis();
47 private boolean finished = false;
48 private String command;
49 private String file;
50
51 public DataTimeOutListener(FTPClient client, long timeout, String command, String file) {
52 this.client = client;
53 timer = new Timer(true);
54 this.timeout = timeout;
55 this.command = command;
56 this.file = file;
57 }
58
59 private void renewTask() {
60 TimerTask task = new TimerTask() {
61 public void run() {
62 if (finished) {
63 return;
64 }
65 long now = System.currentTimeMillis();
66 if (now - last - timeout > 0) {
67 try {
68 logger.warn("Timeout during file transfer: " + command + " " + file);
69 client.abortCurrentDataTransfer(true);
70 } catch (IOException e) {
71 } catch (FTPIllegalReplyException e) {
72 }
73 } else {
74 renewTask();
75 }
76 }
77 };
78 timer.schedule(task, timeout);
79 }
80
81 @Override
82 public void started() {
83 renewTask();
84 last = System.currentTimeMillis();
85 }
86
87 @Override
88 public void transferred(int length) {
89 last = System.currentTimeMillis();
90 }
91
92 @Override
93 public void completed() {
94 finished = true;
95 last = System.currentTimeMillis();
96 timer.cancel();
97 }
98
99 @Override
100 public void aborted() {
101 finished = true;
102 last = System.currentTimeMillis();
103 timer.cancel();
104 }
105
106 @Override
107 public void failed() {
108 finished = true;
109 last = System.currentTimeMillis();
110 timer.cancel();
111 }
112
113 }