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/include/villas/web.hpp

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

84 lines
1.8 KiB
C++
Raw Permalink Normal View History

/* 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
*/
#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>
#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>
2018-10-20 14:20:06 +02:00
namespace villas {
namespace node {
// Forward declarations
2018-10-20 14:20:06 +02:00
class Api;
class Web final {
private:
2019-06-23 16:13:23 +02:00
enum State state;
2019-01-17 20:28:20 +01:00
Logger logger;
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-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.
2018-10-20 14:20:06 +02:00
std::thread thread;
std::atomic<bool> running; // Atomic flag for signalizing thread termination.
2018-10-20 14:20:06 +02:00
Api *api;
2018-10-20 14:20:06 +02:00
void worker();
2018-10-20 14:20:06 +02:00
public:
/* Initialize the web interface.
*
* The web interface is based on the libwebsockets library.
*/
Web(Api *a = nullptr);
~Web();
2018-10-20 14:20:06 +02:00
void start();
void stop();
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
};
} // namespace node
} // namespace villas