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.netty5.handler.address;
17  
18  import io.netty5.channel.ChannelHandler;
19  import io.netty5.channel.ChannelHandlerContext;
20  import io.netty5.resolver.AddressResolver;
21  import io.netty5.resolver.AddressResolverGroup;
22  import io.netty5.util.concurrent.Future;
23  import io.netty5.util.concurrent.FutureListener;
24  import io.netty5.util.concurrent.Promise;
25  
26  import java.net.SocketAddress;
27  import java.util.Objects;
28  
29  /**
30   * {@link ChannelHandler} which will resolve the {@link SocketAddress} that is passed to
31   * {@link #connect(ChannelHandlerContext, SocketAddress, SocketAddress)} if it is not already resolved
32   * and the {@link AddressResolver} supports the type of {@link SocketAddress}.
33   */
34  public class ResolveAddressHandler implements ChannelHandler {
35  
36      private final AddressResolverGroup<? extends SocketAddress> resolverGroup;
37  
38      public ResolveAddressHandler(AddressResolverGroup<? extends SocketAddress> resolverGroup) {
39          this.resolverGroup = Objects.requireNonNull(resolverGroup, "resolverGroup");
40      }
41  
42      @Override
43      public boolean isSharable() {
44          return true;
45      }
46  
47      @Override
48      public Future<Void> connect(final ChannelHandlerContext ctx, SocketAddress remoteAddress,
49                            final SocketAddress localAddress)  {
50          AddressResolver<? extends SocketAddress> resolver = resolverGroup.getResolver(ctx.executor());
51          if (resolver.isSupported(remoteAddress) && !resolver.isResolved(remoteAddress)) {
52              Promise<Void> promise = ctx.newPromise();
53              resolver.resolve(remoteAddress).addListener((FutureListener<SocketAddress>) future -> {
54                  Throwable cause = future.cause();
55                  if (cause != null) {
56                      promise.setFailure(cause);
57                  } else {
58                      ctx.connect(future.getNow(), localAddress).cascadeTo(promise);
59                  }
60                  ctx.pipeline().remove(this);
61              });
62              return promise.asFuture();
63          } else {
64              Future<Void> f = ctx.connect(remoteAddress, localAddress);
65              ctx.pipeline().remove(this);
66              return f;
67          }
68      }
69  }