-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapi.cpp
More file actions
81 lines (63 loc) · 1.86 KB
/
Copy pathapi.cpp
File metadata and controls
81 lines (63 loc) · 1.86 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
/* 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
*/
#include <villas/api.hpp>
#include <villas/api/request.hpp>
#include <villas/api/session.hpp>
#include <villas/compat.hpp>
#include <villas/node/config.hpp>
#include <villas/node/memory.hpp>
#include <villas/utils.hpp>
#include <villas/web.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::node::api;
Error Error::invalidMethod(Request *req) {
return badRequest(
nullptr, "The '{}' API endpoint does not support {} requests",
req->factory->getName(), Session::methodToString(req->method));
}
Api::Api(SuperNode *sn)
: logger(Log::get("api")), state(State::INITIALIZED), super_node(sn) {}
Api::~Api() {
if (state == State::STARTED)
stop();
}
void Api::start() {
assert(state != State::STARTED);
logger->info("Starting sub-system");
running = true;
thread = std::thread(&Api::worker, this);
state = State::STARTED;
}
void Api::stop() {
assert(state == State::STARTED);
logger->info("Stopping sub-system");
for (Session *s : sessions)
s->shutdown();
for (int i = 0; i < 2 && sessions.size() > 0; i++) {
logger->info("Waiting for {} sessions to terminate", sessions.size());
usleep(1 * 1e6);
}
running = false;
pending.push(nullptr); // unblock thread
thread.join();
state = State::STOPPED;
}
void Api::worker() {
logger->info("Started worker");
// Process pending requests
while (running) {
Session *s = pending.pop();
if (s) {
// Check that the session is still alive
auto it = std::find(sessions.begin(), sessions.end(), s);
if (it != sessions.end())
s->execute();
}
}
logger->info("Stopped worker");
}