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(); + } + } +}