2019-04-09 12:54:43 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2019-04-11 16:30:25 +00:00
|
|
|
import java.io.UncheckedIOException;
|
2019-04-09 12:54:43 +00:00
|
|
|
import java.lang.AutoCloseable;
|
|
|
|
|
|
|
|
|
|
|
|
public class PipeReader implements AutoCloseable {
|
|
|
|
private BufferedReader pipe;
|
2019-04-11 16:30:25 +00:00
|
|
|
private MessageHandler messageHandler;
|
2019-04-09 12:54:43 +00:00
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
public PipeReader(String pipe_path) {
|
|
|
|
try {
|
|
|
|
this.pipe = new BufferedReader(new FileReader(pipe_path));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new UncheckedIOException(e);
|
|
|
|
}
|
|
|
|
this.messageHandler = new MessageHandler(){
|
|
|
|
@Override
|
|
|
|
public void call(String msg) {}
|
|
|
|
};
|
2019-04-09 12:54:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
public void close() {
|
|
|
|
try {
|
|
|
|
this.pipe.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new UncheckedIOException(e);
|
|
|
|
}
|
2019-04-09 12:54:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
public void setMessageHandler(MessageHandler handler) {
|
|
|
|
this.messageHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
String msg;
|
|
|
|
while ((msg = this.recvMessage()) != null) {
|
|
|
|
this.messageHandler.call(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String recvMessage() {
|
|
|
|
try {
|
|
|
|
return this.pipe.readLine();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new UncheckedIOException(e);
|
|
|
|
}
|
2019-04-09 12:54:43 +00:00
|
|
|
}
|
|
|
|
}
|