1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
47
48
49
50
51 QuicStreamChannelBootstrap(QuicChannel parent) {
52 this.parent = ObjectUtil.checkNotNull(parent, "parent");
53 }
54
55
56
57
58
59
60
61
62
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
71
72
73
74
75
76
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
85
86
87
88
89
90
91 public QuicStreamChannelBootstrap handler(ChannelHandler streamHandler) {
92 this.handler = ObjectUtil.checkNotNull(streamHandler, "streamHandler");
93 return this;
94 }
95
96
97
98
99
100
101
102
103 public QuicStreamChannelBootstrap type(QuicStreamType type) {
104 this.type = ObjectUtil.checkNotNull(type, "type");
105 return this;
106 }
107
108
109
110
111
112
113 public Future<QuicStreamChannel> create() {
114 return create(parent.eventLoop().newPromise());
115 }
116
117
118
119
120
121
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 }