Skip to content

Commit 40c6bb9

Browse files
committed
wip
1 parent b7ad96e commit 40c6bb9

File tree

4 files changed

+193
-178
lines changed

4 files changed

+193
-178
lines changed

src/main/java/com/github/dockerjava/netty/DockerCmdExecFactoryImpl.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,8 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44

55
import java.io.IOException;
6-
import java.net.InetAddress;
7-
import java.net.InetSocketAddress;
8-
import java.net.ProtocolFamily;
9-
import java.net.SocketAddress;
10-
import java.nio.channels.DatagramChannel;
11-
import java.nio.channels.Pipe;
12-
import java.nio.channels.SelectionKey;
13-
import java.nio.channels.Selector;
14-
import java.nio.channels.ServerSocketChannel;
15-
import java.nio.channels.spi.AbstractSelectableChannel;
16-
import java.nio.channels.spi.AbstractSelector;
176
import java.nio.channels.spi.SelectorProvider;
187
import java.security.Security;
19-
import java.util.Set;
208

219
import javax.net.ssl.SSLContext;
2210
import javax.net.ssl.SSLEngine;
@@ -144,7 +132,6 @@
144132
import jnr.enxio.channels.NativeSelectorProvider;
145133
import jnr.unixsocket.UnixSocketAddress;
146134
import jnr.unixsocket.UnixSocketChannel;
147-
import unisockets.Addr;
148135

