Open Lighting Architecture  0.9.6
 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 SetPluginState(const ola::http::HTTPRequest *request,
72  ola::http::HTTPResponse *response);
73  int JsonUniverseInfo(const ola::http::HTTPRequest *request,
74  ola::http::HTTPResponse *response);
75  int JsonAvailablePorts(const ola::http::HTTPRequest *request,
76  ola::http::HTTPResponse *response);
77  int CreateNewUniverse(const ola::http::HTTPRequest *request,
78  ola::http::HTTPResponse *response);
79  int ModifyUniverse(const ola::http::HTTPRequest *request,
80  ola::http::HTTPResponse *response);
81 
82  int GetDmx(const ola::http::HTTPRequest *request,
83  ola::http::HTTPResponse *response);
84  int HandleSetDmx(const ola::http::HTTPRequest *request,
85  ola::http::HTTPResponse *response);
86  int DisplayQuit(const ola::http::HTTPRequest *request,
87  ola::http::HTTPResponse *response);
88  int ReloadPlugins(const ola::http::HTTPRequest *request,
89  ola::http::HTTPResponse *response);
90  int ReloadPidStore(const ola::http::HTTPRequest *request,
91  ola::http::HTTPResponse *response);
92 
94  const client::Result &result,
95  const std::vector<client::OlaPlugin> &plugins);
96 
99  const client::Result &result,
100  const std::vector<client::OlaUniverse> &universes);
101 
103  int plugin_id,
104  const client::Result &result,
105  const std::string &description);
107  std::string description,
108  const client::Result &result,
109  const ola::client::PluginState &state);
110 
112  const client::Result &result,
113  const client::OlaUniverse &universe);
114 
115  void HandlePortsForUniverse(ola::http::HTTPResponse *response,
116  ola::web::JsonObject *json,
117  unsigned int universe_id,
118  const client::Result &result,
119  const std::vector<client::OlaDevice> &devices);
120 
122  const client::Result &result,
123  const std::vector<client::OlaDevice> &devices);
124 
125  void CreateUniverseComplete(ola::http::HTTPResponse *response,
126  unsigned int universe_id,
127  bool included_name,
128  class ActionQueue *action_queue);
129 
130  void SendCreateUniverseResponse(ola::http::HTTPResponse *response,
131  unsigned int universe_id,
132  bool included_name,
133  class ActionQueue *action_queue);
134 
135  void ModifyUniverseComplete(ola::http::HTTPResponse *response,
136  class ActionQueue *action_queue);
137  void SendModifyUniverseResponse(ola::http::HTTPResponse *response,
138  class ActionQueue *action_queue);
139 
144  inline static int ServeHelpRedirect(ola::http::HTTPResponse *response) {
145  return ola::http::HTTPServer::ServeRedirect(response, HELP_REDIRECTION);
146  }
147 
148  static int ServeUsage(ola::http::HTTPResponse *response,
149  const std::string &details);
150 
151  static const char HELP_PARAMETER[];
152 
153  private:
154  class ola::io::ConnectedDescriptor *m_client_socket;
155  ola::client::OlaClient m_client;
156  class OlaServer *m_ola_server;
157  bool m_enable_quit;
158  ola::network::Interface m_interface;
159  RDMHTTPModule m_rdm_module;
160  time_t m_start_time_t;
161 
162  void HandleGetDmx(ola::http::HTTPResponse *response,
163  const client::Result &result,
164  const client::DMXMetadata &metadata,
165  const DmxBuffer &buffer);
166 
167  void HandleBoolResponse(ola::http::HTTPResponse *response,
168  const client::Result &result);
169 
170  void PortToJson(ola::web::JsonObject *object,
171  const client::OlaDevice &device,
172  const client::OlaPort &port,
173  bool is_output);
174 
175  void AddPatchActions(ActionQueue *action_queue,
176  const std::string port_id_string,
177  unsigned int universe,
178  client::PatchAction port_action);
179 
180  void AddPriorityActions(ActionQueue *action_queue,
181  const ola::http::HTTPRequest *request);
182 
183  typedef struct {
184  unsigned int device_alias;
185  unsigned int port;
186  client::PortDirection direction;
187  std::string string_id;
188  } port_identifier;
189 
190  void DecodePortIds(const std::string &port_ids,
191  std::vector<port_identifier> *ports);
192 
193  void RegisterHandler(
194  const std::string &path,
195  int (OladHTTPServer::*method)(const ola::http::HTTPRequest*,
197 
198  static const char HELP_REDIRECTION[];
199  static const char K_BACKEND_DISCONNECTED_ERROR[];
200  static const unsigned int K_UNIVERSE_NAME_LIMIT = 100;
201  static const char K_PRIORITY_VALUE_SUFFIX[];
202  static const char K_PRIORITY_MODE_SUFFIX[];
203 
204  DISALLOW_COPY_AND_ASSIGN(OladHTTPServer);
205 };
206 } // namespace ola
207 #endif // OLAD_OLADHTTPSERVER_H_