openMSX
CliConnection.hh
Go to the documentation of this file.
1 #ifndef CLICONNECTION_HH
2 #define CLICONNECTION_HH
3 
4 #include "CliListener.hh"
5 #include "Thread.hh"
6 #include "Semaphore.hh"
7 #include "EventListener.hh"
8 #include "Socket.hh"
9 #include "CliComm.hh"
10 #include <libxml/parser.h>
11 #include <string>
12 
13 namespace openmsx {
14 
15 class CommandController;
16 class EventDistributor;
17 
18 class CliConnection : public CliListener, private EventListener,
19  protected Runnable
20 {
21 public:
22  virtual ~CliConnection();
23 
24  void setUpdateEnable(CliComm::UpdateType type, bool value);
25  bool getUpdateEnable(CliComm::UpdateType type) const;
26 
27 protected:
28  CliConnection(CommandController& commandController,
29  EventDistributor& eventDistributor);
30 
31  virtual void output(string_ref message) = 0;
32 
37  void start();
38 
43  void end();
44 
48  virtual void close() = 0;
49 
55  void startOutput();
56 
57  xmlParserCtxt* parser_context;
58  Thread thread; // TODO: Possible to make this private?
59 
60 private:
61  void execute(const std::string& command);
62 
63  // CliListener
64  virtual void log(CliComm::LogLevel level, string_ref message);
65  virtual void update(CliComm::UpdateType type, string_ref machine,
66  string_ref name, string_ref value);
67 
68  // EventListener
69  virtual int signalEvent(const std::shared_ptr<const Event>& event);
70 
71  enum State {
72  START, TAG_OPENMSX, TAG_COMMAND, END
73  };
74  struct ParseState {
75  State state;
76  unsigned unknownLevel;
77  std::string content;
78  CliConnection* object;
79  };
80 
81  static void cb_start_element(void* user_data, const xmlChar* localname,
82  const xmlChar* prefix, const xmlChar* uri,
83  int nb_namespaces, const xmlChar** namespaces,
84  int nb_attributes, int nb_defaulted,
85  const xmlChar** attrs);
86  static void cb_end_element(void* user_data, const xmlChar* localname,
87  const xmlChar* prefix, const xmlChar* uri);
88  static void cb_text(void* user_data, const xmlChar* chars, int len);
89 
90  xmlSAXHandler sax_handler;
91  ParseState user_data;
92 
93  CommandController& commandController;
94  EventDistributor& eventDistributor;
95 
96  bool updateEnabled[CliComm::NUM_UPDATES];
97 };
98 
100 {
101 public:
102  StdioConnection(CommandController& commandController,
103  EventDistributor& eventDistributor);
104  virtual ~StdioConnection();
105 
106  virtual void output(string_ref message);
107 
108 private:
109  virtual void close();
110  virtual void run();
111 
112  bool ok;
113 };
114 
115 #ifdef _WIN32
116 class PipeConnection : public CliConnection
117 {
118 public:
119  PipeConnection(CommandController& commandController,
120  EventDistributor& eventDistributor,
121  string_ref name);
122  virtual ~PipeConnection();
123 
124  virtual void output(string_ref message);
125 
126 private:
127  virtual void close();
128  virtual void run();
129 
130  HANDLE pipeHandle;
131  HANDLE shutdownEvent;
132 };
133 #endif
134 
136 {
137 public:
138  SocketConnection(CommandController& commandController,
139  EventDistributor& eventDistributor,
140  SOCKET sd);
141  virtual ~SocketConnection();
142 
143  virtual void output(string_ref message);
144 
145 private:
146  virtual void close();
147  virtual void run();
148 
149  Semaphore sem;
150  SOCKET sd;
151  bool established;
152 };
153 
154 } // namespace openmsx
155 
156 #endif