Refactor client codes to use similar, standardized API
This commit is contained in:
		
							
								
								
									
										13
									
								
								clients/java/Example.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								clients/java/Example.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
public class Example {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        try (PipeIO pipeio = new PipeIO("in", "out")) {
 | 
			
		||||
            pipeio.reader.setMessageHandler(new MessageHandler(){
 | 
			
		||||
                @Override
 | 
			
		||||
                public void call(String msg) {
 | 
			
		||||
                    pipeio.writer.sendMessage(msg);
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            pipeio.reader.run();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								clients/java/MessageHandler.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								clients/java/MessageHandler.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
public interface MessageHandler {
 | 
			
		||||
    public void call(String msg);
 | 
			
		||||
}
 | 
			
		||||
@@ -2,25 +2,16 @@ import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PipeIO implements AutoCloseable {
 | 
			
		||||
    protected PipeReader reader;
 | 
			
		||||
    protected PipeWriter writer;
 | 
			
		||||
    public PipeReader reader;
 | 
			
		||||
    public PipeWriter writer;
 | 
			
		||||
 | 
			
		||||
    public PipeIO(String in_pipe_path, String out_pipe_path) throws IOException {
 | 
			
		||||
    public PipeIO(String in_pipe_path, String out_pipe_path) {
 | 
			
		||||
        this.reader = new PipeReader(in_pipe_path);
 | 
			
		||||
        this.writer = new PipeWriter(out_pipe_path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
    public void close() {
 | 
			
		||||
        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 {}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,50 @@
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.UncheckedIOException;
 | 
			
		||||
import java.lang.AutoCloseable;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PipeReader implements AutoCloseable {
 | 
			
		||||
    private BufferedReader pipe;
 | 
			
		||||
    private MessageHandler messageHandler;
 | 
			
		||||
 | 
			
		||||
    public PipeReader(String pipe_path) throws IOException {
 | 
			
		||||
        this.pipe = new BufferedReader(new FileReader(pipe_path));
 | 
			
		||||
    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) {}
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
        this.pipe.close();
 | 
			
		||||
    public void close() {
 | 
			
		||||
        try {
 | 
			
		||||
            this.pipe.close();
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            throw new UncheckedIOException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String recvMessage() throws IOException {
 | 
			
		||||
        return this.pipe.readLine();
 | 
			
		||||
    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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,35 @@
 | 
			
		||||
import java.io.BufferedWriter;
 | 
			
		||||
import java.io.FileWriter;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.UncheckedIOException;
 | 
			
		||||
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 PipeWriter(String pipe_path) {
 | 
			
		||||
        try {
 | 
			
		||||
            this.pipe = new BufferedWriter(new FileWriter(pipe_path));
 | 
			
		||||
        } catch(IOException e) {
 | 
			
		||||
            throw new UncheckedIOException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
        this.pipe.close();
 | 
			
		||||
    public void close() {
 | 
			
		||||
        try {
 | 
			
		||||
            this.pipe.close();
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            throw new UncheckedIOException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void sendMessage(String msg) throws IOException {
 | 
			
		||||
        this.pipe.write(msg + "\n");
 | 
			
		||||
        this.pipe.flush();
 | 
			
		||||
    public void sendMessage(String msg) {
 | 
			
		||||
        try {
 | 
			
		||||
            this.pipe.write(msg + "\n");
 | 
			
		||||
            this.pipe.flush();
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            throw new UncheckedIOException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +0,0 @@
 | 
			
		||||
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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user