2015-12-02 13:55:58 +01:00
|
|
|
/** Node type: WebSockets
|
|
|
|
*
|
|
|
|
* @file
|
2022-03-15 09:18:01 -04:00
|
|
|
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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
|
|
|
|
2021-07-07 10:39:35 +02:00
|
|
|
#include <spdlog/fmt/ostr.h>
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/pool.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/queue_signalled.h>
|
2020-03-04 12:41:55 +01:00
|
|
|
#include <villas/common.hpp>
|
2020-06-08 04:03:07 +02:00
|
|
|
#include <villas/buffer.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>
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
struct lws;
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
2017-04-15 22:50:37 +02:00
|
|
|
|
2021-06-21 16:11:42 -04:00
|
|
|
/* Forward declarations */
|
2021-08-10 10:12:48 -04:00
|
|
|
class NodeCompat;
|
2021-06-21 16:11:42 -04:00
|
|
|
|
2018-08-02 10:45:15 +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 {
|
2021-08-10 10:12:48 -04:00
|
|
|
struct List destinations; /**< List of websocket servers connect to in client mode (struct websocket_destination). */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2022-01-11 07:33:43 -05:00
|
|
|
bool wait; /**< Wait until all destinations are connected. */
|
|
|
|
|
2021-08-10 10:12:48 -04: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 {
|
|
|
|
char *uri;
|
|
|
|
struct lws_client_connect_info info;
|
|
|
|
};
|
|
|
|
|
2017-03-29 06:01:50 +02:00
|
|
|
/* Internal datastructures */
|
2016-06-08 22:39:17 +02:00
|
|
|
struct websocket_connection {
|
2019-06-23 16:13:23 +02:00
|
|
|
enum State {
|
2019-04-22 23:43:46 +02:00
|
|
|
DESTROYED,
|
|
|
|
INITIALIZED,
|
|
|
|
CONNECTING,
|
|
|
|
RECONNECTING,
|
|
|
|
ESTABLISHED,
|
2022-01-11 09:13:51 -05:00
|
|
|
CLOSING,
|
|
|
|
CLOSED,
|
2019-04-22 23:43:46 +02:00
|
|
|
ERROR
|
2018-06-29 08:37:14 +02:00
|
|
|
} state; /**< The current status of this connection. */
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum class Mode {
|
2019-04-22 23:43:46 +02:00
|
|
|
CLIENT,
|
|
|
|
SERVER,
|
2018-06-29 08:37:14 +02:00
|
|
|
} mode;
|
|
|
|
|
|
|
|
struct lws *wsi;
|
2021-08-10 10:12:48 -04:00
|
|
|
NodeCompat *node;
|
|
|
|
Format *formatter;
|
|
|
|
struct CQueue queue; /**< For samples which are sent to the Websocket */
|
2018-06-29 08:37:14 +02:00
|
|
|
|
|
|
|
struct websocket_destination *destination;
|
|
|
|
|
|
|
|
struct {
|
2020-10-20 22:17:55 +02:00
|
|
|
villas::Buffer *recv; /**< A buffer for reconstructing fragmented messages. */
|
|
|
|
villas::Buffer *send; /**< A buffer for constructing messages before calling lws_write() */
|
2018-06-29 08:37:14 +02:00
|
|
|
} buffers;
|
|
|
|
|
2021-07-07 10:39:35 +02:00
|
|
|
/** Custom formatter for spdlog */
|
|
|
|
template<typename OStream>
|
|
|
|
friend OStream &operator<<(OStream &os, const struct websocket_connection &c)
|
|
|
|
{
|
|
|
|
if (c.wsi) {
|
|
|
|
char name[128];
|
|
|
|
|
2021-10-01 07:52:19 -04:00
|
|
|
lws_get_peer_simple(c.wsi, name, sizeof(name));
|
2021-07-07 10:39:35 +02:00
|
|
|
|
2021-10-01 07:52:19 -04:00
|
|
|
os << "remote=" << name;
|
2021-07-07 10:39:35 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
os << ", mode=" << (c.mode == websocket_connection::Mode::CLIENT ? "client" : "server");
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
2017-03-29 06:01:50 +02:00
|
|
|
};
|
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
int websocket_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
char * websocket_print(NodeCompat *n);
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_read(NodeCompat *n, struct Sample * const smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int websocket_write(NodeCompat *n, struct Sample * const smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|