@Override public void bind(SocketAddress endpoint, int backlog) throws IOException { this.endpoint = (InetSocketAddress) endpoint; UnixSocketAddress address = new UnixSocketAddress(path); serverSocketChannel = UnixServerSocketChannel.open(); serverSocketChannel.configureBlocking(true); serverSocketChannel.socket().bind(address); }
@Override public void connect(SocketAddress endpoint, Integer timeout) throws IOException { this.inetSocketAddress = (InetSocketAddress) endpoint; super.connect(new UnixSocketAddress(path), timeout); }
@Override public void close() throws IOException { serverSocketChannel.close(); } }
/** * Check whether we can connect to a local Unix socket * */ public static boolean canConnectUnixSocket(File path) { try (UnixSocketChannel channel = UnixSocketChannel.open()) { return channel.connect(new UnixSocketAddress(path)); } catch (IOException e) { return false; } } }
public UnixDomainSocket(String ipcSocketPath, int bufferSize) { this.bufferSize = bufferSize; try { UnixSocketAddress address = new UnixSocketAddress(ipcSocketPath); channel = UnixSocketChannel.open(address); reader = new InputStreamReader(Channels.newInputStream(channel)); writer = new PrintWriter(Channels.newOutputStream(channel)); } catch (IOException e) { throw new RuntimeException( "Provided file socket cannot be opened: " + ipcSocketPath, e); } }
@Override protected SocketAddress createSocketAddress(String path) { return new UnixSocketAddress(new File(path)); }
UnixSocket() throws IOException { channel = UnixSocketChannel.open(); }
@Override public void close() throws IOException { channel.close(); inputShutdown = true; outputShutdown = true; }
@Override public Socket accept() throws IOException { UnixSocketChannel channel = serverSocketChannel.accept(); return new BlockingUnixSocket(path, channel, endpoint); }
@Override public void shutdownOutput() throws IOException { channel.shutdownOutput(); outputShutdown = true; }
@Override public SocketAddress getRemoteSocketAddress() { synchronized (connectLock) { return channel.getRemoteSocketAddress(); } }
@Override public void shutdownInput() throws IOException { channel.shutdownInput(); inputShutdown = true; }
@Override public SocketAddress getLocalSocketAddress() { synchronized (connectLock) { return channel.getLocalSocketAddress(); } }
@Override public boolean isConnected() { return channel.isConnected(); }
@Override public boolean isClosed() { return !channel.isOpen(); }
/** * @return the SocketAddress for the MongoD unix domain socket. */ public SocketAddress getUnixSocketAddress() { return new UnixSocketAddress(getHost()); }
private Socket createUnixDomainSocket() throws IOException { UnixSocketChannel channel = UnixSocketChannel.open(); return new BlockingUnixSocket(path, channel); }
@Override public void close() throws IOException { reader.close(); writer.close(); if (null != channel) { channel.close(); } } }
@Override public String toString() { if (isConnected()) { return "UnixSocket[addr=" + channel.getRemoteSocketAddress() + ']'; } return "UnixSocket[unconnected]"; }