Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
Fix #98: Use jnr-unixsocket instead of junixsocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrunber committed Dec 15, 2014
1 parent 13e702e commit 335bfee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 78 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>de.gesellix</groupId>
<artifactId>unix-socket-factory</artifactId>
<version>2014-09-26T18-52-33</version>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down Expand Up @@ -282,7 +287,8 @@
<include>javax.annotation:*</include>
<include>javax.ws.rs:*</include>
<include>org.glassfish.**</include>
<include>de.gesellix:unix-socket-factory</include>
<include>com.github.jnr:*</include>
<include>org.ow2.asm:*</include>
</includes>
</artifactSet>
<relocations>
Expand Down
118 changes: 46 additions & 72 deletions src/main/java/com/spotify/docker/client/ApacheUnixSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

package com.spotify.docker.client;

import com.google.common.collect.Queues;

import org.newsclub.net.unix.AFUNIXSocket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.Queue;

import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;

import com.google.common.collect.Queues;

/**
* Provides a socket that wraps an org.newsclub.net.unix.AFUNIXSocket and delays setting options
* Provides a socket that wraps an jnr.unixsocket.UnixSocketChannel and delays setting options
* until the socket is connected. This is necessary because the Apache HTTP client attempts to
* set options prior to connecting the socket, which doesn't work for Unix sockets since options
* are being set on the underlying file descriptor. Until the socket is connected, the file
Expand All @@ -47,75 +49,78 @@
*/
public class ApacheUnixSocket extends Socket {

private final AFUNIXSocket inner;
private final UnixSocketChannel inner;

private final Queue<SocketOptionSetter> optionsToSet = Queues.newArrayDeque();

public ApacheUnixSocket() throws IOException {
this.inner = AFUNIXSocket.newInstance();
this.inner = UnixSocketChannel.open();
}

@Override
public void connect(final SocketAddress endpoint) throws IOException {
inner.connect(endpoint);
setAllSocketOptions();
if (endpoint instanceof UnixSocketAddress) {
inner.connect((UnixSocketAddress) endpoint);
setAllSocketOptions();
}
}

@Override
public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
inner.connect(endpoint, timeout);
setAllSocketOptions();
if (endpoint instanceof UnixSocketAddress) {
inner.connect((UnixSocketAddress) endpoint);
setAllSocketOptions();
}
}

@Override
public void bind(final SocketAddress bindpoint) throws IOException {
inner.bind(bindpoint);
setAllSocketOptions();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public InetAddress getInetAddress() {
return inner.getInetAddress();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public InetAddress getLocalAddress() {
return inner.getLocalAddress();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public int getPort() {
return inner.getPort();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public int getLocalPort() {
return inner.getLocalPort();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public SocketAddress getRemoteSocketAddress() {
return inner.getRemoteSocketAddress();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public SocketAddress getLocalSocketAddress() {
return inner.getLocalSocketAddress();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public SocketChannel getChannel() {
return inner.getChannel();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public InputStream getInputStream() throws IOException {
return inner.getInputStream();
return Channels.newInputStream(inner);
}

@Override
public OutputStream getOutputStream() throws IOException {
return inner.getOutputStream();
return Channels.newOutputStream(inner);
}

private void setSocketOption(final SocketOptionSetter s) throws SocketException {
Expand All @@ -136,52 +141,36 @@ private void setAllSocketOptions() throws SocketException {

@Override
public void setTcpNoDelay(final boolean on) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setTcpNoDelay(on);
}
});
}

@Override
public boolean getTcpNoDelay() throws SocketException {
return inner.getTcpNoDelay();
return false;
}

@Override
public void setSoLinger(final boolean on, final int linger) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setSoLinger(on, linger);
}
});
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public int getSoLinger() throws SocketException {
return inner.getSoLinger();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public void sendUrgentData(final int data) throws IOException {
inner.sendUrgentData(data);
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public void setOOBInline(final boolean on) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setOOBInline(on);
}
});
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public boolean getOOBInline() throws SocketException {
return inner.getOOBInline();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
Expand All @@ -201,32 +190,22 @@ public synchronized int getSoTimeout() throws SocketException {

@Override
public synchronized void setSendBufferSize(final int size) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setSendBufferSize(size);
}
});
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public synchronized int getSendBufferSize() throws SocketException {
return inner.getSendBufferSize();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public synchronized void setReceiveBufferSize(final int size) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setReceiveBufferSize(size);
}
});
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public synchronized int getReceiveBufferSize() throws SocketException {
return inner.getReceiveBufferSize();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
Expand All @@ -246,17 +225,12 @@ public boolean getKeepAlive() throws SocketException {

@Override
public void setTrafficClass(final int tc) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setTrafficClass(tc);
}
});
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public int getTrafficClass() throws SocketException {
return inner.getTrafficClass();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
Expand All @@ -266,7 +240,7 @@ public void setReuseAddress(final boolean on) throws SocketException {

@Override
public boolean getReuseAddress() throws SocketException {
return inner.getReuseAddress();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
Expand All @@ -286,7 +260,7 @@ public void shutdownOutput() throws IOException {

@Override
public String toString() {
return inner.toString();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
Expand All @@ -296,28 +270,28 @@ public boolean isConnected() {

@Override
public boolean isBound() {
return inner.isBound();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public boolean isClosed() {
return inner.isClosed();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public boolean isInputShutdown() {
return inner.isInputShutdown();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public boolean isOutputShutdown() {
return inner.isOutputShutdown();
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public void setPerformancePreferences(final int connectionTime, final int latency,
final int bandwidth) {
inner.setPerformancePreferences(connectionTime, latency, bandwidth);
throw new UnsupportedOperationException("Unimplemented");
}

interface SocketOptionSetter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,6 +34,8 @@
import java.net.SocketTimeoutException;
import java.net.URI;

import jnr.unixsocket.UnixSocketAddress;

/**
* Provides a ConnectionSocketFactory for connecting Apache HTTP clients to Unix sockets.
*/
Expand Down Expand Up @@ -74,7 +75,7 @@ public Socket connectSocket(final int connectTimeout,
final InetSocketAddress localAddress,
final HttpContext context) throws IOException {
try {
socket.connect(new AFUNIXSocketAddress(socketFile), connectTimeout);
socket.connect(new UnixSocketAddress(socketFile), connectTimeout);
} catch (SocketTimeoutException e) {
throw new ConnectTimeoutException(e, null, remoteAddress.getAddress());
}
Expand Down

0 comments on commit 335bfee

Please sign in to comment.