use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
the class TCPServerBase method listen.
private synchronized io.netty.util.concurrent.Future<Channel> listen(SocketAddress localAddress, ContextInternal context) {
if (listening) {
throw new IllegalStateException("Listen already called");
}
this.listenContext = context;
this.listening = true;
this.eventLoop = context.nettyEventLoop();
SocketAddress bindAddress;
Map<ServerID, TCPServerBase> sharedNetServers = vertx.sharedTCPServers((Class<TCPServerBase>) getClass());
synchronized (sharedNetServers) {
actualPort = localAddress.port();
String hostOrPath = localAddress.isInetSocket() ? localAddress.host() : localAddress.path();
TCPServerBase main;
boolean shared;
ServerID id;
if (actualPort > 0 || localAddress.isDomainSocket()) {
id = new ServerID(actualPort, hostOrPath);
main = sharedNetServers.get(id);
shared = true;
bindAddress = localAddress;
} else {
if (actualPort < 0) {
id = new ServerID(actualPort, hostOrPath + "/" + -actualPort);
main = sharedNetServers.get(id);
shared = true;
bindAddress = SocketAddress.inetSocketAddress(0, localAddress.host());
} else {
id = new ServerID(actualPort, hostOrPath);
main = null;
shared = false;
bindAddress = localAddress;
}
}
if (main == null) {
try {
sslHelper = createSSLHelper();
sslHelper.validate(vertx);
worker = childHandler(listenContext, localAddress, sslHelper);
servers = new HashSet<>();
servers.add(this);
channelBalancer = new ServerChannelLoadBalancer(vertx.getAcceptorEventLoopGroup().next());
channelBalancer.addWorker(eventLoop, worker);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(vertx.getAcceptorEventLoopGroup(), channelBalancer.workers());
if (sslHelper.isSSL()) {
bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
} else {
bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}
bootstrap.childHandler(channelBalancer);
applyConnectionOptions(localAddress.isDomainSocket(), bootstrap);
bindFuture = AsyncResolveConnectHelper.doBind(vertx, bindAddress, bootstrap);
bindFuture.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Channel>>) res -> {
if (res.isSuccess()) {
Channel ch = res.getNow();
log.trace("Net server listening on " + hostOrPath + ":" + ch.localAddress());
if (shared) {
ch.closeFuture().addListener((ChannelFutureListener) channelFuture -> {
synchronized (sharedNetServers) {
sharedNetServers.remove(id);
}
});
}
if (bindAddress.isInetSocket()) {
actualPort = ((InetSocketAddress) ch.localAddress()).getPort();
}
listenContext.addCloseHook(this);
metrics = createMetrics(localAddress);
} else {
if (shared) {
synchronized (sharedNetServers) {
sharedNetServers.remove(id);
}
}
listening = false;
}
});
} catch (Throwable t) {
listening = false;
return vertx.getAcceptorEventLoopGroup().next().newFailedFuture(t);
}
if (shared) {
sharedNetServers.put(id, this);
}
actualServer = this;
} else {
// Server already exists with that host/port - we will use that
actualServer = main;
metrics = main.metrics;
sslHelper = main.sslHelper;
worker = childHandler(listenContext, localAddress, sslHelper);
actualServer.servers.add(this);
actualServer.channelBalancer.addWorker(eventLoop, worker);
listenContext.addCloseHook(this);
}
}
return actualServer.bindFuture;
}
use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
the class DatagramSocketImpl method listen.
private Future<DatagramSocket> listen(SocketAddress local) {
AddressResolver resolver = context.owner().addressResolver();
PromiseInternal<Void> promise = context.promise();
io.netty.util.concurrent.Future<InetSocketAddress> f1 = resolver.resolveHostname(context.nettyEventLoop(), local.host());
f1.addListener((GenericFutureListener<io.netty.util.concurrent.Future<InetSocketAddress>>) res1 -> {
if (res1.isSuccess()) {
ChannelFuture f2 = channel.bind(new InetSocketAddress(res1.getNow().getAddress(), local.port()));
if (metrics != null) {
f2.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Void>>) res2 -> {
if (res2.isSuccess()) {
metrics.listening(local.host(), localAddress());
}
});
}
f2.addListener(promise);
} else {
promise.fail(res1.cause());
}
});
return promise.future().map(this);
}
use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
the class VertxImpl method deleteCacheDirAndShutdown.
@SuppressWarnings("unchecked")
private void deleteCacheDirAndShutdown(Handler<AsyncResult<Void>> completionHandler) {
executeBlockingInternal(fut -> {
try {
fileResolver.close();
fut.complete();
} catch (IOException e) {
fut.tryFail(e);
}
}, ar -> {
workerPool.close();
internalWorkerPool.close();
new ArrayList<>(namedWorkerPools.values()).forEach(WorkerPool::close);
acceptorEventLoopGroup.shutdownGracefully(0, 10, TimeUnit.SECONDS).addListener(new GenericFutureListener() {
@Override
public void operationComplete(io.netty.util.concurrent.Future future) throws Exception {
if (!future.isSuccess()) {
log.warn("Failure in shutting down acceptor event loop group", future.cause());
}
eventLoopGroup.shutdownGracefully(0, 10, TimeUnit.SECONDS).addListener(new GenericFutureListener() {
@Override
public void operationComplete(io.netty.util.concurrent.Future future) throws Exception {
if (!future.isSuccess()) {
log.warn("Failure in shutting down event loop group", future.cause());
}
if (metrics != null) {
metrics.close();
}
if (tracer != null) {
tracer.close();
}
checker.close();
if (completionHandler != null) {
eventLoopThreadFactory.newThread(() -> {
completionHandler.handle(Future.succeededFuture());
}).start();
}
}
});
}
});
});
}
Aggregations