1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.resolver;
17
18 import io.netty5.util.concurrent.EventExecutor;
19 import io.netty5.util.concurrent.Future;
20 import io.netty5.util.concurrent.Promise;
21
22 import java.net.InetAddress;
23 import java.net.InetSocketAddress;
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30 public class InetSocketAddressResolver extends AbstractAddressResolver<InetSocketAddress> {
31
32 final NameResolver<InetAddress> nameResolver;
33
34
35
36
37
38
39 public InetSocketAddressResolver(EventExecutor executor, NameResolver<InetAddress> nameResolver) {
40 super(executor, InetSocketAddress.class);
41 this.nameResolver = nameResolver;
42 }
43
44 @Override
45 protected boolean doIsResolved(InetSocketAddress address) {
46 return !address.isUnresolved();
47 }
48
49 @Override
50 protected void doResolve(final InetSocketAddress unresolvedAddress, final Promise<InetSocketAddress> promise)
51 throws Exception {
52
53
54 nameResolver.resolve(unresolvedAddress.getHostName())
55 .addListener(future -> {
56 if (future.isSuccess()) {
57 promise.setSuccess(new InetSocketAddress(future.getNow(), unresolvedAddress.getPort()));
58 } else {
59 promise.setFailure(future.cause());
60 }
61 });
62 }
63
64 @Override
65 protected void doResolveAll(final InetSocketAddress unresolvedAddress,
66 final Promise<List<InetSocketAddress>> promise) throws Exception {
67
68
69 nameResolver.resolveAll(unresolvedAddress.getHostName())
70 .addListener(future -> {
71 if (future.isSuccess()) {
72 List<InetAddress> inetAddresses = future.getNow();
73 List<InetSocketAddress> socketAddresses =
74 new ArrayList<>(inetAddresses.size());
75 for (InetAddress inetAddress : inetAddresses) {
76 socketAddresses.add(new InetSocketAddress(inetAddress, unresolvedAddress.getPort()));
77 }
78 promise.setSuccess(socketAddresses);
79 } else {
80 promise.setFailure(future.cause());
81 }
82 });
83 }
84
85 @Override
86 public void close() {
87 nameResolver.close();
88 }
89 }