Search in sources :

Example 1 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress in project beam by apache.

the class SocketAddressFactory method createFrom.

/**
   * Parse a {@link SocketAddress} from the given string.
   */
public static SocketAddress createFrom(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.
        HostAndPort hostAndPort = HostAndPort.fromString(value);
        checkArgument(hostAndPort.hasPort(), "Address must be a unix:// path or be in the form host:port. Got: %s", value);
        return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
    }
}
Also used : HostAndPort(com.google.common.net.HostAndPort) InetSocketAddress(java.net.InetSocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) IOException(java.io.IOException) File(java.io.File)

Example 2 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress in project beam by apache.

the class SocketAddressFactoryTest method testDomainSocket.

@Test
public void testDomainSocket() throws Exception {
    File tmpFile = tmpFolder.newFile();
    SocketAddress socketAddress = SocketAddressFactory.createFrom("unix://" + tmpFile.getAbsolutePath());
    assertThat(socketAddress, instanceOf(DomainSocketAddress.class));
    assertEquals(tmpFile.getAbsolutePath(), ((DomainSocketAddress) socketAddress).path());
}
Also used : DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) Test(org.junit.Test)

Example 3 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress 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 4 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress in project neo4j by neo4j.

the class BoltServer method createLoopbackProtocolInitializer.

private ProtocolInitializer createLoopbackProtocolInitializer(BoltProtocolFactory boltProtocolFactory, TransportThrottleGroup throttleGroup, ByteBufAllocator bufferAllocator) {
    if (config.get(BoltConnectorInternalSettings.enable_loopback_auth)) {
        if (config.get(BoltConnectorInternalSettings.unsupported_loopback_listen_file) == null) {
            throw new IllegalArgumentException("A file has not been specified for use with the loopback domain socket.");
        }
        File unixSocketFile = new File(config.get(BoltConnectorInternalSettings.unsupported_loopback_listen_file).toString());
        if (// Check if the file does not exist before passing to netty to create it
        unixSocketFile.exists()) {
            if (config.get(BoltConnectorInternalSettings.unsupported_loopback_delete)) {
                try {
                    Files.deleteIfExists(Path.of(unixSocketFile.getPath()));
                } catch (IOException e) {
                    throw new IllegalStateException("Failed to delete loopback domain socket file '" + unixSocketFile + "': " + e.getMessage(), e);
                }
            } else {
                throw new IllegalArgumentException("Loopback listen file: " + unixSocketFile + " already exists.");
            }
        }
        DomainSocketAddress loopbackListenAddress = new DomainSocketAddress(unixSocketFile);
        Duration channelTimeout = config.get(BoltConnectorInternalSettings.unsupported_bolt_unauth_connection_timeout);
        long maxMessageSize = config.get(BoltConnectorInternalSettings.unsupported_bolt_unauth_connection_max_inbound_bytes);
        return new SocketTransport(BoltConnectorInternalSettings.LOOPBACK_NAME, loopbackListenAddress, null, false, logService.getInternalLogProvider(), throttleGroup, boltProtocolFactory, connectionTracker, channelTimeout, maxMessageSize, bufferAllocator, boltMemoryPool);
    } else {
        return null;
    }
}
Also used : SocketTransport(org.neo4j.bolt.transport.SocketTransport) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) IOException(java.io.IOException) File(java.io.File)

Example 5 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress in project alluxio by Alluxio.

the class NetworkAddressUtils method getDataPortSocketAddress.

/**
 * Extracts dataPort socket address from Alluxio representation of network address.
 *
 * @param netAddress the input network address representation
 * @param conf Alluxio configuration
 * @return the socket address
 */
public static SocketAddress getDataPortSocketAddress(WorkerNetAddress netAddress, AlluxioConfiguration conf) {
    SocketAddress address;
    if (NettyUtils.isDomainSocketAccessible(netAddress, conf)) {
        address = new DomainSocketAddress(netAddress.getDomainSocketPath());
    } else {
        String host = netAddress.getHost();
        // to establish the connection.
        if (!netAddress.getContainerHost().equals("")) {
            LOG.debug("Worker is in a container. Use container host {} instead of physical host {}", netAddress.getContainerHost(), host);
            host = netAddress.getContainerHost();
        }
        int port = netAddress.getDataPort();
        address = new InetSocketAddress(host, port);
    }
    return address;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)23 File (java.io.File)11 InetSocketAddress (java.net.InetSocketAddress)9 SocketAddress (java.net.SocketAddress)8 IOException (java.io.IOException)7 Test (org.junit.Test)6 ByteString (com.google.protobuf.ByteString)5 ByteBuf (io.netty.buffer.ByteBuf)4 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)4 Test (org.junit.jupiter.api.Test)4 PeerCredentials (io.netty.channel.unix.PeerCredentials)3 SocketTest (io.netty.channel.unix.tests.SocketTest)3 DomainSocketAddress (org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.unix.DomainSocketAddress)3 DomainDatagramPacket (io.netty.channel.unix.DomainDatagramPacket)2 DomainDatagramSocketAddress (io.netty.channel.unix.DomainDatagramSocketAddress)2 IovArray (io.netty.channel.unix.IovArray)2 ByteBuffer (java.nio.ByteBuffer)2 GrpcServerBuilder (alluxio.grpc.GrpcServerBuilder)1 HostAndPort (com.google.common.net.HostAndPort)1