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.nio.NioEventLoop;
21  import io.netty.channel.nio.NioEventLoopGroup;
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 NioEventLoop}, {@link NioSocketChannel}
40       * and {@link NioDatagramChannel}.
41       */
42      public static final IoTransport DEFAULT = new IoTransport(new NioEventLoopGroup(1).next(),
43              new ChannelFactory<SocketChannel>() {
44                  @Override
45                  public SocketChannel newChannel() {
46                      return new NioSocketChannel();
47                  }
48              },
49              new ChannelFactory<DatagramChannel>() {
50                  @Override
51                  public DatagramChannel newChannel() {
52                      return new NioDatagramChannel();
53                  }
54              });
55  
56      /**
57       * Create a new {@link IoTransport} instance
58       *
59       * @param eventLoop       {@link EventLoop} to use for I/O
60       * @param socketChannel   {@link SocketChannel} for TCP DNS lookup and OCSP query
61       * @param datagramChannel {@link DatagramChannel} for UDP DNS lookup
62       * @return {@link NullPointerException} if any parameter is {@code null}
63       */
64      public static IoTransport create(EventLoop eventLoop, ChannelFactory<SocketChannel> socketChannel,
65                                       ChannelFactory<DatagramChannel> datagramChannel) {
66          return new IoTransport(eventLoop, socketChannel, datagramChannel);
67      }
68  
69      private IoTransport(EventLoop eventLoop, ChannelFactory<SocketChannel> socketChannel,
70                          ChannelFactory<DatagramChannel> datagramChannel) {
71          this.eventLoop = checkNotNull(eventLoop, "EventLoop");
72          this.socketChannel = checkNotNull(socketChannel, "SocketChannel");
73          this.datagramChannel = checkNotNull(datagramChannel, "DatagramChannel");
74      }
75  
76      public EventLoop eventLoop() {
77          return eventLoop;
78      }
79  
80      public ChannelFactory<SocketChannel> socketChannel() {
81          return socketChannel;
82      }
83  
84      public ChannelFactory<DatagramChannel> datagramChannel() {
85          return datagramChannel;
86      }
87  }