View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * Timeout listener for Data connection for FTP4J model
34   */
35  public class DataTimeOutListener implements FTPDataTransferListener {
36    /**
37     * Internal Logger
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              // nothing
74            } catch (final FTPIllegalReplyException ignored) {
75              // nothing
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 }