All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.shaded.vertx.core.net.impl.AsyncResolveConnectHelper Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.net.impl;

import com.arangodb.shaded.netty.bootstrap.ServerBootstrap;
import com.arangodb.shaded.netty.channel.Channel;
import com.arangodb.shaded.netty.channel.ChannelFuture;
import com.arangodb.shaded.netty.util.concurrent.Promise;
import com.arangodb.shaded.vertx.core.AsyncResult;
import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.Handler;
import com.arangodb.shaded.vertx.core.impl.VertxInternal;
import com.arangodb.shaded.vertx.core.net.SocketAddress;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * @author Tim Fox
 */
public class AsyncResolveConnectHelper {

  public static com.arangodb.shaded.netty.util.concurrent.Future doBind(VertxInternal vertx,
                                                                SocketAddress socketAddress,
                                                                ServerBootstrap bootstrap) {
    Promise promise = vertx.getAcceptorEventLoopGroup().next().newPromise();
    try {
      bootstrap.channelFactory(vertx.transport().serverChannelFactory(socketAddress.isDomainSocket()));
    } catch (Exception e) {
      promise.setFailure(e);
      return promise;
    }
    if (socketAddress.isDomainSocket()) {
      java.net.SocketAddress converted = vertx.transport().convert(socketAddress);
      ChannelFuture future = bootstrap.bind(converted);
      future.addListener(f -> {
        if (f.isSuccess()) {
          promise.setSuccess(future.channel());
        } else {
          promise.setFailure(f.cause());
        }
      });
    } else {
      SocketAddressImpl impl = (SocketAddressImpl) socketAddress;
      Handler> cont = res -> {
        if (res.succeeded()) {
          // At this point the name is an IP address so there will be no resolve hit
          InetSocketAddress t = new InetSocketAddress(res.result(), socketAddress.port());
          ChannelFuture future = bootstrap.bind(t);
          future.addListener(f -> {
            if (f.isSuccess()) {
              promise.setSuccess(future.channel());
            } else {
              promise.setFailure(f.cause());
            }
          });
        } else {
          promise.setFailure(res.cause());
        }
      };
      if (impl.ipAddress() != null) {
        cont.handle(Future.succeededFuture(impl.ipAddress()));
      } else {
        vertx.resolveAddress(socketAddress.host(), cont);
      }
    }
    return promise;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy