Skip to content

Commit

Permalink
[#28] Added REST method callbacks to the RESTServer class
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
  • Loading branch information
Clovel committed Apr 8, 2020
1 parent 9fd6373 commit d78a1db
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
35 changes: 35 additions & 0 deletions generator/src/rest/inc/RESTServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/* Includes -------------------------------------------- */
/* C++ system */
#include <exception>
#include <functional>
#include <string>

/* Defines --------------------------------------------- */
Expand All @@ -29,17 +30,45 @@ 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<bool(const std::string &, std::string &)>;

/* Contructors */
RESTServer(const std::string &pAddr, const std::string pPort, const std::string &pPath);

/* Destructor */
/**
* @brief The RESTServer class destructor
*/
virtual ~RESTServer();

/* Getters */
std::string address(void) const;
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);
Expand All @@ -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 */
35 changes: 34 additions & 1 deletion generator/src/rest/src/RESTServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions generator/src/rest/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ static void printUsage(const char * const pProgName)
std::cout << " <arg1> : 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 ------------------------------------------ */
/* ----------------------------------------------------- */
Expand All @@ -47,6 +56,9 @@ int main(const int argc, const char * const * const argv) {
/* Create server */
gRESTServer = std::unique_ptr<RESTServer>(new RESTServer(lAddr, lPort, lPath));

/* Set REST API callbacks */
gRESTServer->setGetCallback(sGetCallback);

/* Open server */
if(!gRESTServer->open()) {
return EXIT_FAILURE;
Expand Down

0 comments on commit d78a1db

Please sign in to comment.