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

104 lines
2.5 KiB
C++
Raw Permalink Normal View History

2018-10-20 14:20:06 +02:00
/** REST-API-releated functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2018-10-20 14:20:06 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#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>
2018-10-20 14:20:06 +02:00
#include <villas/node/config.h>
#include <villas/memory.h>
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
}