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/lib/api.cpp

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

81 lines
1.9 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
*/
#include <villas/api.hpp>
2020-08-17 17:03:54 +02:00
#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>
2018-10-20 14:20:06 +02:00
using namespace villas;
using namespace villas::node;
using namespace villas::node::api;
InvalidMethod::InvalidMethod(Request *req)
: BadRequest("The '{}' API endpoint does not support {} requests",
req->factory->getName(),
Session::methodToString(req->method)) {}
Api::Api(SuperNode *sn)
: logger(logging.get("api")), state(State::INITIALIZED), super_node(sn) {}
Api::~Api() {
if (state == State::STARTED)
stop();
2018-10-20 14:20:06 +02:00
}
void Api::start() {
assert(state != State::STARTED);
2018-10-20 14:20:06 +02:00
logger->info("Starting sub-system");
2018-10-20 14:20:06 +02:00
running = true;
thread = std::thread(&Api::worker, this);
2018-10-20 14:20:06 +02:00
state = State::STARTED;
2018-10-20 14:20:06 +02:00
}
void Api::stop() {
assert(state == State::STARTED);
2018-10-20 14:20:06 +02:00
logger->info("Stopping sub-system");
2018-10-20 14:20:06 +02:00
for (Session *s : sessions)
s->shutdown();
2018-10-20 14:20:06 +02:00
for (int i = 0; i < 2 && sessions.size() > 0; i++) {
logger->info("Waiting for {} sessions to terminate", sessions.size());
usleep(1 * 1e6);
}
2018-10-20 14:20:06 +02:00
running = false;
pending.push(nullptr); // unblock thread
thread.join();
2018-10-20 14:20:06 +02:00
state = State::STOPPED;
2018-10-20 14:20:06 +02:00
}
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");
2018-10-20 14:20:06 +02:00
}