| 1 | package kaka.cakelight; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.File; |
| 5 | import java.io.FileReader; |
| 6 | import java.io.IOException; |
| 7 | |
| 8 | public class PipeController extends Thread { |
| 9 | private static final String PIPE = "cakectrl"; |
| 10 | private Console console; |
| 11 | |
| 12 | public static void start(Console console) { |
| 13 | new PipeController(console).start(); |
| 14 | } |
| 15 | |
| 16 | private PipeController(Console console) { |
| 17 | this.console = console; |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public void run() { |
| 22 | while (true) { |
| 23 | createPipe(); |
| 24 | try (BufferedReader reader = new BufferedReader(new FileReader(new File(PIPE)))) { |
| 25 | String input; |
| 26 | while ((input = reader.readLine()) != null) { |
| 27 | console.handleInput(input); |
| 28 | } |
| 29 | } catch (IOException e) { |
| 30 | System.out.println("Error reading from pipe '" + PIPE + "'"); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private void createPipe() { |
| 36 | try { |
| 37 | Runtime.getRuntime().exec("mkfifo " + PIPE); |
| 38 | } catch (IOException e) { |
| 39 | e.printStackTrace(); |
| 40 | } |
| 41 | } |
| 42 | } |