View Javadoc
1   /*
2    * Copyright 2022 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.ssl.ocsp;
17  
18  import io.netty.channel.ChannelFactory;
19  import io.netty.channel.EventLoop;
20  import io.netty.channel.MultiThreadIoEventLoopGroup;
21  import io.netty.channel.nio.NioIoHandler;
22  import io.netty.channel.socket.DatagramChannel;
23  import io.netty.channel.socket.SocketChannel;
24  import io.netty.channel.socket.nio.NioDatagramChannel;
25  import io.netty.channel.socket.nio.NioSocketChannel;
26  
27  import static io.netty.util.internal.ObjectUtil.checkNotNull;
28  
29  /**
30   * {@link IoTransport} holds {@link EventLoop}, {@link SocketChannel}
31   * and {@link DatagramChannel} for DNS I/O.
32   */
33  public final class IoTransport {
34      private final EventLoop eventLoop;
35      private final ChannelFactory<SocketChannel> socketChannel;
36      private final ChannelFactory<DatagramChannel> datagramChannel;
37  
38      /**
39       * Default {@link IoTransport} which uses {@link NioIoHandler}, {@link NioSocketChannel}
40       * and {@link NioDatagramChannel}.
41       */
42      public static final IoTransport DEFAULT = new IoTransport(
43              new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()).next(),
44              new ChannelFactory<SocketChannel>() {
45                  @Override
46                  public SocketChannel newChannel() {
47                      return new NioSocketChannel();
48                  }
49              },
50              new ChannelFactory<DatagramChannel>() {
51                  @Override
52                  public DatagramChannel newChannel() {
53                      return new NioDatagramChannel();
54                  }
55              });
56  
57      /**
58       * Create a new {@link IoTransport} instance
59       *
60       * @param eventLoop       {@link EventLoop} to use for I/O
61       * @param socketChannel   {@link SocketChannel} for TCP DNS lookup and OCSP query
62       * @param datagramChannel {@link DatagramChannel} for UDP DNS lookup
63       * @return {@link NullPointerException} if any parameter is {@code null}
64       */
65      public static IoTransport create(EventLoop eventLoop, ChannelFactory<SocketChannel> socketChannel,
66                                       ChannelFactory<DatagramChannel> datagramChannel) {
67          return new IoTransport(eventLoop, socketChannel, datagramChannel);
68      }
69  
70      private IoTransport(EventLoop eventLoop, ChannelFactory<SocketChannel> socketChannel,
71                          ChannelFactory<DatagramChannel> datagramChannel) {
72          this.eventLoop = checkNotNull(eventLoop, "EventLoop");
73          this.socketChannel = checkNotNull(socketChannel, "SocketChannel");
74          this.datagramChannel = checkNotNull(datagramChannel, "DatagramChannel");
75      }
76  
77      public EventLoop eventLoop() {
78          return eventLoop;
79      }
80  
81      public ChannelFactory<SocketChannel> socketChannel() {
82          return socketChannel;
83      }
84  
85      public ChannelFactory<DatagramChannel> datagramChannel() {
86          return datagramChannel;
87      }
88  }