CSCI3081W Drone Delivery System
WebServer.h
1 
5 #ifndef WEBSERVER_H_
6 #define WEBSERVER_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "libwebsockets.h"
13 #include "libwebsockets/lws-service.h"
14 
20  public:
26  WebServerBase(int port = 8081, const std::string& webDir = ".");
27 
31  virtual ~WebServerBase();
32 
37  class Session {
38  friend class WebServerBase;
39 
40  public:
45 
49  virtual ~Session();
50 
54  virtual int getId() const { return id; }
55 
60  virtual void receiveMessage(const std::string& msg) {}
61 
66  virtual void sendMessage(const std::string& msg);
67 
71  virtual void update() {}
72 
76  virtual void onWrite();
77 
78  private:
79  void* state;
80  int id;
81  };
82 
87  void service(int time = 10);
88 
93  virtual void createSession(void* info);
94 
95  protected:
99  virtual Session* createSession() { return new Session(); }
100 
101  public:
102  lws_context* context = nullptr;
103  std::vector<Session*> sessions;
104  std::map<int, Session*> sessionMap;
105  std::string webDir;
106 };
107 
112 template <typename T>
113 class WebServer : public WebServerBase {
114  public:
120  WebServer(int port = 8081, const std::string& webDir = ".")
121  : WebServerBase(port, webDir) {}
122 
123  protected:
127  Session* createSession() { return new T(); }
128 };
129 
134 template <typename T, typename STATE>
136  public:
143  WebServerWithState(STATE state, int port = 8081,
144  const std::string& webDir = ".")
145  : WebServerBase(port, webDir), state(state) {}
146 
147  protected:
151  Session* createSession() { return new T(state); }
152 
153  private:
154  STATE state;
155 };
156 
157 // JSON support
158 #include "picojson.h"
159 
165  public:
170  virtual void receiveJSON(picojson::value& val) {}
171 
176  virtual void sendJSON(picojson::value& val) { sendMessage(val.serialize()); }
177 
182  void receiveMessage(const std::string& msg) {
183  static std::string buf = "";
184  picojson::value val;
185  std::string err = picojson::parse(val, msg);
186  if (err.empty() && val.is<picojson::object>()) {
187  buf = "";
188  receiveJSON(val);
189  } else {
190  buf += msg;
191  err = picojson::parse(val, buf);
192  if (err.empty() && val.is<picojson::object>()) {
193  buf = "";
194  receiveJSON(val);
195  }
196  }
197  }
198 };
199 
200 #include "util/json.h"
201 
206 class JsonSession : public JSONSession {
207  public:
212  void receiveJSON(picojson::value& val) {
213  JsonObject data = JsonValue(val);
214 
215  std::string cmd = data["command"];
216 
217  JsonObject returnValue;
218  returnValue["id"] = data["id"];
219 
220  receiveCommand(cmd, data, returnValue);
221  JsonValue retVal(returnValue);
222  sendJSON(retVal.getValue());
223  }
224 
232  virtual void receiveCommand(const std::string& cmd, const JsonObject& data,
233  JsonObject& returnValue) = 0;
234 };
235 
236 #endif // WEBSERVER_H_
Abstract class for a session supporting JSON communication.
Definition: WebServer.h:164
void receiveMessage(const std::string &msg)
Receives a message from the client and parses it as JSON.
Definition: WebServer.h:182
virtual void receiveJSON(picojson::value &val)
Receives a JSON message from the client.
Definition: WebServer.h:170
virtual void sendJSON(picojson::value &val)
Sends a JSON message to the client.
Definition: WebServer.h:176
Manages a picojson::object, works with JsonValue to provide implicit casting.
Abstract class for a session supporting JSON communication with command handling, inherits from JSONS...
Definition: WebServer.h:206
void receiveJSON(picojson::value &val)
Receive a command from the web server.
Definition: WebServer.h:212
virtual void receiveCommand(const std::string &cmd, const JsonObject &data, JsonObject &returnValue)=0
Handles specific commands from the web server.
Manages a picojson::value, provides implicit casting for valid JSON types.
Definition: json.h:20
picojson::value & getValue()
Definition: json.h:118
Represents a session with a client.
Definition: WebServer.h:37
virtual int getId() const
Returns the ID of the session.
Definition: WebServer.h:54
Session()
Constructor for Session.
virtual ~Session()
Destructor.
virtual void sendMessage(const std::string &msg)
Sends a message to the client.
virtual void receiveMessage(const std::string &msg)
Receives a message from the client.
Definition: WebServer.h:60
virtual void onWrite()
Function called when the session is ready to write.
virtual void update()
Performs any necessary updates for the session.
Definition: WebServer.h:71
Base class for web server implementation.
Definition: WebServer.h:19
virtual ~WebServerBase()
Destructor.
WebServerBase(int port=8081, const std::string &webDir=".")
Constructor for WebServerBase.
virtual void createSession(void *info)
Creates a session for a new client connection.
void service(int time=10)
Services the server, processing incoming requests.
virtual Session * createSession()
Factory method to create a new session.
Definition: WebServer.h:99
Templated class for a web server with state that inherits from WebServerBase.
Definition: WebServer.h:135
WebServerWithState(STATE state, int port=8081, const std::string &webDir=".")
Constructor for WebServerWithState that calls WebServerBase's constructor.
Definition: WebServer.h:143
Session * createSession()
Factory method to create a new session of type T with the given state.
Definition: WebServer.h:151
Templated class for a basic web server that inherits from WebServerBase.
Definition: WebServer.h:113
WebServer(int port=8081, const std::string &webDir=".")
Constructor for WebServer that calls WebServerBase's constructor.
Definition: WebServer.h:120
Session * createSession()
Factory method to create a new session of type T.
Definition: WebServer.h:127