/** REST-API-releated functions. * * @file * @author Steffen Vogel * @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC * @license Apache 2.0 *********************************************************************************/ #pragma once #include #include #include #include #include #include #include #include #include #include namespace villas { namespace node { namespace api { const int version = 2; /* Forward declarations */ class Session; class Response; class Request; class Error : public RuntimeError { public: template Error(int c = HTTP_STATUS_INTERNAL_SERVER_ERROR, const std::string &msg = "Invalid API request", Args&&... args) : RuntimeError(msg, std::forward(args)...), code(c), json(nullptr) { } template Error(int c = HTTP_STATUS_INTERNAL_SERVER_ERROR, const std::string &msg = "Invalid API request", const char *fmt = nullptr, Args&&... args) : RuntimeError(msg), code(c), json(fmt ? json_pack(fmt, std::forward(args)...) : nullptr ) { } int code; json_t *json; }; class BadRequest : public Error { public: template BadRequest(const std::string &msg = "Bad API request", Args&&... args) : Error(HTTP_STATUS_BAD_REQUEST, msg, std::forward(args)...) { } }; class InvalidMethod : public BadRequest { public: InvalidMethod(Request *req); }; } /* namespace api */ /* Forward declarations */ class SuperNode; class Api { protected: Logger logger; enum State state; std::thread thread; std::atomic running; /**< Atomic flag for signalizing thread termination. */ SuperNode *super_node; void run(); void worker(); public: /** Initialize the API. * * Save references to list of paths / nodes for command execution. */ Api(SuperNode *sn); ~Api(); void start(); void stop(); SuperNode * getSuperNode() { return super_node; } std::list sessions; /**< List of currently active connections */ villas::QueueSignalled pending; /**< A queue of api_sessions which have pending requests. */ }; } /* namespace node */ } /* namespace villas */