View Javadoc
1   /*
2    * Copyright 2020 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    *   https://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.quic;
17  
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelInitializer;
20  import io.netty.channel.ChannelOption;
21  import io.netty.util.AttributeKey;
22  import io.netty.util.concurrent.Future;
23  import io.netty.util.concurrent.Promise;
24  import io.netty.util.internal.ObjectUtil;
25  import io.netty.util.internal.logging.InternalLogger;
26  import io.netty.util.internal.logging.InternalLoggerFactory;
27  import org.jetbrains.annotations.Nullable;
28  
29  import java.util.HashMap;
30  import java.util.LinkedHashMap;
31  import java.util.Map;
32  
33  /**
34   * Allows to bootstrap outgoing {@link QuicStreamChannel}s.
35   */
36  public final class QuicStreamChannelBootstrap {
37      private static final InternalLogger logger = InternalLoggerFactory.getInstance(QuicStreamChannelBootstrap.class);
38  
39      private final QuicChannel parent;
40      private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<>();
41      private final Map<AttributeKey<?>, Object> attrs = new HashMap<>();
42      private ChannelHandler handler;
43      private QuicStreamType type = QuicStreamType.BIDIRECTIONAL;
44  
45      /**
46       * Creates a new instance which uses the given {@link QuicChannel} to bootstrap {@link QuicStreamChannel}s.
47       *
48       * @param parent    the {@link QuicChannel} that is used.
49  
50       */
51      QuicStreamChannelBootstrap(QuicChannel parent) {
52          this.parent = ObjectUtil.checkNotNull(parent, "parent");
53      }
54  
55      /**
56       * Allow to specify a {@link ChannelOption} which is used for the {@link QuicStreamChannel} instances once they got
57       * created. Use a value of {@code null} to remove a previous set {@link ChannelOption}.
58       *
59       * @param option    the {@link ChannelOption} to apply to the {@link QuicStreamChannel}.
60       * @param value     the value of the option.
61       * @param <T>       the type of the value.
62       * @return          this instance.
63       */
64      public <T> QuicStreamChannelBootstrap option(ChannelOption<T> option, @Nullable T value) {
65          Quic.updateOptions(options, option, value);
66          return this;
67      }
68  
69      /**
70       * Allow to specify an initial attribute of the newly created {@link QuicStreamChannel}. If the {@code value} is
71       * {@code null}, the attribute of the specified {@code key} is removed.
72       *
73       * @param key       the {@link AttributeKey} to apply to the {@link QuicChannel}.
74       * @param value     the value of the attribute.
75       * @param <T>       the type of the value.
76       * @return          this instance.
77       */
78      public <T> QuicStreamChannelBootstrap attr(AttributeKey<T> key, @Nullable T value) {
79          Quic.updateAttributes(attrs, key, value);
80          return this;
81      }
82  
83      /**
84       * Set the {@link ChannelHandler} that is added to the {@link io.netty.channel.ChannelPipeline} of the
85       * {@link QuicStreamChannel} once created.
86       *
87       * @param streamHandler     the {@link ChannelHandler} that is added to the {@link QuicStreamChannel}s
88       *                          {@link io.netty.channel.ChannelPipeline}.
89       * @return                  this instance.
90       */
91      public QuicStreamChannelBootstrap handler(ChannelHandler streamHandler) {
92          this.handler = ObjectUtil.checkNotNull(streamHandler, "streamHandler");
93          return this;
94      }
95  
96      /**
97       * Set the {@link QuicStreamType} to use for the {@link QuicStreamChannel}, default is
98       * {@link QuicStreamType#BIDIRECTIONAL}.
99       *
100      * @param type     the {@link QuicStreamType} of the  {@link QuicStreamChannel}.
101      * @return         this instance.
102      */
103     public QuicStreamChannelBootstrap type(QuicStreamType type) {
104         this.type = ObjectUtil.checkNotNull(type, "type");
105         return this;
106     }
107 
108     /**
109      * Creates a new {@link QuicStreamChannel} and notifies the {@link Future}.
110      *
111      * @return  the {@link Future} that is notified once the operation completes.
112      */
113     public Future<QuicStreamChannel> create() {
114         return create(parent.eventLoop().newPromise());
115     }
116 
117     /**
118      * Creates a new {@link QuicStreamChannel} and notifies the {@link Future}.
119      *
120      * @param promise   the {@link Promise} that is notified once the operation completes.
121      * @return          the {@link Future} that is notified once the operation completes.
122      */
123     public Future<QuicStreamChannel> create(Promise<QuicStreamChannel> promise) {
124         if (handler == null) {
125             throw new IllegalStateException("streamHandler not set");
126         }
127 
128         return parent.createStream(type, new QuicStreamChannelBootstrapHandler(handler,
129                 Quic.toOptionsArray(options), Quic.toAttributesArray(attrs)), promise);
130     }
131 
132     private static final class QuicStreamChannelBootstrapHandler extends ChannelInitializer<QuicStreamChannel> {
133         private final ChannelHandler streamHandler;
134         private final Map.Entry<ChannelOption<?>, Object>[] streamOptions;
135         private final Map.Entry<AttributeKey<?>, Object>[] streamAttrs;
136 
137         QuicStreamChannelBootstrapHandler(ChannelHandler streamHandler,
138                                           Map.Entry<ChannelOption<?>, Object>[] streamOptions,
139                                           Map.Entry<AttributeKey<?>, Object>[] streamAttrs) {
140             this.streamHandler = streamHandler;
141             this.streamOptions = streamOptions;
142             this.streamAttrs = streamAttrs;
143         }
144         @Override
145         protected void initChannel(QuicStreamChannel ch) {
146             Quic.setupChannel(ch, streamOptions, streamAttrs, streamHandler, logger);
147         }
148     }
149 }