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