Open Lighting Architecture
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * OladHTTPServer.h
17  * Interface for the OLA HTTP class
18  * Copyright (C) 2005-2010 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/OlaCallbackClient.h"
29 #include "ola/http/HTTPServer.h"
30 #include "ola/http/OlaHTTPServer.h"
31 #include "ola/network/Interface.h"
32 #include "ola/rdm/PidStore.h"
33 #include "olad/RDMHTTPModule.h"
34 
35 namespace ola {
36 
37 using std::string;
39 
40 
41 /*
42  * This is the main OLA HTTP Server
43  */
45  public:
47  public:
48  bool enable_quit;
49 
52  enable_quit(true) {
53  }
54  };
55 
56  OladHTTPServer(ExportMap *export_map,
57  const OladHTTPServerOptions &options,
58  ola::io::ConnectedDescriptor *client_socket,
59  class OlaServer *ola_server,
60  const ola::network::Interface &interface);
61  virtual ~OladHTTPServer();
62 
63  bool Init();
64  void SetPidStore(const ola::rdm::RootPidStore *pid_store);
65 
66  int JsonServerStats(const HTTPRequest *request, HTTPResponse *response);
67  int JsonUniversePluginList(const HTTPRequest *request,
68  HTTPResponse *response);
69  int JsonPluginInfo(const HTTPRequest *request, HTTPResponse *response);
70  int JsonUniverseInfo(const HTTPRequest *request, HTTPResponse *response);
71  int JsonAvailablePorts(const HTTPRequest *request, HTTPResponse *response);
72  int CreateNewUniverse(const HTTPRequest *request, HTTPResponse *response);
73  int ModifyUniverse(const HTTPRequest *request, HTTPResponse *response);
74 
75  int GetDmx(const HTTPRequest *request, HTTPResponse *response);
76  int HandleSetDmx(const HTTPRequest *request, HTTPResponse *response);
77  int DisplayQuit(const HTTPRequest *request, HTTPResponse *response);
78  int ReloadPlugins(const HTTPRequest *request, HTTPResponse *response);
79  int ReloadPidStore(const HTTPRequest *request, HTTPResponse *response);
80 
81  void HandlePluginList(HTTPResponse *response,
82  const vector<class OlaPlugin> &plugins,
83  const string &error);
84 
85  void HandleUniverseList(HTTPResponse *response,
87  const vector<class OlaUniverse> &universes,
88  const string &error);
89 
90  void HandlePartialPluginInfo(HTTPResponse *response,
91  int plugin_id,
92  const string &description,
93  const string &error);
94  void HandlePluginInfo(HTTPResponse *response,
95  string description,
96  const OlaCallbackClient::PluginState &state,
97  const string &error);
98 
99  void HandleUniverseInfo(HTTPResponse *response,
100  class OlaUniverse &universe,
101  const string &error);
102 
103  void HandlePortsForUniverse(HTTPResponse *response,
104  ola::web::JsonObject *json,
105  unsigned int universe_id,
106  const vector<class OlaDevice> &devices,
107  const string &error);
108 
109  void HandleCandidatePorts(HTTPResponse *response,
110  const vector<class OlaDevice> &devices,
111  const string &error);
112 
113  void CreateUniverseComplete(HTTPResponse *response,
114  unsigned int universe_id,
115  bool included_name,
116  class ActionQueue *action_queue);
117 
118  void SendCreateUniverseResponse(HTTPResponse *response,
119  unsigned int universe_id,
120  bool included_name,
121  class ActionQueue *action_queue);
122 
123  void ModifyUniverseComplete(HTTPResponse *response,
124  class ActionQueue *action_queue);
125  void SendModifyUniverseResponse(HTTPResponse *response,
126  class ActionQueue *action_queue);
127 
128  /*
129  * Serve a help redirect
130  * @param response the response to use
131  */
132  inline static int ServeHelpRedirect(HTTPResponse *response) {
133  return HTTPServer::ServeRedirect(response, HELP_REDIRECTION);
134  }
135 
136  static int ServeUsage(HTTPResponse *response, const string &details);
137 
138  static const char HELP_PARAMETER[];
139 
140  private:
141  class ola::io::ConnectedDescriptor *m_client_socket;
142  ola::OlaCallbackClient m_client;
143  class OlaServer *m_ola_server;
144  bool m_enable_quit;
145  ola::network::Interface m_interface;
146  RDMHTTPModule m_rdm_module;
147  time_t m_start_time_t;
148 
150  OladHTTPServer& operator=(const OladHTTPServer&);
151 
152  void HandleGetDmx(HTTPResponse *response,
153  const DmxBuffer &buffer,
154  const string &error);
155 
156  void HandleBoolResponse(HTTPResponse *response, const string &error);
157 
158  void PortToJson(ola::web::JsonObject *object,
159  const class OlaDevice &device,
160  const class OlaPort &port,
161  bool is_output);
162 
163  void AddPatchActions(ActionQueue *action_queue,
164  const string port_id_string,
165  unsigned int universe,
166  PatchAction port_action);
167 
168  void AddPriorityActions(ActionQueue *action_queue,
169  const HTTPRequest *request);
170 
171  typedef struct {
172  unsigned int device_alias;
173  unsigned int port;
174  PortDirection direction;
175  string string_id;
176  } port_identifier;
177 
178  void DecodePortIds(const string &port_ids, vector<port_identifier> *ports);
179 
180  void RegisterHandler(
181  const string &path,
182  int (OladHTTPServer::*method)(const HTTPRequest*, HTTPResponse*));
183 
184  static const char HELP_REDIRECTION[];
185  static const char K_BACKEND_DISCONNECTED_ERROR[];
186  static const unsigned int K_UNIVERSE_NAME_LIMIT = 100;
187  static const char K_PRIORITY_VALUE_SUFFIX[];
188  static const char K_PRIORITY_MODE_SUFFIX[];
189 };
190 } // namespace ola
191 #endif // OLAD_OLADHTTPSERVER_H_