From 61eb57c9183acbb2db5370428dede8f0cab0bf78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 9 Apr 2019 14:54:43 +0200 Subject: [PATCH] Implement Java bindings for pipe io client --- clients/java/PipeIO.java | 26 ++++++++++++++++++++++++++ clients/java/PipeReader.java | 21 +++++++++++++++++++++ clients/java/PipeWriter.java | 22 ++++++++++++++++++++++ clients/java/Test.java | 22 ++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 clients/java/PipeIO.java create mode 100644 clients/java/PipeReader.java create mode 100644 clients/java/PipeWriter.java create mode 100644 clients/java/Test.java diff --git a/clients/java/PipeIO.java b/clients/java/PipeIO.java new file mode 100644 index 0000000..b9e8702 --- /dev/null +++ b/clients/java/PipeIO.java @@ -0,0 +1,26 @@ +import java.io.IOException; + + +public class PipeIO implements AutoCloseable { + protected PipeReader reader; + protected PipeWriter writer; + + public PipeIO(String in_pipe_path, String out_pipe_path) throws IOException { + this.reader = new PipeReader(in_pipe_path); + this.writer = new PipeWriter(out_pipe_path); + } + + public void close() throws IOException { + this.reader.close(); + this.writer.close(); + } + + public void run() throws IOException { + String msg; + while ((msg = this.reader.recvMessage()) != null) { + this.handleMessage(msg); + } + } + + public void handleMessage(String msg) throws IOException {} +} diff --git a/clients/java/PipeReader.java b/clients/java/PipeReader.java new file mode 100644 index 0000000..b83f11b --- /dev/null +++ b/clients/java/PipeReader.java @@ -0,0 +1,21 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.lang.AutoCloseable; + + +public class PipeReader implements AutoCloseable { + private BufferedReader pipe; + + public PipeReader(String pipe_path) throws IOException { + this.pipe = new BufferedReader(new FileReader(pipe_path)); + } + + public void close() throws IOException { + this.pipe.close(); + } + + public String recvMessage() throws IOException { + return this.pipe.readLine(); + } +} diff --git a/clients/java/PipeWriter.java b/clients/java/PipeWriter.java new file mode 100644 index 0000000..1329a38 --- /dev/null +++ b/clients/java/PipeWriter.java @@ -0,0 +1,22 @@ +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.AutoCloseable; + + +public class PipeWriter implements AutoCloseable { + private BufferedWriter pipe; + + public PipeWriter(String pipe_path) throws IOException { + this.pipe = new BufferedWriter(new FileWriter(pipe_path)); + } + + public void close() throws IOException { + this.pipe.close(); + } + + public void sendMessage(String msg) throws IOException { + this.pipe.write(msg + "\n"); + this.pipe.flush(); + } +} diff --git a/clients/java/Test.java b/clients/java/Test.java new file mode 100644 index 0000000..9f936de --- /dev/null +++ b/clients/java/Test.java @@ -0,0 +1,22 @@ +import java.io.IOException; + + +public class Test extends PipeIO { + public Test(String in_pipe_path, String out_pipe_path) throws IOException { + super(in_pipe_path, out_pipe_path); + } + + @Override + public void handleMessage(String msg) throws IOException { + this.writer.sendMessage(msg); + } + + public static void main(String[] args) { + try(Test test = new Test("in", "out")) { + test.run(); + } + catch (IOException e) { + e.printStackTrace(); + } + } +}