2017-02-18 10:57:29 -05:00
|
|
|
/** LWS-releated functions.
|
|
|
|
*
|
|
|
|
* @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-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
|
|
|
|
2021-10-01 07:52:01 -04:00
|
|
|
#include <libwebsockets.h>
|
2018-07-03 21:51:48 +02:00
|
|
|
#include <jansson.h>
|
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
#include <villas/log.hpp>
|
2020-03-04 12:41:55 +01:00
|
|
|
#include <villas/common.hpp>
|
2018-10-20 14:20:06 +02:00
|
|
|
#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
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
class Web {
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
protected:
|
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:
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
/** 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
|
|
|
|
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
|
2018-12-02 02:56:52 +01:00
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-06-15 22:16:38 +02:00
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|