1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/api.hpp

118 lines
2.3 KiB
C++
Raw Permalink Normal View History

2018-10-20 14:20:06 +02:00
/** REST-API-releated functions.
*
* @file
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2018-10-20 14:20:06 +02:00
*********************************************************************************/
#pragma once
#include <jansson.h>
#include <libwebsockets.h>
#include <atomic>
#include <thread>
#include <list>
2020-08-17 17:03:54 +02:00
#include <libwebsockets.h>
2018-10-20 14:20:06 +02:00
#include <villas/log.hpp>
2020-03-04 12:41:55 +01:00
#include <villas/common.hpp>
2018-10-20 14:20:06 +02:00
#include <villas/queue_signalled.hpp>
2020-08-17 17:03:54 +02:00
#include <villas/exceptions.hpp>
2018-10-20 14:20:06 +02:00
namespace villas {
namespace node {
namespace api {
2020-08-17 17:03:54 +02:00
const int version = 2;
2018-11-02 15:00:03 +01:00
2018-10-20 14:20:06 +02:00
/* Forward declarations */
class Session;
2020-08-17 17:03:54 +02:00
class Response;
class Request;
class Error : public RuntimeError {
public:
2020-09-30 16:11:13 +02:00
template<typename... Args>
Error(int c = HTTP_STATUS_INTERNAL_SERVER_ERROR, const std::string &msg = "Invalid API request", Args&&... args) :
RuntimeError(msg, std::forward<Args>(args)...),
code(c),
json(nullptr)
{ }
template<typename... Args>
Error(int c = HTTP_STATUS_INTERNAL_SERVER_ERROR, const std::string &msg = "Invalid API request", const char *fmt = nullptr, Args&&... args) :
2020-08-17 17:03:54 +02:00
RuntimeError(msg),
2020-09-30 16:11:13 +02:00
code(c),
json(fmt
? json_pack(fmt, std::forward<Args>(args)...)
: nullptr
)
2020-08-17 17:03:54 +02:00
{ }
int code;
2020-09-30 16:11:13 +02:00
json_t *json;
2020-08-17 17:03:54 +02:00
};
class BadRequest : public Error {
public:
2020-09-30 16:11:13 +02:00
template<typename... Args>
BadRequest(const std::string &msg = "Bad API request", Args&&... args) :
Error(HTTP_STATUS_BAD_REQUEST, msg, std::forward<Args>(args)...)
2020-08-17 17:03:54 +02:00
{ }
};
class InvalidMethod : public BadRequest {
public:
InvalidMethod(Request *req);
};
2018-10-20 14:20:06 +02:00
2020-06-15 22:16:38 +02:00
} /* namespace api */
2018-10-20 14:20:06 +02:00
/* Forward declarations */
class SuperNode;
class Api {
protected:
2019-01-17 20:28:20 +01:00
Logger logger;
2018-10-20 14:20:06 +02:00
2019-06-23 16:13:23 +02:00
enum State state;
2018-10-20 14:20:06 +02:00
std::thread thread;
std::atomic<bool> running; /**< Atomic flag for signalizing thread termination. */
SuperNode *super_node;
void run();
void worker();
public:
/** Initialize the API.
2018-10-20 14:20:06 +02:00
*
* 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<api::Session *> sessions; /**< List of currently active connections */
villas::QueueSignalled<api::Session *> pending; /**< A queue of api_sessions which have pending requests. */
};
2020-07-08 13:43:15 +02:00
} /* namespace node */
} /* namespace villas */