2017-04-09 16:22:03 +02:00
|
|
|
/** API session.
|
|
|
|
*
|
|
|
|
* @file
|
2022-12-14 17:41:58 +01:00
|
|
|
* @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
|
2017-04-09 16:22:03 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/queue.h>
|
2020-10-20 22:17:55 +02:00
|
|
|
#include <villas/buffer.hpp>
|
2018-10-20 14:20:06 +02:00
|
|
|
#include <villas/api.hpp>
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
2018-06-28 13:42:50 +02:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
/* Forward declarations */
|
|
|
|
class SuperNode;
|
|
|
|
class Api;
|
2020-08-17 17:03:54 +02:00
|
|
|
class Web;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
namespace api {
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
/* Forward declarations */
|
|
|
|
class Request;
|
|
|
|
class Response;
|
2020-10-16 11:43:24 +02:00
|
|
|
class StatusRequest;
|
2021-02-16 14:15:14 +01:00
|
|
|
class RequestFactory;
|
2020-08-17 17:03:54 +02:00
|
|
|
|
|
|
|
/** A connection via HTTP REST or WebSockets to issue API requests. */
|
2018-10-20 14:20:06 +02:00
|
|
|
class Session {
|
|
|
|
|
|
|
|
public:
|
2020-10-20 22:26:31 +02:00
|
|
|
friend Request; /**< Requires access to wsi for getting URL args and headers */
|
|
|
|
friend StatusRequest; /**< Requires access to wsi for context status */
|
2021-02-16 14:15:14 +01:00
|
|
|
friend RequestFactory;
|
2020-10-16 11:43:24 +02:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
enum State {
|
|
|
|
ESTABLISHED,
|
|
|
|
SHUTDOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Version {
|
2020-08-17 17:03:54 +02:00
|
|
|
UNKNOWN_VERSION = 0,
|
|
|
|
VERSION_1 = 1,
|
|
|
|
VERSION_2 = 2
|
2018-10-20 14:20:06 +02:00
|
|
|
};
|
|
|
|
|
2020-09-30 17:56:12 +02:00
|
|
|
enum Method {
|
|
|
|
UNKNOWN,
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
DELETE,
|
|
|
|
OPTIONS,
|
|
|
|
PUT,
|
|
|
|
PATCH
|
|
|
|
};
|
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
protected:
|
|
|
|
enum State state;
|
|
|
|
enum Version version;
|
2017-08-27 17:04:25 +02:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
lws *wsi;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
Web *web;
|
2018-10-20 14:20:06 +02:00
|
|
|
Api *api;
|
2017-12-09 02:19:28 +08:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
Logger logger;
|
2018-10-20 14:20:06 +02:00
|
|
|
|
2020-10-20 22:17:55 +02:00
|
|
|
std::unique_ptr<Request> request;
|
|
|
|
std::unique_ptr<Response> response;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
bool headersSent;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
public:
|
|
|
|
Session(struct lws *w);
|
|
|
|
~Session();
|
|
|
|
|
|
|
|
std::string getName() const;
|
2017-08-27 17:59:59 +02:00
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
SuperNode * getSuperNode() const
|
2018-10-20 14:20:06 +02:00
|
|
|
{
|
|
|
|
return api->getSuperNode();
|
|
|
|
}
|
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
void open(void *in, size_t len);
|
|
|
|
int writeable();
|
|
|
|
void body(void *in, size_t len);
|
|
|
|
void bodyComplete();
|
|
|
|
void execute();
|
|
|
|
void shutdown();
|
|
|
|
|
2020-09-30 17:56:12 +02:00
|
|
|
static int
|
|
|
|
protocolCallback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
|
2020-08-17 17:03:54 +02:00
|
|
|
|
2020-09-30 17:56:12 +02:00
|
|
|
Method getRequestMethod() const;
|
2020-08-17 17:03:54 +02:00
|
|
|
|
2020-09-30 17:56:12 +02:00
|
|
|
static std::string
|
|
|
|
methodToString(Method meth);
|
2018-10-20 14:20:06 +02:00
|
|
|
};
|
2018-06-28 13:42:50 +02:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace api
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|