Search in sources :

Example 61 with InetSocketAddress

use of java.net.InetSocketAddress in project j2objc by google.

the class DatagramChannelImpl method connect.

@Override
public synchronized DatagramChannel connect(SocketAddress address) throws IOException {
    // must be open
    checkOpen();
    // status must be un-connected.
    if (connected) {
        throw new IllegalStateException();
    }
    // check the address
    InetSocketAddress inetSocketAddress = SocketChannelImpl.validateAddress(address);
    InetAddress remoteAddress = inetSocketAddress.getAddress();
    int remotePort = inetSocketAddress.getPort();
    try {
        begin();
        NetworkBridge.connect(fd, remoteAddress, remotePort);
    } catch (ConnectException e) {
    // ConnectException means connect fail, not exception
    } finally {
        end(true);
    }
    // address state held by the channel and the socket up to date.
    if (!isBound) {
        onBind(true);
    }
    // Keep the connected state held by the channel and the socket up to date.
    onConnect(remoteAddress, remotePort, true);
    return this;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 62 with InetSocketAddress

use of java.net.InetSocketAddress in project grpc-java by grpc.

the class Utils method parseSocketAddress.

/**
   * Parse a {@link SocketAddress} from the given string.
   */
public static SocketAddress parseSocketAddress(String value) {
    if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
        // Unix Domain Socket address.
        // Create the underlying file for the Unix Domain Socket.
        String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
        File file = new File(filePath);
        if (!file.isAbsolute()) {
            throw new IllegalArgumentException("File path must be absolute: " + filePath);
        }
        try {
            if (file.createNewFile()) {
                // If this application created the file, delete it when the application exits.
                file.deleteOnExit();
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        // Create the SocketAddress referencing the file.
        return new DomainSocketAddress(file);
    } else {
        // Standard TCP/IP address.
        String[] parts = value.split(":", 2);
        if (parts.length < 2) {
            throw new IllegalArgumentException("Address must be a unix:// path or be in the form host:port. Got: " + value);
        }
        String host = parts[0];
        int port = Integer.parseInt(parts[1]);
        return new InetSocketAddress(host, port);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) File(java.io.File)

Example 63 with InetSocketAddress

use of java.net.InetSocketAddress in project grpc-java by grpc.

the class Utils method newOkhttpClientChannel.

private static OkHttpChannelBuilder newOkhttpClientChannel(SocketAddress address, boolean tls, boolean testca, @Nullable String authorityOverride) {
    InetSocketAddress addr = (InetSocketAddress) address;
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress(addr.getHostName(), addr.getPort());
    if (tls) {
        builder.negotiationType(io.grpc.okhttp.NegotiationType.TLS);
        SSLSocketFactory factory;
        if (testca) {
            builder.overrideAuthority(GrpcUtil.authorityFromHostAndPort(authorityOverride, addr.getPort()));
            try {
                factory = TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        }
        builder.sslSocketFactory(factory);
    } else {
        builder.negotiationType(io.grpc.okhttp.NegotiationType.PLAINTEXT);
    }
    return builder;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) IOException(java.io.IOException) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Example 64 with InetSocketAddress

use of java.net.InetSocketAddress in project grpc-java by grpc.

the class TransportBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    AbstractServerImplBuilder<?> serverBuilder;
    AbstractManagedChannelImplBuilder<?> channelBuilder;
    switch(transport) {
        case INPROCESS:
            {
                String name = "bench" + Math.random();
                serverBuilder = InProcessServerBuilder.forName(name);
                channelBuilder = InProcessChannelBuilder.forName(name);
                break;
            }
        case NETTY:
            {
                InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
                serverBuilder = NettyServerBuilder.forAddress(address);
                channelBuilder = NettyChannelBuilder.forAddress(address).negotiationType(NegotiationType.PLAINTEXT);
                break;
            }
        case NETTY_LOCAL:
            {
                String name = "bench" + Math.random();
                LocalAddress address = new LocalAddress(name);
                serverBuilder = NettyServerBuilder.forAddress(address).channelType(LocalServerChannel.class);
                channelBuilder = NettyChannelBuilder.forAddress(address).channelType(LocalChannel.class).negotiationType(NegotiationType.PLAINTEXT);
                break;
            }
        case NETTY_EPOLL:
            {
                InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
                // Reflection used since they are only available on linux.
                Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
                EventLoopGroup group = (EventLoopGroup) groupClass.getConstructor().newInstance();
                @SuppressWarnings("unchecked") Class<? extends ServerChannel> serverChannelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerSocketChannel");
                serverBuilder = NettyServerBuilder.forAddress(address).bossEventLoopGroup(group).workerEventLoopGroup(group).channelType(serverChannelClass);
                @SuppressWarnings("unchecked") Class<? extends Channel> channelClass = (Class<? extends Channel>) Class.forName("io.netty.channel.epoll.EpollSocketChannel");
                channelBuilder = NettyChannelBuilder.forAddress(address).eventLoopGroup(group).channelType(channelClass).negotiationType(NegotiationType.PLAINTEXT);
                groupToShutdown = group;
                break;
            }
        case OKHTTP:
            {
                int port = pickUnusedPort();
                InetSocketAddress address = new InetSocketAddress("localhost", port);
                serverBuilder = NettyServerBuilder.forAddress(address);
                channelBuilder = OkHttpChannelBuilder.forAddress("localhost", port).negotiationType(io.grpc.okhttp.NegotiationType.PLAINTEXT);
                break;
            }
        default:
            throw new Exception("Unknown transport: " + transport);
    }
    if (direct) {
        serverBuilder.directExecutor();
        // Because blocking stubs avoid the executor, this doesn't do much.
        channelBuilder.directExecutor();
    }
    server = serverBuilder.addService(new AsyncServer.BenchmarkServiceImpl()).build();
    server.start();
    channel = channelBuilder.build();
    stub = BenchmarkServiceGrpc.newBlockingStub(channel);
    // Wait for channel to start
    stub.unaryCall(SimpleRequest.getDefaultInstance());
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) InetSocketAddress(java.net.InetSocketAddress) LocalChannel(io.netty.channel.local.LocalChannel) ManagedChannel(io.grpc.ManagedChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) AsyncServer(io.grpc.benchmarks.qps.AsyncServer) ByteString(com.google.protobuf.ByteString) LocalServerChannel(io.netty.channel.local.LocalServerChannel) ServerChannel(io.netty.channel.ServerChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) Setup(org.openjdk.jmh.annotations.Setup)

Example 65 with InetSocketAddress

use of java.net.InetSocketAddress in project grpc-java by grpc.

the class AbstractManagedChannelImplBuilderTest method makeTargetStringForDirectAddress_scopedIpv6.

@Test
public void makeTargetStringForDirectAddress_scopedIpv6() throws Exception {
    InetSocketAddress address = new InetSocketAddress("0:0:0:0:0:0:0:0%0", 10005);
    assertEquals("/0:0:0:0:0:0:0:0%0:10005", address.toString());
    String target = AbstractManagedChannelImplBuilder.makeTargetStringForDirectAddress(address);
    URI uri = new URI(target);
    assertEquals("directaddress:////0:0:0:0:0:0:0:0%250:10005", target);
    assertEquals(target, uri.toString());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2731 IOException (java.io.IOException)626 Test (org.junit.Test)614 Socket (java.net.Socket)362 InetAddress (java.net.InetAddress)253 ServerSocket (java.net.ServerSocket)182 SocketAddress (java.net.SocketAddress)180 ArrayList (java.util.ArrayList)179 Configuration (org.apache.hadoop.conf.Configuration)141 ByteBuffer (java.nio.ByteBuffer)135 UnknownHostException (java.net.UnknownHostException)127 InputStream (java.io.InputStream)106 SocketChannel (java.nio.channels.SocketChannel)106 OutputStream (java.io.OutputStream)104 SocketException (java.net.SocketException)94 File (java.io.File)93 URI (java.net.URI)80 HashMap (java.util.HashMap)79 Proxy (java.net.Proxy)72 ServerSocketChannel (java.nio.channels.ServerSocketChannel)69