149136
/**
150137
* Experimental implementation of {@link DockerCmdExecFactory} that supports http connection hijacking that is needed to pass STDIN to the
@@ -232,8 +219,8 @@ private class UnixDomainSocketInitializer implements NettyInitializer {
232219

233220
@Override
234221
public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) {
235-
final SelectorProvider nativeSelectorProvider = NativeSelectorProvider.getInstance();
236-
222+
final SelectorProvider nativeSelectorProvider = NativeSelectorProvider.getInstance();
223+
237224
EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0,
238225
new DefaultThreadFactory(threadPrefix), nativeSelectorProvider);
239226

@@ -242,17 +229,18 @@ public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientC
242229
@Override
243230
public NioSocketChannel newChannel() {
244231
try {
245-
return new NioSocketChannel(UnixSocketChannel.create());
246-
} catch (IOException e) {
247-
throw new RuntimeException();
248-
}
232+
return new NioSocketChannel(UnixSocketChannel.create());
233+
} catch (IOException e) {
234+
throw new RuntimeException();
235+
}
249236
}
250237
};
251238

252239
bootstrap.group(nioEventLoopGroup).channelFactory(factory)
253240
.handler(new ChannelInitializer<SocketChannel>() {
254241
@Override
255242
protected void initChannel(final SocketChannel channel) throws Exception {
243+
channel.pipeline().addLast(new LoggingHandler(getClass()));
256244
channel.pipeline().addLast(new HttpClientCodec());
257245
}
258246
});
@@ -263,13 +251,13 @@ protected void initChannel(final SocketChannel channel) throws Exception {
263251
@Override
264252
public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException {
265253

266-
if(!path.exists()) {
267-
throw new RuntimeException("socket not found: " + path);
254+
if (!path.exists()) {
255+
throw new RuntimeException("socket not found: " + path);
268256
}
269-
270-
UnixSocketAddress socket = new UnixSocketAddress(path);
271257

272-
return (DuplexChannel) bootstrap.connect(socket).sync().channel();
258+
UnixSocketAddress address = new UnixSocketAddress(path);
259+
260+
return (DuplexChannel) bootstrap.connect(address).sync().channel();
273261
}
274262
}
275263

@@ -278,9 +266,9 @@ private class InetSocketInitializer implements NettyInitializer {
278266
public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) {
279267
EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
280268

281-
InetAddress addr = InetAddress.getLoopbackAddress();
269+
//InetAddress addr = InetAddress.getLoopbackAddress();
282270

283-
final SocketAddress proxyAddress = new InetSocketAddress(addr, 8008);
271+
//final SocketAddress proxyAddress = new InetSocketAddress(addr, 8008);
284272

285273
Security.addProvider(new BouncyCastleProvider());
286274

src/main/java/jnr/enxio/channels/NativeSocketChannel.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
import java.nio.channels.SocketChannel;
2525
import java.nio.channels.spi.SelectorProvider;
2626

27+
import jnr.constants.platform.Errno;
2728
import jnr.constants.platform.Shutdown;
2829

29-
public abstract class NativeSocketChannel extends SocketChannel
30-
implements ByteChannel, NativeSelectableChannel {
30+
public abstract class NativeSocketChannel extends SocketChannel implements ByteChannel, NativeSelectableChannel {
3131

3232
private int fd = -1;
3333

3434
public NativeSocketChannel(int fd) {
3535
this(NativeSelectorProvider.getInstance(), fd);
3636
}
37-
37+
3838
public NativeSocketChannel() {
3939
super(NativeSelectorProvider.getInstance());
4040
}
@@ -43,43 +43,55 @@ public NativeSocketChannel() {
4343
super(provider);
4444
this.fd = fd;
4545
}
46-
46+
4747
public void setFD(int fd) {
48-
this.fd = fd;
49-
}
48+
this.fd = fd;
49+
}
5050

5151
@Override
5252
protected void implCloseSelectableChannel() throws IOException {
53-
Native.close(fd);
53+
Native.close(fd);
5454
}
5555

5656
@Override
5757
protected void implConfigureBlocking(boolean block) throws IOException {
5858
Native.setBlocking(fd, block);
5959
}
60-
60+
6161
public final int getFD() {
6262
return fd;
6363
}
6464

6565
public int read(ByteBuffer dst) throws IOException {
66-
int n = Native.read(fd, dst);
67-
switch (n) {
68-
case 0:
69-
return -1;
66+
// System.out.println("dst.remaining: " + dst.remaining());
67+
// System.out.println("dst.limit: " + dst.limit());
7068

71-
case -1:
72-
switch (Native.getLastError()) {
73-
case EAGAIN:
74-
case EWOULDBLOCK:
75-
return 0;
69+
ByteBuffer buffer = ByteBuffer.allocate(dst.remaining());
7670

77-
default:
78-
throw new IOException(Native.getLastErrorString());
79-
}
71+
int n = Native.read(fd, buffer);
72+
System.out.println("n: " + n);
73+
74+
dst.put(buffer.array());
75+
76+
switch (n) {
77+
case 0:
78+
return -1;
79+
80+
case -1:
81+
Errno lastError = Native.getLastError();
82+
switch (lastError) {
83+
case EAGAIN:
84+
case EWOULDBLOCK:
85+
return 0;
8086

8187
default:
82-
return n;
88+
throw new IOException(Native.getLastErrorString());
89+
}
90+
91+
default: {
92+
93+
return n;
94+
}
8395
}
8496
}
8597

@@ -91,25 +103,23 @@ public int write(ByteBuffer src) throws IOException {
91103

92104
return n;
93105
}
94-
95-
protected void _shutdownInput() throws IOException {
106+
107+
public SocketChannel shutdownInput() throws IOException {
96108
int n = Native.shutdown(fd, SHUT_RD);
97109
if (n < 0) {
98110
throw new IOException(Native.getLastErrorString());
99111
}
112+
return this;
100113
}
101-
102-
103-
104-
protected void _shutdownOutput() throws IOException {
114+
115+
public SocketChannel shutdownOutput() throws IOException {
105116
int n = Native.shutdown(fd, SHUT_WR);
106117
if (n < 0) {
107118
throw new IOException(Native.getLastErrorString());
108119
}
120+
return this;
109121
}
110-
111-
private final static int SHUT_RD = Shutdown.SHUT_RD.intValue();
112-
private final static int SHUT_WR = Shutdown.SHUT_WR.intValue();
113122

114-
115-
}
123+
private static final int SHUT_RD = Shutdown.SHUT_RD.intValue();
124+
private static final int SHUT_WR = Shutdown.SHUT_WR.intValue();
125+
}

0 commit comments

Comments
 (0)