Skip to content

Commit

Permalink
Fill in additional API methods.
Browse files Browse the repository at this point in the history
ApacheUnixSocket provides API for a client to communicate with an
existing unix socket, so the API related to the server-side 'bind'
command isn't necessary. They can simply be made to return whichever
values indicate an unbound state.
  • Loading branch information
rgrunber committed Apr 14, 2015
1 parent dda298a commit a9ca94e
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/main/java/com/spotify/docker/client/ApacheUnixSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.Queue;
Expand All @@ -46,16 +47,19 @@
public class ApacheUnixSocket extends Socket {

private final UnixSocketChannel inner;
private SocketAddress addr;

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

public ApacheUnixSocket() throws IOException {
this.inner = UnixSocketChannel.open();
this.addr = null;
}

@Override
public void connect(final SocketAddress endpoint) throws IOException {
if (endpoint instanceof UnixSocketAddress) {
addr = endpoint;
inner.connect((UnixSocketAddress) endpoint);
setAllSocketOptions();
}
Expand All @@ -64,6 +68,7 @@ public void connect(final SocketAddress endpoint) throws IOException {
@Override
public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
if (endpoint instanceof UnixSocketAddress) {
addr = endpoint;
inner.connect((UnixSocketAddress) endpoint);
setAllSocketOptions();
}
Expand All @@ -76,32 +81,43 @@ public void bind(final SocketAddress bindpoint) throws IOException {

@Override
public InetAddress getInetAddress() {
throw new UnsupportedOperationException("Unimplemented");
if (inner.isConnected()) {
try {
return InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
return null;
}
}
return null;
}

@Override
public InetAddress getLocalAddress() {
throw new UnsupportedOperationException("Unimplemented");
try {
return InetAddress.getByAddress(new byte [] {0, 0, 0, 0}); // not bound
} catch (UnknownHostException e) {
return null;
}
}

@Override
public int getPort() {
throw new UnsupportedOperationException("Unimplemented");
return -1; // meaningless for UNIX sockets
}

@Override
public int getLocalPort() {
throw new UnsupportedOperationException("Unimplemented");
return -1; // not bound
}

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

@Override
public SocketAddress getLocalSocketAddress() {
throw new UnsupportedOperationException("Unimplemented");
return null; // not bound
}

@Override
Expand Down Expand Up @@ -256,7 +272,10 @@ public void shutdownOutput() throws IOException {

@Override
public String toString() {
throw new UnsupportedOperationException("Unimplemented");
if (addr != null) {
return ((UnixSocketAddress) addr).toString();
}
return inner.toString();
}

@Override
Expand All @@ -266,12 +285,12 @@ public boolean isConnected() {

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

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

@Override
Expand Down

0 comments on commit a9ca94e

Please sign in to comment.