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());
}
}
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());
}
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);
}
}
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;
}
}
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;
}
Aggregations