Skip to content

Commit

Permalink
Add custom NamedPipeSocketFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
kiview committed Sep 12, 2018
1 parent 1429c9f commit 728a27f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
2 changes: 0 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ shadowJar {
include(dependency('com.squareup.okio:.*'))
include(dependency('org.scala-sbt.ipcsocket:ipcsocket'))
include(dependency('org.glassfish.main.external:trilead-ssh2-repackaged'))
include(dependency('org.mariadb.jdbc:.*'))
}

relocate("org.mariadb", "org.testcontainers.shaded.org.mariadb")
}

task jarFileTest(type: Test) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,124 @@
package org.testcontainers.dockerclient.transport.okhttp;

import com.sun.jna.platform.win32.Kernel32;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import lombok.Value;
import org.mariadb.jdbc.internal.io.socket.NamedPipeSocket;
import org.rnorth.ducttape.unreliables.Unreliables;

import javax.net.SocketFactory;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;

@Value
@EqualsAndHashCode(callSuper = false)
public class NamedPipeSocketFactory extends SocketFactory {

String socketPath;
String socketFileName;

@Override
@SneakyThrows
public Socket createSocket() {
String pipeName = socketPath.substring("//./pipe/".length()).replace("/", "\\");
return new NamedPipeSocket("localhost", pipeName);
return new Socket() {

RandomAccessFile file;
InputStream is;
OutputStream os;

@Override
public void close() throws IOException {
if (file != null) {
file.close();
file = null;
}
}

@Override
public void connect(SocketAddress endpoint) {
connect(endpoint, 0);
}

public void connect(SocketAddress endpoint, int timeout) {
file = Unreliables.retryUntilSuccess(Math.max(timeout, 10_000), TimeUnit.MILLISECONDS, () -> {
try {
return new RandomAccessFile(socketFileName, "rw");
} catch (FileNotFoundException e) {
Kernel32.INSTANCE.WaitNamedPipe(socketFileName, 100);
throw e;
}
});

is = new InputStream() {
@Override
public int read(byte[] bytes, int off, int len) throws IOException {
return file.read(bytes, off, len);
}

@Override
public int read() throws IOException {
return file.read();
}

@Override
public int read(byte[] bytes) throws IOException {
return file.read(bytes);
}
};

os = new OutputStream() {
@Override
public void write(byte[] bytes, int off, int len) throws IOException {
file.write(bytes, off, len);
}

@Override
public void write(int value) throws IOException {
file.write(value);
}

@Override
public void write(byte[] bytes) throws IOException {
file.write(bytes);
}
};
}

public InputStream getInputStream() {
return is;
}

public OutputStream getOutputStream() {
return os;
}

public void setTcpNoDelay(boolean bool) {
}

public void setKeepAlive(boolean bool) {
}

public void setReceiveBufferSize(int size) {
}

public void setSendBufferSize(int size) {
}

public void setSoLinger(boolean bool, int value) {
}

@Override
public void setSoTimeout(int timeout) {
}

public void shutdownInput() {
}

public void shutdownOutput() {
}
};
}

@Override
Expand Down

0 comments on commit 728a27f

Please sign in to comment.