Search in sources :

Example 1 with UnixSocketChannel

use of jnr.unixsocket.UnixSocketChannel in project jetty.project by eclipse.

the class UnixSocketConnector method accept.

@Override
public void accept(int acceptorID) throws IOException {
    LOG.warn("Blocking UnixSocket accept used.  Cannot be interrupted!");
    UnixServerSocketChannel serverChannel = _acceptChannel;
    if (serverChannel != null && serverChannel.isOpen()) {
        LOG.debug("accept {}", serverChannel);
        UnixSocketChannel channel = serverChannel.accept();
        LOG.debug("accepted {}", channel);
        accepted(channel);
    }
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) UnixServerSocketChannel(jnr.unixsocket.UnixServerSocketChannel)

Example 2 with UnixSocketChannel

use of jnr.unixsocket.UnixSocketChannel in project jetty.project by eclipse.

the class UnixSocketClient method main.

public static void main(String[] args) throws Exception {
    java.io.File path = new java.io.File("/tmp/jetty.sock");
    String data = "GET / HTTP/1.1\r\nHost: unixsock\r\n\r\n";
    UnixSocketAddress address = new UnixSocketAddress(path);
    UnixSocketChannel channel = UnixSocketChannel.open(address);
    System.out.println("connected to " + channel.getRemoteSocketAddress());
    PrintWriter w = new PrintWriter(Channels.newOutputStream(channel));
    InputStreamReader r = new InputStreamReader(Channels.newInputStream(channel));
    while (true) {
        w.print(data);
        w.flush();
        CharBuffer result = CharBuffer.allocate(4096);
        r.read(result);
        result.flip();
        System.out.println("read from server: " + result.toString());
        Thread.sleep(1000);
    }
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) InputStreamReader(java.io.InputStreamReader) CharBuffer(java.nio.CharBuffer) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress) PrintWriter(java.io.PrintWriter)

Example 3 with UnixSocketChannel

use of jnr.unixsocket.UnixSocketChannel in project linuxtools by eclipse.

the class DefaultUnixConnectionSettingsProvider method getConnectionSettings.

@Override
public List<IDockerConnectionSettings> getConnectionSettings() {
    // $NON-NLS-1$
    final File unixSocketFile = new File("/var/run/docker.sock");
    if (unixSocketFile.exists() && unixSocketFile.canRead() && unixSocketFile.canWrite()) {
        final UnixSocketAddress address = new UnixSocketAddress(unixSocketFile);
        try (final UnixSocketChannel channel = UnixSocketChannel.open(address)) {
            // assume socket works
            final UnixSocketConnectionSettings socket = new UnixSocketConnectionSettings(DefaultDockerConnectionSettingsFinder.Defaults.DEFAULT_UNIX_SOCKET_PATH);
            socket.setName(socket.getPath());
            return Arrays.asList(socket);
        } catch (IOException e) {
        // do nothing, just assume socket did not work.
        }
    }
    return Collections.emptyList();
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) IOException(java.io.IOException) File(java.io.File) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress)

Aggregations

UnixSocketChannel (jnr.unixsocket.UnixSocketChannel)3 UnixSocketAddress (jnr.unixsocket.UnixSocketAddress)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 CharBuffer (java.nio.CharBuffer)1 UnixServerSocketChannel (jnr.unixsocket.UnixServerSocketChannel)1