-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathapi.hpp
More file actions
94 lines (69 loc) · 2.02 KB
/
api.hpp
File metadata and controls
94 lines (69 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* REST-API-releated functions.
*
* 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
*/
#pragma once
#include <atomic>
#include <list>
#include <thread>
#include <jansson.h>
#include <libwebsockets.h>
#include <villas/common.hpp>
#include <villas/exceptions.hpp>
#include <villas/log.hpp>
#include <villas/queue_signalled.hpp>
namespace villas {
namespace node {
namespace api {
const int version = 2;
// Forward declarations
class Session;
class Response;
class Request;
class Error : public RuntimeError {
public:
template <typename... Args>
Error(int c = HTTP_STATUS_INTERNAL_SERVER_ERROR, json_t *json = nullptr,
fmt::format_string<Args...> what = "Invalid API request",
Args &&...args)
: RuntimeError(what, std::forward<Args>(args)...), code(c), json(json) {}
template <typename... Args>
static Error badRequest(json_t *json = nullptr,
fmt::format_string<Args...> what = "Bad API request",
Args &&...args) {
return {HTTP_STATUS_BAD_REQUEST, json, what, std::forward<Args>(args)...};
}
static Error invalidMethod(Request *req);
int code;
json_t *json;
};
} // namespace api
// Forward declarations
class SuperNode;
class Api {
protected:
Logger logger;
enum State state;
std::thread thread;
std::atomic<bool> 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<api::Session *> sessions; // List of currently active connections
villas::QueueSignalled<api::Session *>
pending; // A queue of api_sessions which have pending requests.
};
} // namespace node
} // namespace villas