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 
32  void start();
33 
34 protected:
35  CliConnection(CommandController& commandController,
36  EventDistributor& eventDistributor);
37 
38  virtual void output(string_ref message) = 0;
39 
44  void end();
45 
49  virtual void close() = 0;
50 
56  void startOutput();
57 
58  xmlParserCtxt* parser_context;
59  Thread thread; // TODO: Possible to make this private?
60 
61 private:
62  void execute(const std::string& command);
63 
64  // CliListener
65  virtual void log(CliComm::LogLevel level, string_ref message);
66  virtual void update(CliComm::UpdateType type, string_ref machine,
67  string_ref name, string_ref value);
68 
69  // EventListener
70  virtual int signalEvent(const std::shared_ptr<const Event>& event);
71 
72  enum State {
73  START, TAG_OPENMSX, TAG_COMMAND, END
74  };
75  struct ParseState {
76  State state;
77  unsigned unknownLevel;
78  std::string content;
79  CliConnection* object;
80  };
81 
82  static void cb_start_element(void* user_data, const xmlChar* localname,
83  const xmlChar* prefix, const xmlChar* uri,
84  int nb_namespaces, const xmlChar** namespaces,
85  int nb_attributes, int nb_defaulted,
86  const xmlChar** attrs);
87  static void cb_end_element(void* user_data, const xmlChar* localname,
88  const xmlChar* prefix, const xmlChar* uri);
89  static void cb_text(void* user_data, const xmlChar* chars, int len);
90 
91  xmlSAXHandler sax_handler;
92  ParseState user_data;
93 
94  CommandController& commandController;
95  EventDistributor& eventDistributor;
96 
97  bool updateEnabled[CliComm::NUM_UPDATES];
98 };
99 
101 {
102 public:
103  StdioConnection(CommandController& commandController,
104  EventDistributor& eventDistributor);
105  virtual ~StdioConnection();
106 
107  virtual void output(string_ref message);
108 
109 private:
110  virtual void close();
111  virtual void run();
112 
113  bool ok;
114 };
115 
116 #ifdef _WIN32
117 class PipeConnection : public CliConnection
118 {
119 public:
120  PipeConnection(CommandController& commandController,
121  EventDistributor& eventDistributor,
122  string_ref name);
123  virtual ~PipeConnection();
124 
125  virtual void output(string_ref message);
126 
127 private:
128  virtual void close();
129  virtual void run();
130 
131  HANDLE pipeHandle;
132  HANDLE shutdownEvent;
133 };
134 #endif
135 
137 {
138 public:
139  SocketConnection(CommandController& commandController,
140  EventDistributor& eventDistributor,
141  SOCKET sd);
142  virtual ~SocketConnection();
143 
144  virtual void output(string_ref message);
145 
146 private:
147  virtual void close();
148  virtual void run();
149 
150  Semaphore sem;
151  SOCKET sd;
152  bool established;
153 };
154 
155 } // namespace openmsx
156 
157 #endif
virtual void output(string_ref message)=0
xmlParserCtxt * parser_context
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void start()
Starts the helper thread.
int SOCKET
Definition: Socket.hh:22
StdioConnection(CommandController &commandController, EventDistributor &eventDistributor)
bool getUpdateEnable(CliComm::UpdateType type) const
virtual void output(string_ref message)
virtual void close()=0
Close the connection.
CliConnection(CommandController &commandController, EventDistributor &eventDistributor)
virtual void output(string_ref message)
void end()
End this connection by sending the closing tag and then closing the stream.
SocketConnection(CommandController &commandController, EventDistributor &eventDistributor, SOCKET sd)
void startOutput()
Send opening XML tag, should be called exactly once by a subclass shortly after opening a connection...
void setUpdateEnable(CliComm::UpdateType type, bool value)