2023-09-04 12:21:37 +02:00
|
|
|
/* Node type: WebSockets.
|
2015-12-02 13:55:58 +01:00
|
|
|
*
|
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-03-03 20:20:13 -04:00
|
|
|
*/
|
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2023-09-25 12:40:55 +02:00
|
|
|
#include <fmt/ostream.h>
|
2024-02-08 14:55:05 +00:00
|
|
|
#include <libwebsockets.h>
|
2020-06-08 04:03:07 +02:00
|
|
|
#include <villas/buffer.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/common.hpp>
|
2023-09-25 12:40:55 +02:00
|
|
|
#include <villas/config.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/format.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node.hpp>
|
|
|
|
#include <villas/node/config.hpp>
|
2023-01-10 15:25:27 +01:00
|
|
|
#include <villas/node_compat.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/pool.hpp>
|
|
|
|
#include <villas/queue_signalled.h>
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
struct lws;
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
2017-04-15 22:50:37 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#define DEFAULT_WEBSOCKET_QUEUE_LENGTH (DEFAULT_QUEUE_LENGTH * 64)
|
2016-07-11 18:18:20 +02:00
|
|
|
|
2016-06-08 22:39:17 +02:00
|
|
|
// Internal data per websocket node
|
|
|
|
struct websocket {
|
2023-09-07 11:46:39 +02:00
|
|
|
struct List
|
|
|
|
destinations; // List of websocket servers connect to in client mode (struct websocket_destination).
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
bool wait; // Wait until all destinations are connected.
|
2022-01-11 07:33:43 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct Pool pool;
|
|
|
|
struct CQueueSignalled
|
|
|
|
queue; // For samples which are received from WebSockets
|
2016-06-08 22:39:17 +02:00
|
|
|
};
|
|
|
|
|
2021-07-07 10:39:35 +02:00
|
|
|
struct websocket_destination {
|
2023-09-07 11:46:39 +02:00
|
|
|
char *uri;
|
|
|
|
struct lws_client_connect_info info;
|
2021-07-07 10:39:35 +02:00
|
|
|
};
|
|
|
|
|
2017-03-29 06:01:50 +02:00
|
|
|
// Internal datastructures
|
2016-06-08 22:39:17 +02:00
|
|
|
struct websocket_connection {
|
2023-09-07 11:46:39 +02:00
|
|
|
enum State {
|
|
|
|
DESTROYED,
|
|
|
|
INITIALIZED,
|
|
|
|
CONNECTING,
|
|
|
|
RECONNECTING,
|
|
|
|
ESTABLISHED,
|
|
|
|
CLOSING,
|
|
|
|
CLOSED,
|
|
|
|
ERROR
|
|
|
|
} state; // The current status of this connection.
|
|
|
|
|
|
|
|
enum class Mode {
|
|
|
|
CLIENT,
|
|
|
|
SERVER,
|
|
|
|
} mode;
|
|
|
|
|
|
|
|
struct lws *wsi;
|
|
|
|
NodeCompat *node;
|
|
|
|
Format *formatter;
|
|
|
|
struct CQueue queue; // For samples which are sent to the Websocket
|
|
|
|
|
|
|
|
struct websocket_destination *destination;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
villas::Buffer *recv; // A buffer for reconstructing fragmented messages.
|
|
|
|
villas::Buffer
|
|
|
|
*send; // A buffer for constructing messages before calling lws_write()
|
|
|
|
} buffers;
|
|
|
|
|
2023-09-25 12:40:06 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &os,
|
2024-02-08 14:55:05 +00:00
|
|
|
const struct websocket_connection &c) {
|
2023-09-07 11:46:39 +02:00
|
|
|
if (c.wsi) {
|
|
|
|
char name[128];
|
|
|
|
|
|
|
|
lws_get_peer_simple(c.wsi, name, sizeof(name));
|
|
|
|
|
|
|
|
os << "remote=" << name;
|
|
|
|
} else if (c.mode == websocket_connection::Mode::CLIENT &&
|
|
|
|
c.destination != nullptr)
|
|
|
|
os << "dest=" << c.destination->info.address << ":"
|
|
|
|
<< c.destination->info.port;
|
|
|
|
|
|
|
|
if (c.node)
|
|
|
|
os << ", node=" << c.node->getName();
|
|
|
|
|
|
|
|
os << ", mode="
|
|
|
|
<< (c.mode == websocket_connection::Mode::CLIENT ? "client" : "server");
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << *this;
|
|
|
|
return ss.str();
|
|
|
|
}
|
2017-03-29 06:01:50 +02:00
|
|
|
};
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int websocket_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len);
|
2017-02-16 09:04:12 -03:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_type_start(SuperNode *sn);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int websocket_type_stop();
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_parse(NodeCompat *n, json_t *j);
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
char *websocket_print(NodeCompat *n);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
int websocket_start(NodeCompat *n);
|
|
|
|
|
|
|
|
int websocket_stop(NodeCompat *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2022-01-11 07:35:28 -05:00
|
|
|
int websocket_init(NodeCompat *n);
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_destroy(NodeCompat *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_poll_fds(NodeCompat *n, int fds[]);
|
2016-02-04 17:13:28 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int websocket_read(NodeCompat *n, struct Sample *const smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int websocket_write(NodeCompat *n, struct Sample *const smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|
2023-09-25 12:40:06 +02:00
|
|
|
|
|
|
|
#ifndef FMT_LEGACY_OSTREAM_FORMATTER
|
|
|
|
template <>
|
|
|
|
class fmt::formatter<villas::node::websocket_connection>
|
|
|
|
: public fmt::ostream_formatter {};
|
|
|
|
#endif
|