1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/api.cpp

89 lines
1.8 KiB
C++
Raw Normal View History

2018-10-20 14:20:06 +02:00
/** REST-API-releated functions.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license 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/web.hpp>
2018-10-20 14:20:06 +02:00
#include <villas/api/session.hpp>
2020-08-17 17:03:54 +02:00
#include <villas/api/request.hpp>
#include <villas/utils.hpp>
#include <villas/node/config.hpp>
#include <villas/node/memory.hpp>
2020-03-04 13:07:20 +01:00
#include <villas/compat.hpp>
2018-10-20 14:20:06 +02:00
using namespace villas;
using namespace villas::node;
using namespace villas::node::api;
2020-08-17 17:03:54 +02:00
InvalidMethod::InvalidMethod(Request *req) :
BadRequest("The '{}' API endpoint does not support {} requests",
req->factory->getName(),
Session::methodToString(req->method)
2020-08-17 17:03:54 +02:00
)
{ }
2018-10-20 14:20:06 +02:00
Api::Api(SuperNode *sn) :
logger(logging.get("api")),
2019-06-23 16:13:23 +02:00
state(State::INITIALIZED),
2020-08-17 17:03:54 +02:00
super_node(sn)
{ }
2018-10-20 14:20:06 +02:00
Api::~Api()
{
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2018-10-20 14:20:06 +02:00
}
void Api::start()
{
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2018-10-20 14:20:06 +02:00
logger->info("Starting sub-system");
running = true;
thread = std::thread(&Api::worker, this);
2019-06-23 16:13:23 +02:00
state = State::STARTED;
2018-10-20 14:20:06 +02:00
}
void Api::stop()
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2018-10-20 14:20:06 +02:00
logger->info("Stopping sub-system");
for (Session *s : sessions)
s->shutdown();
for (int i = 0; i < 2 && sessions.size() > 0; i++) {
2018-10-20 14:20:06 +02:00
logger->info("Waiting for {} sessions to terminate", sessions.size());
usleep(1 * 1e6);
}
running = false;
pending.push(nullptr); /* unblock thread */
thread.join();
2019-06-23 16:13:23 +02:00
state = State::STOPPED;
2018-10-20 14:20:06 +02:00
}
void Api::worker()
{
2018-12-02 03:18:09 +01:00
logger->info("Started worker");
2020-10-10 22:46:42 +02:00
/* Process pending requests */
2020-10-15 14:47:43 +02:00
while (running) {
2020-10-10 22:46:42 +02:00
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();
2020-08-17 17:03:54 +02:00
}
2018-10-20 14:20:06 +02:00
}
2018-12-02 03:18:09 +01:00
logger->info("Stopped worker");
2018-10-20 14:20:06 +02:00
}