2017-02-18 10:57:29 -05:00
|
|
|
/* LWS-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
|
2017-02-18 10:57:29 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
2017-04-07 17:39:37 +02:00
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
#include <jansson.h>
|
2021-10-01 07:52:01 -04:00
|
|
|
#include <libwebsockets.h>
|
2018-07-03 21:51:48 +02:00
|
|
|
|
2020-03-04 12:41:55 +01:00
|
|
|
#include <villas/common.hpp>
|
2018-10-20 14:20:06 +02:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/queue.hpp>
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
2018-06-28 13:42:50 +02:00
|
|
|
|
2017-02-18 10:57:29 -05:00
|
|
|
// Forward declarations
|
2018-10-20 14:20:06 +02:00
|
|
|
class Api;
|
2017-02-18 10:57:29 -05:00
|
|
|
|
2023-08-22 16:10:22 +02:00
|
|
|
class Web final {
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-08-22 16:10:22 +02:00
|
|
|
private:
|
2019-06-23 16:13:23 +02:00
|
|
|
enum State state;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-17 20:28:20 +01:00
|
|
|
Logger logger;
|
2017-02-18 10:57:29 -05:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
lws_context *context; // The libwebsockets server context.
|
|
|
|
lws_vhost *vhost; // The libwebsockets vhost.
|
|
|
|
|
|
|
|
Queue<lws *>
|
|
|
|
writables; // Queue of WSIs for which we will call lws_callback_on_writable()
|
2018-11-23 20:57:34 +02:00
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
int port; // Port of the build in HTTP / WebSocket server.
|
2018-10-20 14:20:06 +02:00
|
|
|
std::string ssl_cert; // Path to the SSL certitifcate for HTTPS / WSS.
|
|
|
|
std::string ssl_private_key; // Path to the SSL private key for HTTPS / WSS.
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
std::thread thread;
|
|
|
|
std::atomic<bool> running; // Atomic flag for signalizing thread termination.
|
2017-02-18 10:57:29 -05:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
Api *api;
|
2017-02-18 10:57:29 -05:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
void worker();
|
2017-02-18 10:57:29 -05:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
public:
|
|
|
|
/* Initialize the web interface.
|
|
|
|
*
|
|
|
|
* The web interface is based on the libwebsockets library.
|
|
|
|
*/
|
2019-04-05 19:28:42 +02:00
|
|
|
Web(Api *a = nullptr);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-08-22 16:10:22 +02:00
|
|
|
~Web();
|
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
void start();
|
|
|
|
void stop();
|
2018-06-28 13:42:50 +02:00
|
|
|
|
2021-10-01 07:52:01 -04:00
|
|
|
static void lwsLogger(int level, const char *msg);
|
|
|
|
static int lwsLogLevel(Log::Level lvl);
|
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
// Parse HTTPd and WebSocket related options
|
2021-02-16 14:15:14 +01:00
|
|
|
int parse(json_t *json);
|
2018-10-20 14:20:06 +02:00
|
|
|
|
|
|
|
Api *getApi() { return api; }
|
|
|
|
|
2018-12-02 02:56:52 +01:00
|
|
|
// for C-compatability
|
|
|
|
lws_context *getContext() { return context; }
|
|
|
|
|
|
|
|
lws_vhost *getVHost() { return vhost; }
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum State getState() const { return state; }
|
2018-12-02 02:56:52 +01:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
void callbackOnWritable(struct lws *wsi);
|
2021-09-14 10:50:05 +02:00
|
|
|
|
|
|
|
bool isEnabled() { return port != CONTEXT_PORT_NO_LISTEN; }
|
2018-10-20 14:20:06 +02:00
|
|
|
};
|
2018-11-23 20:57:34 +02:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|