Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OladHTTPServer.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * OladHTTPServer.h
17  * Interface for the OLA HTTP class
18  * Copyright (C) 2005 Simon Newton
19  */
20 
21 #ifndef OLAD_OLADHTTPSERVER_H_
22 #define OLAD_OLADHTTPSERVER_H_
23 
24 #include <time.h>
25 #include <string>
26 #include <vector>
27 #include "ola/ExportMap.h"
28 #include "ola/client/OlaClient.h"
29 #include "ola/base/Macro.h"
30 #include "ola/http/HTTPServer.h"
31 #include "ola/http/OlaHTTPServer.h"
32 #include "ola/network/Interface.h"
33 #include "ola/rdm/PidStore.h"
34 #include "olad/RDMHTTPModule.h"
35 
36 namespace ola {
37 
38 
39 /*
40  * This is the main OLA HTTP Server
41  */
43  public:
44  struct OladHTTPServerOptions: public
46  public:
47  bool enable_quit;
48 
51  enable_quit(true) {
52  }
53  };
54 
55  OladHTTPServer(ExportMap *export_map,
56  const OladHTTPServerOptions &options,
57  ola::io::ConnectedDescriptor *client_socket,
58  class OlaServer *ola_server,
59  const ola::network::Interface &iface);
60  virtual ~OladHTTPServer();
61 
62  bool Init();
63  void SetPidStore(const ola::rdm::RootPidStore *pid_store);
64 
65  int JsonServerStats(const ola::http::HTTPRequest *request,
66  ola::http::HTTPResponse *response);
68  ola::http::HTTPResponse *response);
69  int JsonPluginInfo(const ola::http::HTTPRequest *request,
70  ola::http::HTTPResponse *response);
71  int JsonUniverseInfo(const ola::http::HTTPRequest *request,
72  ola::http::HTTPResponse *response);
73  int JsonAvailablePorts(const ola::http::HTTPRequest *request,
74  ola::http::HTTPResponse *response);
75  int CreateNewUniverse(const ola::http::HTTPRequest *request,
76  ola::http::HTTPResponse *response);
77  int ModifyUniverse(const ola::http::HTTPRequest *request,
78  ola::http::HTTPResponse *response);
79 
80  int GetDmx(const ola::http::HTTPRequest *request,
81  ola::http::HTTPResponse *response);
82  int HandleSetDmx(const ola::http::HTTPRequest *request,
83  ola::http::HTTPResponse *response);
84  int DisplayQuit(const ola::http::HTTPRequest *request,
85  ola::http::HTTPResponse *response);
86  int ReloadPlugins(const ola::http::HTTPRequest *request,
87  ola::http::HTTPResponse *response);
88  int ReloadPidStore(const ola::http::HTTPRequest *request,
89  ola::http::HTTPResponse *response);
90 
92  const client::Result &result,
93  const std::vector<client::OlaPlugin> &plugins);
94 
97  const client::Result &result,
98  const std::vector<client::OlaUniverse> &universes);
99 
101  int plugin_id,
102  const client::Result &result,
103  const std::string &description);
105  std::string description,
106  const client::Result &result,
107  const ola::client::PluginState &state);
108 
110  const client::Result &result,
111  const client::OlaUniverse &universe);
112 
113  void HandlePortsForUniverse(ola::http::HTTPResponse *response,
114  ola::web::JsonObject *json,
115  unsigned int universe_id,
116  const client::Result &result,
117  const std::vector<client::OlaDevice> &devices);
118 
120  const client::Result &result,
121  const std::vector<client::OlaDevice> &devices);
122 
123  void CreateUniverseComplete(ola::http::HTTPResponse *response,
124  unsigned int universe_id,
125  bool included_name,
126  class ActionQueue *action_queue);
127 
128  void SendCreateUniverseResponse(ola::http::HTTPResponse *response,
129  unsigned int universe_id,
130  bool included_name,
131  class ActionQueue *action_queue);
132 
133  void ModifyUniverseComplete(ola::http::HTTPResponse *response,
134  class ActionQueue *action_queue);
135  void SendModifyUniverseResponse(ola::http::HTTPResponse *response,
136  class ActionQueue *action_queue);
137 
142  inline static int ServeHelpRedirect(ola::http::HTTPResponse *response) {
143  return ola::http::HTTPServer::ServeRedirect(response, HELP_REDIRECTION);
144  }
145 
146  static int ServeUsage(ola::http::HTTPResponse *response,
147  const std::string &details);
148 
149  static const char HELP_PARAMETER[];
150 
151  private:
152  class ola::io::ConnectedDescriptor *m_client_socket;
153  ola::client::OlaClient m_client;
154  class OlaServer *m_ola_server;
155  bool m_enable_quit;
156  ola::network::Interface m_interface;
157  RDMHTTPModule m_rdm_module;
158  time_t m_start_time_t;
159 
160  void HandleGetDmx(ola::http::HTTPResponse *response,
161  const client::Result &result,
162  const client::DMXMetadata &metadata,
163  const DmxBuffer &buffer);
164 
165  void HandleBoolResponse(ola::http::HTTPResponse *response,
166  const client::Result &result);
167 
168  void PortToJson(ola::web::JsonObject *object,
169  const client::OlaDevice &device,
170  const client::OlaPort &port,
171  bool is_output);
172 
173  void AddPatchActions(ActionQueue *action_queue,
174  const std::string port_id_string,
175  unsigned int universe,
176  client::PatchAction port_action);
177 
178  void AddPriorityActions(ActionQueue *action_queue,
179  const ola::http::HTTPRequest *request);
180 
181  typedef struct {
182  unsigned int device_alias;
183  unsigned int port;
184  client::PortDirection direction;
185  std::string string_id;
186  } port_identifier;
187 
188  void DecodePortIds(const std::string &port_ids,
189  std::vector<port_identifier> *ports);
190 
191  void RegisterHandler(
192  const std::string &path,
193  int (OladHTTPServer::*method)(const ola::http::HTTPRequest*,
195 
196  static const char HELP_REDIRECTION[];
197  static const char K_BACKEND_DISCONNECTED_ERROR[];
198  static const unsigned int K_UNIVERSE_NAME_LIMIT = 100;
199  static const char K_PRIORITY_VALUE_SUFFIX[];
200  static const char K_PRIORITY_MODE_SUFFIX[];
201 
202  DISALLOW_COPY_AND_ASSIGN(OladHTTPServer);
203 };
204 } // namespace ola
205 #endif // OLAD_OLADHTTPSERVER_H_