From d78a1db3fb71aa435f97da74d44c0b10738f59fe Mon Sep 17 00:00:00 2001 From: Clovis Durand Date: Wed, 8 Apr 2020 17:01:39 +0200 Subject: [PATCH] [#28] Added REST method callbacks to the RESTServer class Signed-off-by: Clovis Durand --- generator/src/rest/inc/RESTServer.hpp | 35 +++++++++++++++++++++++++++ generator/src/rest/src/RESTServer.cpp | 35 ++++++++++++++++++++++++++- generator/src/rest/src/main.cpp | 12 +++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/generator/src/rest/inc/RESTServer.hpp b/generator/src/rest/inc/RESTServer.hpp index 36a72db..6744553 100644 --- a/generator/src/rest/inc/RESTServer.hpp +++ b/generator/src/rest/inc/RESTServer.hpp @@ -10,6 +10,7 @@ /* Includes -------------------------------------------- */ /* C++ system */ #include +#include #include /* Defines --------------------------------------------- */ @@ -29,10 +30,27 @@ class RESTServerException : public std::exception { /* RESTServer class ------------------------------------ */ class RESTServer { public: + /* Types */ + /** + * @brief Callback type for the REST API methods. + * + * @details A method callback provides a way to build the response + * to a certain method. + * + * @param[in] pMsg The request. + * @param[out] pOut The output string : the response. + * + * @return Returns true if successful, false elsewise + */ + using methodFct_t = std::function; + /* Contructors */ RESTServer(const std::string &pAddr, const std::string pPort, const std::string &pPath); /* Destructor */ + /** + * @brief The RESTServer class destructor + */ virtual ~RESTServer(); /* Getters */ @@ -40,6 +58,17 @@ class RESTServer { std::string port(void) const; std::string apiPath(void) const; + methodFct_t getCallback(void) const; + methodFct_t postCallback(void) const; + methodFct_t putCallback(void) const; + methodFct_t delCallback(void) const; + + /* Setters */ + void setGetCallback(const methodFct_t &pFct); + void setPostCallback(const methodFct_t &pFct); + void setPutCallback(const methodFct_t &pFct); + void setDelCallback(const methodFct_t &pFct); + /* Server management */ bool open(void); bool listen(void); @@ -60,6 +89,12 @@ class RESTServer { std::string mAddr; std::string mPort; std::string mPath; + + /* REST API callbacks */ + methodFct_t mGetCallback; /**< REST API GET method callback */ + methodFct_t mPostCallback; /**< REST API POST method callback */ + methodFct_t mPutCallback; /**< REST API PUT method callback */ + methodFct_t mDelCallback; /**< REST API DEL method callback */ }; #endif /* RESTSERVER_HPP */ diff --git a/generator/src/rest/src/RESTServer.cpp b/generator/src/rest/src/RESTServer.cpp index b48501e..f4b172e 100644 --- a/generator/src/rest/src/RESTServer.cpp +++ b/generator/src/rest/src/RESTServer.cpp @@ -60,6 +60,39 @@ std::string RESTServer::apiPath(void) const { return mPath; } +RESTServer::methodFct_t RESTServer::getCallback(void) const { + return mGetCallback; +} + +RESTServer::methodFct_t RESTServer::postCallback(void) const { + return mPostCallback; +} + +RESTServer::methodFct_t RESTServer::putCallback(void) const { + return mPutCallback; +} + +RESTServer::methodFct_t RESTServer::delCallback(void) const { + return mDelCallback; +} + +/* Setters */ +void RESTServer::setGetCallback(const methodFct_t &pFct) { + mGetCallback = pFct; +} + +void RESTServer::setPostCallback(const methodFct_t &pFct) { + mPostCallback = pFct; +} + +void RESTServer::setPutCallback(const methodFct_t &pFct) { + mPutCallback = pFct; +} + +void RESTServer::setDelCallback(const methodFct_t &pFct) { + mDelCallback = pFct; +} + /* Server management */ bool RESTServer::open(void) { /* Create a socket */ @@ -229,7 +262,7 @@ bool RESTServer::processClientMessage(const char * const pMsg, const size_t &pRe /* Build response */ lResponse = std::string(htmlResponseCode200) + "\r\n"; - lResponse += "{\"ACK\": true}\r\n"; + getCallback()("{\"ACK\": true}\r\n", lResponse); lSentBytes = send(pClientSocket, lResponse.c_str(), std::strlen(lResponse.c_str()), 0); if(0 > lSentBytes) { diff --git a/generator/src/rest/src/main.cpp b/generator/src/rest/src/main.cpp index 1f25a87..ca328ab 100644 --- a/generator/src/rest/src/main.cpp +++ b/generator/src/rest/src/main.cpp @@ -31,6 +31,15 @@ static void printUsage(const char * const pProgName) std::cout << " : Localhost port number" << std::endl; } +/* REST Test callbacks */ +static RESTServer::methodFct_t sGetCallback = [](const std::string &pMsg, std::string &pOut) -> bool { + (void)pMsg; + + pOut += "{\"ACK\": true}\r\n"; + + return true; +}; + /* ----------------------------------------------------- */ /* Main tests ------------------------------------------ */ /* ----------------------------------------------------- */ @@ -47,6 +56,9 @@ int main(const int argc, const char * const * const argv) { /* Create server */ gRESTServer = std::unique_ptr(new RESTServer(lAddr, lPort, lPath)); + /* Set REST API callbacks */ + gRESTServer->setGetCallback(sGetCallback); + /* Open server */ if(!gRESTServer->open()) { return EXIT_FAILURE;