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.networkhandler;
21  
22  import io.netty.channel.ChannelInitializer;
23  import io.netty.channel.ChannelPipeline;
24  import io.netty.channel.socket.SocketChannel;
25  import io.netty.handler.timeout.IdleStateHandler;
26  import io.netty.handler.traffic.ChannelTrafficShapingHandler;
27  import io.netty.handler.traffic.GlobalTrafficShapingHandler;
28  import org.waarp.common.logging.WaarpLogger;
29  import org.waarp.common.logging.WaarpLoggerFactory;
30  import org.waarp.openr66.protocol.configuration.Configuration;
31  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNetworkException;
32  import org.waarp.openr66.protocol.networkhandler.packet.NetworkPacketCodec;
33  
34  import java.util.concurrent.TimeUnit;
35  
36  /**
37   * NetworkServer pipeline (Requester side)
38   */
39  public class NetworkServerInitializer
40      extends ChannelInitializer<SocketChannel> {
41    /**
42     * Internal Logger
43     */
44    private static final WaarpLogger logger =
45        WaarpLoggerFactory.getLogger(NetworkServerInitializer.class);
46  
47    public static final String TIMEOUT = "timeout";
48    public static final String LIMITGLOBAL = "GLOBALLIMIT";
49    public static final String LIMITCHANNEL = "CHANNELLIMIT";
50    public static final String NETWORK_CODEC = "codec";
51    public static final String NETWORK_HANDLER = "handler";
52  
53    protected final boolean server;
54  
55    public NetworkServerInitializer(final boolean server) {
56      this.server = server;
57    }
58  
59    @Override
60    protected void initChannel(final SocketChannel ch) throws Exception {
61      final ChannelPipeline pipeline = ch.pipeline();
62      logger.debug("Create IdleStateHandler with {} ms",
63                   Configuration.configuration.getTimeoutCon());
64      pipeline.addLast(TIMEOUT, new IdleStateHandler(true, 0, 0,
65                                                     Configuration.configuration.getTimeoutCon(),
66                                                     TimeUnit.MILLISECONDS));
67      // Global limitation
68      final GlobalTrafficShapingHandler handler =
69          Configuration.configuration.getGlobalTrafficShapingHandler();
70      if (handler == null) {
71        throw new OpenR66ProtocolNetworkException(
72            "Error at pipeline initialization," +
73            " GlobalTrafficShapingHandler configured.");
74      }
75      pipeline.addLast(LIMITGLOBAL, handler);
76      // Per channel limitation
77      pipeline.addLast(LIMITCHANNEL, new ChannelTrafficShapingHandler(
78          Configuration.configuration.getServerChannelWriteLimit(),
79          Configuration.configuration.getServerChannelReadLimit(),
80          Configuration.configuration.getDelayLimit(),
81          Configuration.configuration.getTimeoutCon()));
82      pipeline.addLast(NETWORK_CODEC, new NetworkPacketCodec());
83      pipeline.addLast(Configuration.configuration.getHandlerGroup(),
84                       NETWORK_HANDLER, new NetworkServerHandler());
85    }
86  }