package io.vertx.core.net.impl;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.SocketAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
public class AsyncResolveConnectHelper {
private List<Handler<AsyncResult<Channel>>> handlers = new ArrayList<>();
private ChannelFuture future;
private AsyncResult<Channel> result;
public synchronized void addListener(Handler<AsyncResult<Channel>> handler) {
if (result != null) {
if (future != null) {
future.addListener(v -> handler.handle(result));
} else {
handler.handle(result);
}
} else {
handlers.add(handler);
}
}
private synchronized void handle(ChannelFuture cf, AsyncResult<Channel> res) {
if (result == null) {
for (Handler<AsyncResult<Channel>> handler: handlers) {
handler.handle(res);
}
future = cf;
result = res;
} else {
throw new IllegalStateException("Already complete!");
}
}
private static void checkPort(int port) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port " + port);
}
}
public static AsyncResolveConnectHelper doBind(VertxInternal vertx, SocketAddress socketAddress,
ServerBootstrap bootstrap) {
AsyncResolveConnectHelper asyncResolveConnectHelper = new AsyncResolveConnectHelper();
bootstrap.channelFactory(vertx.transport().serverChannelFactory(socketAddress.path() != null));
if (socketAddress.path() != null) {
java.net.SocketAddress converted = vertx.transport().convert(socketAddress, true);
ChannelFuture future = bootstrap.bind(converted);
future.addListener(f -> {
if (f.isSuccess()) {
asyncResolveConnectHelper.handle(future, Future.succeededFuture(future.channel()));
} else {
asyncResolveConnectHelper.handle(future, Future.failedFuture(f.cause()));
}
});
} else {
checkPort(socketAddress.port());
vertx.resolveAddress(socketAddress.host(), res -> {
if (res.succeeded()) {
InetSocketAddress t = new InetSocketAddress(res.result(), socketAddress.port());
ChannelFuture future = bootstrap.bind(t);
future.addListener(f -> {
if (f.isSuccess()) {
asyncResolveConnectHelper.handle(future, Future.succeededFuture(future.channel()));
} else {
asyncResolveConnectHelper.handle(future, Future.failedFuture(f.cause()));
}
});
} else {
asyncResolveConnectHelper.handle(null, Future.failedFuture(res.cause()));
}
});
}
return asyncResolveConnectHelper;
}
}