Skip to content

Commit 8f7e8f6

Browse files
committed
wip
1 parent 40c6bb9 commit 8f7e8f6

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@
405405
<groups>integration</groups>
406406
<!-- <excludedGroups>ignoreInCircleCi</excludedGroups> -->
407407
<includes>
408-
<include>**/*Test.java</include>
408+
<include>**/*ExecTest.java</include>
409409
</includes>
410410
</configuration>
411411
</execution>

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
package jnr.enxio.channels;
2020

2121
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.io.StringWriter;
2224
import java.nio.ByteBuffer;
2325
import java.nio.channels.ByteChannel;
2426
import java.nio.channels.SocketChannel;
2527
import java.nio.channels.spi.SelectorProvider;
2628

29+
import io.netty.buffer.ByteBufUtil;
2730
import jnr.constants.platform.Errno;
2831
import jnr.constants.platform.Shutdown;
2932

@@ -96,6 +99,9 @@ public int read(ByteBuffer dst) throws IOException {
9699
}
97100

98101
public int write(ByteBuffer src) throws IOException {
102+
103+
System.out.println("write: " + hexDump(src, ""));
104+
99105
int n = Native.write(fd, src);
100106
if (n < 0) {
101107
throw new IOException(Native.getLastErrorString());
@@ -105,6 +111,7 @@ public int write(ByteBuffer src) throws IOException {
105111
}
106112

107113
public SocketChannel shutdownInput() throws IOException {
114+
System.out.println("shutdownInput");
108115
int n = Native.shutdown(fd, SHUT_RD);
109116
if (n < 0) {
110117
throw new IOException(Native.getLastErrorString());
@@ -113,6 +120,7 @@ public SocketChannel shutdownInput() throws IOException {
113120
}
114121

115122
public SocketChannel shutdownOutput() throws IOException {
123+
System.out.println("shutdownOutput");
116124
int n = Native.shutdown(fd, SHUT_WR);
117125
if (n < 0) {
118126
throw new IOException(Native.getLastErrorString());
@@ -122,4 +130,66 @@ public SocketChannel shutdownOutput() throws IOException {
122130

123131
private static final int SHUT_RD = Shutdown.SHUT_RD.intValue();
124132
private static final int SHUT_WR = Shutdown.SHUT_WR.intValue();
133+
134+
public static String hexDump (ByteBuffer buf, String prefix)
135+
{
136+
buf = buf.duplicate();
137+
StringWriter str = new StringWriter ();
138+
PrintWriter out = new PrintWriter (str);
139+
int i = 0;
140+
int len = buf.remaining();
141+
byte[] line = new byte[16];
142+
while (i < len)
143+
{
144+
if (prefix != null)
145+
out.print(prefix);
146+
out.print(formatInt (i, 16, 8));
147+
out.print(" ");
148+
int l = Math.min(16, len - i);
149+
buf.get(line, 0, l);
150+
String s = toHexString(line, 0, l, ' ');
151+
out.print(s);
152+
for (int j = s.length(); j < 49; j++)
153+
out.print(' ');
154+
for (int j = 0; j < l; j++)
155+
{
156+
int c = line[j] & 0xFF;
157+
if (c < 0x20 || c > 0x7E)
158+
out.print('.');
159+
else
160+
out.print((char) c);
161+
}
162+
out.println();
163+
i += 16;
164+
}
165+
return str.toString();
166+
}
167+
168+
public static String formatInt(int i, int radix, int len)
169+
{
170+
String s = Integer.toString(i, radix);
171+
StringBuffer buf = new StringBuffer();
172+
for (int j = 0; j < len - s.length(); j++)
173+
buf.append("0");
174+
buf.append(s);
175+
return buf.toString();
176+
}
177+
178+
public static String toHexString(byte[] buf, int off, int len, char sep)
179+
{
180+
StringBuffer str = new StringBuffer();
181+
for (int i = 0; i < len; i++)
182+
{
183+
str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
184+
str.append(HEX.charAt(buf[i+off] & 0x0F));
185+
if (i < len - 1)
186+
str.append(sep);
187+
}
188+
return str.toString();
189+
}
190+
191+
192+
static final String HEX = "0123456789abcdef";
193+
194+
125195
}

src/main/java/jnr/unixsocket/UnixSocketChannel.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public SocketAddress getRemoteAddress() throws IOException {
286286

287287
@Override
288288
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
289+
System.err.println("read unsupported");
289290
throw new UnsupportedOperationException("read");
290291
}
291292

@@ -303,12 +304,45 @@ public Socket socket() {
303304

304305
@Override
305306
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
306-
throw new UnsupportedOperationException("write");
307+
System.out.println("write buffers: " + srcs.length + " offset: " + offset + " length: " + length);
308+
309+
if (state == State.CONNECTED) {
310+
long result = 0;
311+
int index = 0;
312+
int remaining = 0;
313+
314+
for (index = offset; index < length; index++) {
315+
remaining += srcs[index].remaining();
316+
System.out.println("index: " + index + " remaining: " + remaining);
317+
}
318+
319+
System.out.println("remaining: " + remaining);
320+
321+
322+
ByteBuffer buffer = ByteBuffer.allocate(remaining);
323+
324+
for (index = offset; index < length; index++) {
325+
buffer.put(srcs[index]);
326+
}
327+
328+
buffer.position(0);
329+
330+
System.out.println("buffer.remaining: " + buffer.remaining());
331+
332+
result = write(buffer);
333+
System.out.println("finally written: " + result);
334+
335+
return result;
336+
} else if (state == State.IDLE) {
337+
return 0;
338+
} else {
339+
throw new ClosedChannelException();
340+
}
307341
}
308342

309343
@Override
310344
public int read(ByteBuffer dst) throws IOException {
311-
System.out.println("state: " + state);
345+
System.out.println("read state: " + state);
312346

313347
if (state == State.CONNECTED) {
314348
return super.read(dst);
@@ -317,6 +351,20 @@ public int read(ByteBuffer dst) throws IOException {
317351
} else {
318352
throw new ClosedChannelException();
319353
}
354+
}
355+
356+
@Override
357+
public int write(ByteBuffer src) throws IOException {
358+
System.out.println("write state: " + state);
320359

360+
if (state == State.CONNECTED) {
361+
int written = super.write(src);
362+
System.out.println("finally written: " + written);
363+
return written;
364+
} else if (state == State.IDLE) {
365+
return 0;
366+
} else {
367+
throw new ClosedChannelException();
368+
}
321369
}
322370
}

src/test/java/com/github/dockerjava/netty/exec/AttachContainerCmdExecTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public void onNext(Frame frame) {
7979
@Test
8080
public void attachContainerWithStdin() throws Exception {
8181

82-
String snippet = "hello world";
83-
84-
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("/bin/sh", "-c", "read line && echo $line")
82+
String snippet = "hello world!";
83+
84+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("/bin/sh", "-c", "echo running >> /tmp/attach && read line && echo $line > /tmp/attach && sleep 9999")
8585
.withTty(false).withStdinOpen(true).exec();
8686

8787
LOG.info("Created container: {}", container.toString());
@@ -105,7 +105,7 @@ public void onNext(Frame frame) {
105105
InputStream stdin = new ByteArrayInputStream((snippet + "\n").getBytes());
106106

107107
dockerClient.attachContainerCmd(container.getId()).withStdErr(true).withStdOut(true).withFollowStream(true)
108-
.withStdIn(stdin).exec(callback).awaitCompletion(2, TimeUnit.SECONDS);
108+
.withStdIn(stdin).exec(callback).awaitCompletion(5, TimeUnit.MINUTES);
109109
callback.close();
110110

111111
assertThat(callback.toString(), containsString(snippet));

0 commit comments

Comments
 (0)