Skip to content

Commit bc05ff9

Browse files
committed
start netty implementation (proof of concept)
1 parent 2d828ad commit bc05ff9

22 files changed

Lines changed: 1635 additions & 105 deletions

pom.xml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,25 @@
204204
</dependency>
205205

206206
<dependency>
207-
<groupId>org.asynchttpclient</groupId>
208-
<artifactId>async-http-client</artifactId>
209-
<version>2.0.0-alpha22</version>
207+
<groupId>io.netty</groupId>
208+
<artifactId>netty-codec-http</artifactId>
209+
<version>4.1.0.Beta7</version>
210+
</dependency>
211+
<dependency>
212+
<groupId>io.netty</groupId>
213+
<artifactId>netty-handler</artifactId>
214+
<version>4.1.0.Beta7</version>
215+
</dependency>
216+
<dependency>
217+
<groupId>io.netty</groupId>
218+
<artifactId>netty-handler-proxy</artifactId>
219+
<version>4.1.0.Beta7</version>
220+
</dependency>
221+
<dependency>
222+
<groupId>io.netty</groupId>
223+
<artifactId>netty-transport-native-epoll</artifactId>
224+
<version>4.1.0.Beta7</version>
225+
<classifier>linux-x86_64</classifier>
210226
</dependency>
211227
</dependencies>
212228

@@ -222,6 +238,13 @@
222238
</distributionManagement>
223239

224240
<build>
241+
<extensions>
242+
<extension>
243+
<groupId>kr.motd.maven</groupId>
244+
<artifactId>os-maven-plugin</artifactId>
245+
<version>1.2.3.Final</version>
246+
</extension>
247+
</extensions>
225248
<pluginManagement>
226249
<plugins>
227250

src/main/java/com/github/dockerjava/ahc/AttachContainerCmdExec.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/main/java/com/github/dockerjava/api/command/ExecCreateCmdResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.dockerjava.api.command;
22

3+
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
4+
import org.apache.commons.lang.builder.ToStringStyle;
5+
36
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
47
import com.fasterxml.jackson.annotation.JsonProperty;
58

@@ -12,4 +15,9 @@ public class ExecCreateCmdResponse {
1215
public String getId() {
1316
return id;
1417
}
18+
19+
@Override
20+
public String toString() {
21+
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
22+
}
1523
}

src/main/java/com/github/dockerjava/core/command/FrameReader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.InputStream;
55
import java.util.Arrays;
66

7+
import org.apache.commons.io.HexDump;
8+
79
import com.github.dockerjava.api.model.Frame;
810
import com.github.dockerjava.api.model.StreamType;
911

@@ -16,11 +18,12 @@ public class FrameReader implements AutoCloseable {
1618

1719
private static final int HEADER_SIZE = 8;
1820

21+
private final byte[] rawBuffer = new byte[1000];
22+
1923
private final InputStream inputStream;
2024

2125
private Boolean rawStreamDetected = false;
2226

23-
private final byte[] rawBuffer = new byte[1000];
2427

2528
public FrameReader(InputStream inputStream) {
2629
this.inputStream = inputStream;
@@ -65,6 +68,8 @@ public Frame readFrame() throws IOException {
6568
actualHeaderSize += headerCount;
6669
} while (actualHeaderSize < HEADER_SIZE);
6770

71+
// HexDump.dump(header, 0, System.err, 0);
72+
6873
StreamType streamType = streamType(header[0]);
6974

7075
if (streamType.equals(StreamType.RAW)) {
@@ -74,6 +79,8 @@ public Frame readFrame() throws IOException {
7479

7580
int payloadSize = ((header[4] & 0xff) << 24) + ((header[5] & 0xff) << 16) + ((header[6] & 0xff) << 8)
7681
+ (header[7] & 0xff);
82+
83+
//System.out.println("payload size: " + payloadSize);
7784

7885
byte[] payload = new byte[payloadSize];
7986
int actualPayloadSize = 0;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.dockerjava.netty;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.github.dockerjava.api.model.AuthConfig;
5+
import com.github.dockerjava.api.model.AuthConfigurations;
6+
import com.github.dockerjava.core.DockerClientConfig;
7+
import com.github.dockerjava.core.RemoteApiVersion;
8+
import org.apache.commons.codec.binary.Base64;
9+
10+
import java.io.IOException;
11+
12+
import static com.google.common.base.Preconditions.checkNotNull;
13+
14+
public abstract class AbstrDockerCmdExec {
15+
16+
private final DockerClientConfig dockerClientConfig;
17+
18+
private final WebTarget baseResource;
19+
20+
public AbstrDockerCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
21+
checkNotNull(baseResource, "baseResource was not specified");
22+
checkNotNull(dockerClientConfig, "dockerClientConfig was not specified");
23+
this.baseResource = baseResource;
24+
this.dockerClientConfig = dockerClientConfig;
25+
}
26+
27+
protected WebTarget getBaseResource() {
28+
return baseResource;
29+
}
30+
31+
protected AuthConfigurations getBuildAuthConfigs() {
32+
return dockerClientConfig.getAuthConfigurations();
33+
}
34+
35+
protected String registryAuth(AuthConfig authConfig) {
36+
try {
37+
return Base64.encodeBase64String(new ObjectMapper().writeValueAsString(authConfig).getBytes());
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
protected String registryConfigs(AuthConfigurations authConfigs) {
44+
try {
45+
final String json;
46+
if (dockerClientConfig.getVersion().isGreaterOrEqual(RemoteApiVersion.VERSION_1_19)) {
47+
json = new ObjectMapper().writeValueAsString(authConfigs.getConfigs());
48+
} else {
49+
json = new ObjectMapper().writeValueAsString(authConfigs);
50+
}
51+
52+
return Base64.encodeBase64String(json.getBytes());
53+
} catch (IOException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
58+
protected boolean bool(Boolean bool) {
59+
return bool != null && bool;
60+
}
61+
62+
protected WebTarget booleanQueryParam(WebTarget webTarget, String name, Boolean value) {
63+
if (bool(value)) {
64+
webTarget = webTarget.queryParam(name, bool(value) + "");
65+
}
66+
67+
return webTarget;
68+
}
69+
70+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.dockerjava.netty;
2+
3+
import com.github.dockerjava.api.DockerException;
4+
import com.github.dockerjava.api.command.DockerCmd;
5+
import com.github.dockerjava.api.command.DockerCmdSyncExec;
6+
import com.github.dockerjava.core.DockerClientConfig;
7+
8+
9+
public abstract class AbstrSyncDockerCmdExec<CMD_T extends DockerCmd<RES_T>, RES_T> extends AbstrDockerCmdExec
10+
implements DockerCmdSyncExec<CMD_T, RES_T> {
11+
12+
public AbstrSyncDockerCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
13+
super(baseResource, dockerClientConfig);
14+
}
15+
16+
@Override
17+
public RES_T exec(CMD_T command) {
18+
// this hack works because of ResponseStatusExceptionFilter
19+
try (CMD_T cmd = command) {
20+
try {
21+
return execute(cmd);
22+
} catch (RuntimeException e) {
23+
if (e.getCause() instanceof DockerException) {
24+
throw (DockerException) e.getCause();
25+
} else {
26+
throw e;
27+
}
28+
}
29+
}
30+
}
31+
32+
protected abstract RES_T execute(CMD_T command);
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.dockerjava.netty;
2+
3+
import io.netty.channel.Channel;
4+
5+
public interface ChannelProvider {
6+
Channel getChannel();
7+
}

0 commit comments

Comments
 (0)