Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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_
Definition: OlaHTTPServer.h:38
int JsonAvailablePorts(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Return a list of unbound ports.
Definition: OladHTTPServer.cpp:385
int CreateNewUniverse(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Create a new universe by binding one or more ports.
Definition: OladHTTPServer.cpp:420
int JsonPluginInfo(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Print the plugin info as a JSON string.
Definition: OladHTTPServer.cpp:332
PortDirection
The port direction.
Definition: ClientArgs.h:54
void HandlePluginInfo(ola::http::HTTPResponse *response, std::string description, const client::Result &result, const ola::client::PluginState &state)
Handle the plugin description response.
Definition: OladHTTPServer.cpp:767
int JsonUniversePluginList(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Print the list of universes / plugins as a json string.
Definition: OladHTTPServer.cpp:316
Represents a device.
Definition: ClientTypes.h:219
A container for the exported variables.
Definition: ExportMap.h:324
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
int DisplayQuit(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Cause the server to shutdown.
Definition: OladHTTPServer.cpp:619
Definition: HTTPServer.h:54
void SetPidStore(const ola::rdm::RootPidStore *pid_store)
Can be called while the HTTP server is running.
Definition: OladHTTPServer.cpp:266
OladHTTPServer(ExportMap *export_map, const OladHTTPServerOptions &options, ola::io::ConnectedDescriptor *client_socket, class OlaServer *ola_server, const ola::network::Interface &iface)
Create a new OLA HTTP server.
Definition: OladHTTPServer.cpp:77
Definition: HTTPServer.h:149
Definition: OladHTTPServer.h:44
int JsonUniverseInfo(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Return information about a universe.
Definition: OladHTTPServer.cpp:358
Holds information about RDM PIDs.
void HandlePartialPluginInfo(ola::http::HTTPResponse *response, int plugin_id, const client::Result &result, const std::string &description)
Handle the plugin description response.
Definition: OladHTTPServer.cpp:745
Used to hold a single universe of DMX data.
Definition: DmxBuffer.h:49
int SetPluginState(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Set plugin state.
Definition: OladHTTPServer.cpp:525
Export variables on the http server.
static int ServeHelpRedirect(ola::http::HTTPResponse *response)
Definition: OladHTTPServer.h:144
PatchAction
The patch action, used with OlaClient::Patch()
Definition: ClientArgs.h:38
The callback based C++ client for OLA.
Definition: OlaClient.h:45
int ReloadPidStore(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Reload the PID Store.
Definition: OladHTTPServer.cpp:657
bool Init()
Setup the OLA HTTP server.
Definition: OladHTTPServer.cpp:245
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition: Json.h:890
int HandleSetDmx(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Handle the set DMX command.
Definition: OladHTTPServer.cpp:587
Definition: OladHTTPServer.h:42
The state of a plugin. This information can be used to detect conflicts between plugins.
Definition: ClientTypes.h:93
Definition: HTTPServer.h:98
The main OlaServer class.
Definition: OlaServer.h:61
Definition: Interface.h:35
Indicates the result of a OLA API call.
Definition: Result.h:52
Represents a universe.
Definition: ClientTypes.h:264
static int ServeRedirect(HTTPResponse *response, const std::string &location)
Serve a redirect.
Definition: HTTPServer.cpp:755
void HandleCandidatePorts(ola::http::HTTPResponse *response, const client::Result &result, const std::vector< client::OlaDevice > &devices)
Handle the list of candidate ports.
Definition: OladHTTPServer.cpp:885
Helper macros.
int ModifyUniverse(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Modify an existing universe.
Definition: OladHTTPServer.cpp:466
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Metadata that accompanies DMX packets.
Definition: ClientTypes.h:311
The root of the RDM parameter descriptor store.
Definition: PidStore.h:68
void HandleUniverseInfo(ola::http::HTTPResponse *response, const client::Result &result, const client::OlaUniverse &universe)
Handle the universe info.
Definition: OladHTTPServer.cpp:808
static int ServeUsage(ola::http::HTTPResponse *response, const std::string &details)
Serve usage information.
Definition: OladHTTPServer.cpp:1006
void HandleUniverseList(ola::http::HTTPResponse *response, ola::web::JsonObject *json, const client::Result &result, const std::vector< client::OlaUniverse > &universes)
Handle the universe list callback.
Definition: OladHTTPServer.cpp:712
The base class that represents a port.
Definition: ClientTypes.h:119
int JsonServerStats(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Print the server stats JSON.
Definition: OladHTTPServer.cpp:277
Definition: RDMHTTPModule.h:45
Definition: ActionQueue.h:54
int ReloadPlugins(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Reload all plugins.
Definition: OladHTTPServer.cpp:643
void HandlePluginList(ola::http::HTTPResponse *response, const client::Result &result, const std::vector< client::OlaPlugin > &plugins)
Handle the plugin list callback.
Definition: OladHTTPServer.cpp:675
int GetDmx(const ola::http::HTTPRequest *request, ola::http::HTTPResponse *response)
Handle the get DMX command.
Definition: OladHTTPServer.cpp:563