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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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

2018-10-20 14:20:06 +02:00
/* REST-API-releated functions.
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* 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>
#include <thread>
2018-10-20 14:20:06 +02:00
2020-08-17 17:03:54 +02:00
#include <libwebsockets.h>
2020-03-04 12:41:55 +01:00
#include <villas/common.hpp>
2020-08-17 17:03:54 +02:00
#include <villas/exceptions.hpp>
#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
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:
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:
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);
2020-08-17 17:03:54 +02:00
};
2018-10-20 14:20:06 +02:00
} // namespace api
2018-10-20 14:20:06 +02:00
// Forward declarations
class SuperNode;
class Api {
protected:
Logger logger;
2018-10-20 14:20:06 +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.
2018-10-20 14:20:06 +02:00
SuperNode *super_node;
2018-10-20 14:20:06 +02:00
void run();
void worker();
2018-10-20 14:20:06 +02:00
public:
/* Initialize the API.
*
* Save references to list of paths / nodes for command execution.
*/
Api(SuperNode *sn);
~Api();
2018-10-20 14:20:06 +02:00
void start();
void stop();
2018-10-20 14:20:06 +02:00
SuperNode *getSuperNode() { return super_node; }
2018-10-20 14:20:06 +02:00
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
};
} // namespace node
} // namespace villas