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