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.openr66.protocol.utils;
21  
22  import io.netty.channel.Channel;
23  import io.netty.channel.ChannelFuture;
24  import io.netty.util.Timeout;
25  import io.netty.util.TimerTask;
26  import org.waarp.common.crypto.ssl.WaarpSslUtility;
27  import org.waarp.common.database.DbSession;
28  import org.waarp.common.utility.WaarpNettyUtil;
29  import org.waarp.openr66.protocol.configuration.Configuration;
30  import org.waarp.openr66.protocol.localhandler.ConnectionActions;
31  
32  import java.util.concurrent.TimeUnit;
33  
34  import static org.waarp.common.database.DbConstant.*;
35  
36  /**
37   * TimerTask to Close a Channel in the future
38   */
39  public class ChannelCloseTimer implements TimerTask {
40  
41    private Channel channel;
42    private ChannelFuture future;
43    private ConnectionActions connectionActions;
44    private DbSession noConcurrencyDbSession;
45  
46    public ChannelCloseTimer(final Channel channel) {
47      this.channel = channel;
48    }
49  
50    public ChannelCloseTimer(final Channel channel, final ChannelFuture future) {
51      this.channel = channel;
52      this.future = future;
53    }
54  
55    public ChannelCloseTimer(final ConnectionActions connectionActions) {
56      this.connectionActions = connectionActions;
57    }
58  
59    @Override
60    public void run(final Timeout timeout) {
61      if (future != null) {
62        WaarpNettyUtil.awaitOrInterrupted(future);
63      }
64      if (connectionActions != null) {
65        if (noConcurrencyDbSession != null && admin != null &&
66            admin.getSession() != null &&
67            !noConcurrencyDbSession.equals(admin.getSession())) {
68          noConcurrencyDbSession.forceDisconnect();
69          noConcurrencyDbSession = null;
70        }
71        connectionActions.getLocalChannelReference().close();
72      } else if (channel != null) {
73        WaarpSslUtility.closingSslChannel(channel);
74      }
75    }
76  
77    /**
78     * Close in the future this transaction (may need more than 1 WAITFORNETOP)
79     *
80     * @param connectionActions
81     */
82    public static void closeFutureTransaction(
83        final ConnectionActions connectionActions) {
84      if (Configuration.configuration.isTimerCloseReady()) {
85        Configuration.configuration.getTimerClose().newTimeout(
86            new ChannelCloseTimer(connectionActions), Configuration.WAITFORNETOP,
87            TimeUnit.MILLISECONDS);
88      }
89    }
90  
91    public final void setDbSession(final DbSession dbSession) {
92      noConcurrencyDbSession = dbSession;
93    }
94  
95    /**
96     * Close in the future this channel
97     *
98     * @param channel
99     */
100   public static void closeFutureChannel(final Channel channel) {
101     if (Configuration.configuration.isTimerCloseReady()) {
102       Configuration.configuration.getTimerClose()
103                                  .newTimeout(new ChannelCloseTimer(channel),
104                                              Configuration.WAITFORNETOP,
105                                              TimeUnit.MILLISECONDS);
106     }
107   }
108 
109 }