| skipped 11 lines |
12 | 12 | | #include "libwebsockets.h" |
13 | 13 | | #include "libwebsockets/lws-service.h" |
14 | 14 | | |
| 15 | + | /** |
| 16 | + | * @class WebServerBase |
| 17 | + | * @brief Base class for web server implementation |
| 18 | + | */ |
15 | 19 | | class WebServerBase { |
16 | 20 | | public: |
| 21 | + | /** |
| 22 | + | * @brief Constructor for WebServerBase |
| 23 | + | * @param port The port on which the server will listen |
| 24 | + | * @param webDir The directory from which to serve web content |
| 25 | + | */ |
17 | 26 | | WebServerBase(int port = 8081, const std::string& webDir = "."); |
| 27 | + | |
| 28 | + | /** |
| 29 | + | * @brief Destructor |
| 30 | + | */ |
18 | 31 | | virtual ~WebServerBase(); |
19 | 32 | | |
| 33 | + | /** |
| 34 | + | * @class Session |
| 35 | + | * @brief Represents a session with a client |
| 36 | + | */ |
20 | 37 | | class Session { |
21 | 38 | | friend class WebServerBase; |
22 | 39 | | |
23 | 40 | | public: |
| 41 | + | /** |
| 42 | + | * @brief Constructor for Session |
| 43 | + | */ |
24 | 44 | | Session(); |
| 45 | + | |
| 46 | + | /** |
| 47 | + | * @brief Destructor |
| 48 | + | */ |
25 | 49 | | virtual ~Session(); |
| 50 | + | |
| 51 | + | /** |
| 52 | + | * @brief Returns the ID of the session |
| 53 | + | */ |
26 | 54 | | virtual int getId() const { return id; } |
| 55 | + | |
| 56 | + | /** |
| 57 | + | * @brief Receives a message from the client |
| 58 | + | * @param msg The message received from the client |
| 59 | + | */ |
27 | 60 | | virtual void receiveMessage(const std::string& msg) {} |
| 61 | + | |
| 62 | + | /** |
| 63 | + | * @brief Sends a message to the client |
| 64 | + | * @param msg The message to send to the client |
| 65 | + | */ |
28 | 66 | | virtual void sendMessage(const std::string& msg); |
| 67 | + | |
| 68 | + | /** |
| 69 | + | * @brief Performs any necessary updates for the session |
| 70 | + | */ |
29 | 71 | | virtual void update() {} |
| 72 | + | |
| 73 | + | /** |
| 74 | + | * @brief Function called when the session is ready to write |
| 75 | + | */ |
30 | 76 | | virtual void onWrite(); |
31 | 77 | | |
32 | 78 | | private: |
| skipped 1 lines |
34 | 80 | | int id; |
35 | 81 | | }; |
36 | 82 | | |
| 83 | + | /** |
| 84 | + | * @brief Services the server, processing incoming requests |
| 85 | + | * @param time Time for which to service the server |
| 86 | + | */ |
37 | 87 | | void service(int time = 10); |
38 | 88 | | |
| 89 | + | /** |
| 90 | + | * @brief Creates a session for a new client connection |
| 91 | + | * @param info Pointer to information about the session |
| 92 | + | */ |
39 | 93 | | virtual void createSession(void* info); |
40 | 94 | | |
41 | 95 | | protected: |
| 96 | + | /** |
| 97 | + | * @brief Factory method to create a new session |
| 98 | + | */ |
42 | 99 | | virtual Session* createSession() { return new Session(); } |
43 | 100 | | |
44 | 101 | | public: |
| skipped 3 lines |
48 | 105 | | std::string webDir; |
49 | 106 | | }; |
50 | 107 | | |
| 108 | + | /** |
| 109 | + | * @class WebServer |
| 110 | + | * @brief Templated class for a basic web server that inherits from WebServerBase |
| 111 | + | */ |
51 | 112 | | template <typename T> |
52 | 113 | | class WebServer : public WebServerBase { |
53 | 114 | | public: |
| 115 | + | /** |
| 116 | + | * @brief Constructor for WebServer that calls WebServerBase's constructor |
| 117 | + | * @param port The port on which the server will listen |
| 118 | + | * @param webDir The directory from which to serve web content |
| 119 | + | */ |
54 | 120 | | WebServer(int port = 8081, const std::string& webDir = ".") |
55 | 121 | | : WebServerBase(port, webDir) {} |
56 | 122 | | |
57 | 123 | | protected: |
| 124 | + | /** |
| 125 | + | * @brief Factory method to create a new session of type T |
| 126 | + | */ |
58 | 127 | | Session* createSession() { return new T(); } |
59 | 128 | | }; |
60 | 129 | | |
| 130 | + | /** |
| 131 | + | * @class WebServerWithState |
| 132 | + | * @brief Templated class for a web server with state that inherits from WebServerBase |
| 133 | + | */ |
61 | 134 | | template <typename T, typename STATE> |
62 | 135 | | class WebServerWithState : public WebServerBase { |
63 | 136 | | public: |
| 137 | + | /** |
| 138 | + | * @brief Constructor for WebServerWithState that calls WebServerBase's constructor |
| 139 | + | * @param state The initial state of the server |
| 140 | + | * @param port The port on which the server will listen |
| 141 | + | * @param webDir The directory from which to serve web content |
| 142 | + | */ |
64 | 143 | | WebServerWithState(STATE state, int port = 8081, |
65 | 144 | | const std::string& webDir = ".") |
66 | 145 | | : WebServerBase(port, webDir), state(state) {} |
67 | 146 | | |
68 | 147 | | protected: |
| 148 | + | /** |
| 149 | + | * @brief Factory method to create a new session of type T with the given state |
| 150 | + | */ |
69 | 151 | | Session* createSession() { return new T(state); } |
70 | 152 | | |
71 | 153 | | private: |
| skipped 3 lines |
75 | 157 | | // JSON support |
76 | 158 | | #include "picojson.h" |
77 | 159 | | |
| 160 | + | /** |
| 161 | + | * @class JSONSession |
| 162 | + | * @brief Abstract class for a session supporting JSON communication |
| 163 | + | */ |
78 | 164 | | class JSONSession : public WebServerBase::Session { |
79 | 165 | | public: |
| 166 | + | /** |
| 167 | + | * @brief Receives a JSON message from the client |
| 168 | + | * @param val The JSON value received from the client |
| 169 | + | */ |
80 | 170 | | virtual void receiveJSON(picojson::value& val) {} |
81 | 171 | | |
| 172 | + | /** |
| 173 | + | * @brief Sends a JSON message to the client |
| 174 | + | * @param val The JSON value to send to the client |
| 175 | + | */ |
82 | 176 | | virtual void sendJSON(picojson::value& val) { sendMessage(val.serialize()); } |
83 | 177 | | |
| 178 | + | /** |
| 179 | + | * @brief Receives a message from the client and parses it as JSON |
| 180 | + | * @param msg The message received from the client |
| 181 | + | */ |
84 | 182 | | void receiveMessage(const std::string& msg) { |
85 | 183 | | static std::string buf = ""; |
86 | 184 | | picojson::value val; |
| skipped 14 lines |
101 | 199 | | |
102 | 200 | | #include "util/json.h" |
103 | 201 | | |
| 202 | + | /** |
| 203 | + | * @class JsonSession |
| 204 | + | * @brief Abstract class for a session supporting JSON communication with command handling, inherits from JSONSession |
| 205 | + | */ |
104 | 206 | | class JsonSession : public JSONSession { |
105 | 207 | | public: |
106 | 208 | | /** |
| skipped 29 lines |