View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.networkhandler;
22  
23  import java.util.concurrent.TimeUnit;
24  
25  import openr66.protocol.configuration.Configuration;
26  import openr66.protocol.exception.OpenR66ProtocolNoDataException;
27  import openr66.protocol.networkhandler.packet.NetworkPacketCodec;
28  
29  import org.jboss.netty.channel.ChannelPipeline;
30  import org.jboss.netty.channel.ChannelPipelineFactory;
31  import org.jboss.netty.channel.Channels;
32  import org.jboss.netty.handler.execution.ExecutionHandler;
33  import org.jboss.netty.handler.timeout.IdleStateHandler;
34  import org.jboss.netty.handler.traffic.ChannelTrafficShapingHandler;
35  import org.jboss.netty.handler.traffic.GlobalTrafficShapingHandler;
36  import org.jboss.netty.util.HashedWheelTimer;
37  
38  /**
39   * NetworkServer pipeline (Requester side)
40   * @author Frederic Bregier
41   */
42  public class NetworkServerPipelineFactory implements ChannelPipelineFactory {
43      /**
44       * Global HashedWheelTimer
45       */
46      public HashedWheelTimer timer = (HashedWheelTimer) Configuration.configuration.getTimerClose();
47  
48      public static final String TIMEOUT = "timeout";
49      public static final String READTIMEOUT = "readTimeout";
50      public static final String LIMIT = "LIMIT";
51      public static final String LIMITCHANNEL = "LIMITCHANNEL";
52  
53      private boolean server = false;
54      public NetworkServerPipelineFactory(boolean server) {
55          this.server = server;
56      }
57      @Override
58      public ChannelPipeline getPipeline() {
59          final ChannelPipeline pipeline = Channels.pipeline();
60          pipeline.addLast("codec", new NetworkPacketCodec());
61          GlobalTrafficShapingHandler handler =
62              Configuration.configuration.getGlobalTrafficShapingHandler();
63          if (handler != null) {
64              pipeline.addLast(LIMIT, handler);
65          }
66          ChannelTrafficShapingHandler trafficChannel = null;
67          try {
68              trafficChannel =
69                  Configuration.configuration
70                  .newChannelTrafficShapingHandler();
71              if (trafficChannel != null) {
72                  pipeline.addLast(LIMITCHANNEL, trafficChannel);
73              }
74          } catch (OpenR66ProtocolNoDataException e) {
75          }
76          pipeline.addLast("pipelineExecutor", new ExecutionHandler(
77                  Configuration.configuration.getServerPipelineExecutor()));
78          
79          pipeline.addLast(TIMEOUT,
80                  new IdleStateHandler(timer,
81                          0, 0, 
82                          Configuration.configuration.TIMEOUTCON, 
83                          TimeUnit.MILLISECONDS));
84          pipeline.addLast("handler", new NetworkServerHandler(this.server));
85          return pipeline;
86      }
87  
88  }