Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
SimpleWebServer.h
Go to the documentation of this file.
1
11#ifndef SIMPLEWEBSERVER_H
12#define SIMPLEWEBSERVER_H
13
15#include <atomic>
16#include <functional>
17#include <map>
18#include <mutex>
19#include <string>
20#include <thread>
21#include <vector>
22
24
25
32{
33 public:
34 using RequestCallback = std::function<std::vector<char>(const std::string&)>;
35
37 // Declare class methods.
39
40 SimpleWebServer(int nPort = 8080);
42 void SetHtmlContent(const std::string& szHtml);
43 void RegisterEndpoint(const std::string& szEndpoint, RequestCallback fnCallback);
44 void AddStaticDirectory(const std::string& szUrlPrefix, const std::string& szLocalDir);
45
46 private:
48 // Private Members
50
51 int m_nPort;
52 std::atomic<int> m_nSocketFD;
53 std::atomic<bool> m_bRunning;
54 std::string m_szHtmlContent;
55 std::map<std::string, RequestCallback> m_mGetCallbacks;
56 std::mutex m_muDataMutex;
57
58
65 struct StaticDir
66 {
67 public:
68 std::string szLocalPath;
69 };
70
71 std::map<std::string, StaticDir> m_mStaticDirectories;
72
73 // Thread Management.
74 std::thread m_thAcceptThread;
75 std::vector<std::thread> m_vWorkerThreads;
76 std::mutex m_muThreadMutex;
77
79 // Private Methods
81
82 void StartServer();
83 void StopServer();
84 void AcceptLoop();
85 void HandleClient(int nClientFD);
86
87 // File Utilities.
88 std::vector<char> LoadFile(const std::string& szPath);
89 std::string GetMimeType(const std::string& szPath);
90};
91
92#endif
A lightweight, multi-threaded HTTP server.
Definition SimpleWebServer.h:32
void RegisterEndpoint(const std::string &szEndpoint, RequestCallback fnCallback)
Registers a GET endpoint with a callback function.
Definition SimpleWebServer.cpp:81
void StopServer()
Stops the web server.
Definition SimpleWebServer.cpp:253
std::vector< char > LoadFile(const std::string &szPath)
Loads a file from disk into a byte vector.
Definition SimpleWebServer.cpp:119
void StartServer()
Starts the web server.
Definition SimpleWebServer.cpp:193
void HandleClient(int nClientFD)
Handles an individual client connection.
Definition SimpleWebServer.cpp:328
void AcceptLoop()
Main loop to accept incoming connections.
Definition SimpleWebServer.cpp:291
std::string GetMimeType(const std::string &szPath)
Determines the MIME type based on the file extension.
Definition SimpleWebServer.cpp:165
~SimpleWebServer()
Destroy the Simple Web Server object.
Definition SimpleWebServer.cpp:52
void AddStaticDirectory(const std::string &szUrlPrefix, const std::string &szLocalDir)
Adds a static directory to serve files from.
Definition SimpleWebServer.cpp:96
void SetHtmlContent(const std::string &szHtml)
Mutator for the Html Content private member.
Definition SimpleWebServer.cpp:66
Represents a local path as an object.
Definition SimpleWebServer.h:66