summaryrefslogtreecommitdiff
path: root/src/autoplay/child_process.h
blob: 6c03ecd0a7341a25fd5302da434f7711febeb636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// child_process.h -- description

#ifndef _CHILD_PROCESS_H_
#define _CHILD_PROCESS_H_

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
using namespace std;

namespace util {

class ChildProcess {
 public:
  explicit ChildProcess(const char *commandline)
    : commandline_(strdup(commandline)),
      name_(NULL),
      running_(false),
      pid_(-1),
      to_(NULL),
      from_(NULL),
      child_ready_(false) {}
  virtual ~ChildProcess() {
    Stop();
    free(commandline_);
    commandline_ = NULL;
  }

  virtual void Start();
  virtual void Stop();
  void Send(char *line);
  void Receive(char *line, int size);
  bool Poll();
  void Flush();

 private:
  void ParseCommandline(vector<char *> *args);
  char *commandline_;
  char *name_;
  bool running_;
  pid_t pid_;
  int write_pipe_[2];
  int read_pipe_[2];
  FILE *to_;
  FILE *from_;
  volatile bool child_ready_;
};

}  // namespace

#endif /* _CHILD_PROCESS_H_ */