View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelFutureListener;
21  import io.netty.channel.ChannelHandler;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.channel.ChannelOption;
24  import io.netty.channel.ChannelPipeline;
25  import io.netty.util.AttributeKey;
26  import io.netty.util.concurrent.EventExecutor;
27  import io.netty.util.concurrent.Future;
28  import io.netty.util.concurrent.Promise;
29  import io.netty.util.internal.ObjectUtil;
30  import io.netty.util.internal.StringUtil;
31  import io.netty.util.internal.UnstableApi;
32  import io.netty.util.internal.logging.InternalLogger;
33  import io.netty.util.internal.logging.InternalLoggerFactory;
34  
35  import java.nio.channels.ClosedChannelException;
36  import java.util.LinkedHashMap;
37  import java.util.Map;
38  
39  @UnstableApi
40  public final class Http2StreamChannelBootstrap {
41      private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2StreamChannelBootstrap.class);
42  
43      private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
44      private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();
45      private final Channel channel;
46      private volatile ChannelHandler handler;
47  
48      public Http2StreamChannelBootstrap(Channel channel) {
49          this.channel = ObjectUtil.checkNotNull(channel, "channel");
50      }
51  
52      /**
53       * Allow to specify a {@link ChannelOption} which is used for the {@link Http2StreamChannel} instances once they got
54       * created. Use a value of {@code null} to remove a previous set {@link ChannelOption}.
55       */
56      @SuppressWarnings("unchecked")
57      public <T> Http2StreamChannelBootstrap option(ChannelOption<T> option, T value) {
58          if (option == null) {
59              throw new NullPointerException("option");
60          }
61          if (value == null) {
62              synchronized (options) {
63                  options.remove(option);
64              }
65          } else {
66              synchronized (options) {
67                  options.put(option, value);
68              }
69          }
70          return this;
71      }
72  
73      /**
74       * Allow to specify an initial attribute of the newly created {@link Http2StreamChannel}.  If the {@code value} is
75       * {@code null}, the attribute of the specified {@code key} is removed.
76       */
77      @SuppressWarnings("unchecked")
78      public <T> Http2StreamChannelBootstrap attr(AttributeKey<T> key, T value) {
79          if (key == null) {
80              throw new NullPointerException("key");
81          }
82          if (value == null) {
83              synchronized (attrs) {
84                  attrs.remove(key);
85              }
86          } else {
87              synchronized (attrs) {
88                  attrs.put(key, value);
89              }
90          }
91          return this;
92      }
93  
94      /**
95       * the {@link ChannelHandler} to use for serving the requests.
96       */
97      @SuppressWarnings("unchecked")
98      public Http2StreamChannelBootstrap handler(ChannelHandler handler) {
99          this.handler = ObjectUtil.checkNotNull(handler, "handler");
100         return this;
101     }
102 
103     public Future<Http2StreamChannel> open() {
104         return open(channel.eventLoop().<Http2StreamChannel>newPromise());
105     }
106 
107     public Future<Http2StreamChannel> open(final Promise<Http2StreamChannel> promise) {
108         final ChannelHandlerContext ctx = channel.pipeline().context(Http2MultiplexCodec.class);
109         if (ctx == null) {
110             if (channel.isActive()) {
111                 promise.setFailure(new IllegalStateException(StringUtil.simpleClassName(Http2MultiplexCodec.class) +
112                         " must be in the ChannelPipeline of Channel " + channel));
113             } else {
114                 promise.setFailure(new ClosedChannelException());
115             }
116         } else {
117             EventExecutor executor = ctx.executor();
118             if (executor.inEventLoop()) {
119                 open0(ctx, promise);
120             } else {
121                 executor.execute(new Runnable() {
122                     @Override
123                     public void run() {
124                         open0(ctx, promise);
125                     }
126                 });
127             }
128         }
129         return promise;
130     }
131 
132     public void open0(ChannelHandlerContext ctx, final Promise<Http2StreamChannel> promise) {
133         assert ctx.executor().inEventLoop();
134         final Http2StreamChannel streamChannel = ((Http2MultiplexCodec) ctx.handler()).newOutboundStream();
135         try {
136             init(streamChannel);
137         } catch (Exception e) {
138             streamChannel.unsafe().closeForcibly();
139             promise.setFailure(e);
140             return;
141         }
142 
143         ChannelFuture future = ctx.channel().eventLoop().register(streamChannel);
144         future.addListener(new ChannelFutureListener() {
145             @Override
146             public void operationComplete(ChannelFuture future) throws Exception {
147                 if (future.isSuccess()) {
148                     promise.setSuccess(streamChannel);
149                 } else if (future.isCancelled()) {
150                     promise.cancel(false);
151                 } else {
152                     if (streamChannel.isRegistered()) {
153                         streamChannel.close();
154                     } else {
155                         streamChannel.unsafe().closeForcibly();
156                     }
157 
158                     promise.setFailure(future.cause());
159                 }
160             }
161         });
162     }
163 
164     @SuppressWarnings("unchecked")
165     private void init(Channel channel) throws Exception {
166         ChannelPipeline p = channel.pipeline();
167         ChannelHandler handler = this.handler;
168         if (handler != null) {
169             p.addLast(handler);
170         }
171         synchronized (options) {
172             setChannelOptions(channel, options, logger);
173         }
174 
175         synchronized (attrs) {
176             for (Map.Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
177                 channel.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
178             }
179         }
180     }
181 
182     private static void setChannelOptions(
183             Channel channel, Map<ChannelOption<?>, Object> options, InternalLogger logger) {
184         for (Map.Entry<ChannelOption<?>, Object> e: options.entrySet()) {
185             setChannelOption(channel, e.getKey(), e.getValue(), logger);
186         }
187     }
188 
189     @SuppressWarnings("unchecked")
190     private static void setChannelOption(
191             Channel channel, ChannelOption<?> option, Object value, InternalLogger logger) {
192         try {
193             if (!channel.config().setOption((ChannelOption<Object>) option, value)) {
194                 logger.warn("Unknown channel option '{}' for channel '{}'", option, channel);
195             }
196         } catch (Throwable t) {
197             logger.warn(
198                     "Failed to set channel option '{}' with value '{}' for channel '{}'", option, value, channel, t);
199         }
200     }
201 }