Implement Java bindings for pipe io client

This commit is contained in:
Kristóf Tóth 2019-04-09 14:54:43 +02:00
parent 586c8b8503
commit 61eb57c918
4 changed files with 91 additions and 0 deletions

26
clients/java/PipeIO.java Normal file
View File

@ -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 {}
}

View File

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

View File

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

22
clients/java/Test.java Normal file
View File

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