2025-01-14 14:42:39 +00:00
|
|
|
/* REST-API-releated functions.
|
2018-10-20 14:20:06 +02:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-10-20 14:20:06 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <list>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <thread>
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2020-03-04 12:41:55 +01:00
|
|
|
#include <villas/common.hpp>
|
2020-08-17 17:03:54 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/queue_signalled.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
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Forward declarations
|
2018-10-20 14:20:06 +02:00
|
|
|
class Session;
|
2020-08-17 17:03:54 +02:00
|
|
|
class Response;
|
|
|
|
class Request;
|
|
|
|
|
|
|
|
class Error : public RuntimeError {
|
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00: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)
|
|
|
|
: RuntimeError(msg), code(c),
|
|
|
|
json(fmt ? json_pack(fmt, std::forward<Args>(args)...) : nullptr) {}
|
|
|
|
|
|
|
|
int code;
|
|
|
|
json_t *json;
|
2020-08-17 17:03:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class BadRequest : public Error {
|
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00: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:
|
2025-01-14 14:42:39 +00:00
|
|
|
InvalidMethod(Request *req);
|
2020-08-17 17:03:54 +02:00
|
|
|
};
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
} // namespace api
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Forward declarations
|
2018-10-20 14:20:06 +02:00
|
|
|
class SuperNode;
|
|
|
|
|
|
|
|
class Api {
|
|
|
|
|
|
|
|
protected:
|
2025-01-14 14:42:39 +00:00
|
|
|
Logger logger;
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
enum State state;
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::thread thread;
|
|
|
|
std::atomic<bool> running; // Atomic flag for signalizing thread termination.
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
SuperNode *super_node;
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void run();
|
|
|
|
void worker();
|
2018-10-20 14:20:06 +02:00
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00:00
|
|
|
/* 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<api::Session *> sessions; // List of currently active connections
|
|
|
|
villas::QueueSignalled<api::Session *>
|
|
|
|
pending; // A queue of api_sessions which have pending requests.
|
2018-10-20 14:20:06 +02:00
|
|
|
};
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|