Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Feb 22, 2017
1 parent 4e8aa11 commit 456d420
Showing 1 changed file with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -83,40 +84,45 @@ InetSocketAddress start() throws IOException {
try {
Socket incomingSocket = listenSocket.accept();
log.debug("Accepting incoming connection from {}", incomingSocket.getRemoteSocketAddress());

InputStream socketIn = incomingSocket.getInputStream();
OutputStream socketOut = incomingSocket.getOutputStream();
new Thread(() -> {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(NAMED_PIPE_FILE_NAME, "rw")) {
try (
RandomAccessFile randomAccessFile = new RandomAccessFile(NAMED_PIPE_FILE_NAME, "rw");
) {

log.info("Created RandomAccessFile from " + NAMED_PIPE_FILE_NAME);

Thread threadA = new Thread(() -> {
log.info("Redirecting input");
try {
FileOutputStream fileOut = new FileOutputStream(randomAccessFile.getFD());
try (
InputStream socketIn = incomingSocket.getInputStream();
FileOutputStream fileOut = new FileOutputStream(randomAccessFile.getFD());
) {
final byte[] buffer = new byte[1024 * 4];
int n;
while (true) {
final byte[] buffer = new byte[1024];
if (IOUtils.EOF == (n = socketIn.read(buffer))) {
break;
}
fileOut.write(buffer, 0, n);
System.out.println("Sent " + n + " bytes to a file. Value: " + new String(buffer, 0, n));
}
log.info("Done redirecting input");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("input", e);
}
});

Thread threadB = new Thread(() -> {
log.info("Redirecting output");
try {
FileInputStream fileIn = new FileInputStream(randomAccessFile.getFD());
try (
OutputStream socketOut = incomingSocket.getOutputStream();
FileInputStream fileIn = new FileInputStream(randomAccessFile.getFD());
) {
final byte[] buffer = new byte[1024 * 4];
int n;
while (true) {
final byte[] buffer = new byte[1024];
if (IOUtils.EOF == (n = fileIn.read(buffer))) {
break;
}
Expand All @@ -125,6 +131,8 @@ InetSocketAddress start() throws IOException {
socketOut.flush();
}
log.info("Done redirecting output");
IOUtils.closeQuietly(incomingSocket);
IOUtils.closeQuietly(randomAccessFile);
} catch (Exception e) {
log.warn("output", e);
}
Expand All @@ -138,22 +146,6 @@ InetSocketAddress start() throws IOException {
log.info("all futures completed");
} catch (Exception e) {
log.warn("", e);
} finally {
try {
incomingSocket.close();
} catch (IOException e) {
log.warn("", e);
}
try {
socketIn.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socketOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

Expand All @@ -173,10 +165,10 @@ InetSocketAddress start() throws IOException {
});
InetSocketAddress inetSocketAddress = (InetSocketAddress) listenSocket.getLocalSocketAddress();

// log.info("Pinging...");
// log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));
// log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));
// log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));
log.info("Pinging...");
log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));
log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));
log.info("Ping: " + IOUtils.toString(URI.create("http://localhost:" + inetSocketAddress.getPort() + "/v1.25/_ping")));

return inetSocketAddress;
}
Expand Down

0 comments on commit 456d420

Please sign in to comment.