Add command control via named pipe
[kaka/cakelight.git] / src / kaka / cakelight / PipeController.java
CommitLineData
f1a6a6a5
TW
1package kaka.cakelight;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileReader;
6import java.io.IOException;
7
8public 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}