2015-12-02 13:55:58 +01:00
|
|
|
/** Node type: WebSockets
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, 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
|
|
|
*********************************************************************************/
|
|
|
|
|
2015-12-02 13:55:58 +01:00
|
|
|
/**
|
|
|
|
* @addtogroup websockets WebSockets node type
|
|
|
|
* @ingroup node
|
|
|
|
* @{
|
2017-03-03 20:20:13 -04:00
|
|
|
*/
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#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>
|
2018-05-12 15:25:29 +02:00
|
|
|
#include <villas/io.h>
|
2018-08-23 17:31:01 +02:00
|
|
|
#include <villas/node/config.h>
|
2017-04-15 22:50:37 +02:00
|
|
|
|
2018-08-02 10:45:15 +02:00
|
|
|
#define DEFAULT_WEBSOCKET_QUEUE_LENGTH (DEFAULT_QUEUE_LENGTH * 64)
|
|
|
|
#define DEFAULT_WEBSOCKET_SAMPLE_LENGTH DEFAULT_SAMPLE_LENGTH
|
2016-07-11 18:18:20 +02:00
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
struct lws;
|
2015-12-04 01:47:49 +01:00
|
|
|
|
2016-06-08 22:39:17 +02:00
|
|
|
/** Internal data per websocket node */
|
|
|
|
struct websocket {
|
2019-01-07 10:28:55 +01:00
|
|
|
struct vlist destinations; /**< List of websocket servers connect to in client mode (struct websocket_destination). */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
struct pool pool;
|
|
|
|
struct queue_signalled queue; /**< For samples which are received from WebSockets */
|
2016-06-08 22:39:17 +02:00
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
SHUTDOWN,
|
|
|
|
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;
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *node;
|
2018-06-29 08:37:14 +02:00
|
|
|
struct io io;
|
2020-10-20 22:17:55 +02:00
|
|
|
struct queue queue; /**< For samples which are sent to the Websocket */
|
2018-06-29 08:37:14 +02:00
|
|
|
|
|
|
|
struct format_type *format;
|
|
|
|
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;
|
|
|
|
|
|
|
|
char *_name;
|
2015-12-02 13:55:58 +01:00
|
|
|
};
|
|
|
|
|
2017-03-29 06:01:50 +02:00
|
|
|
struct websocket_destination {
|
2018-06-29 08:37:14 +02:00
|
|
|
char *uri;
|
|
|
|
struct lws_client_connect_info info;
|
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);
|
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
/** @see node_type::type_start */
|
2019-04-23 13:14:47 +02:00
|
|
|
int websocket_type_start(villas::node::SuperNode *sn);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
/** @see node_type::type_stop */
|
|
|
|
int websocket_type_stop();
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2020-01-21 14:27:28 +01:00
|
|
|
/** @see node_type::start */
|
2020-08-25 21:00:52 +02:00
|
|
|
int websocket_start(struct vnode *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2020-01-21 14:27:28 +01:00
|
|
|
/** @see node_type::stop */
|
2020-08-25 21:00:52 +02:00
|
|
|
int websocket_stop(struct vnode *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2020-01-21 14:27:28 +01:00
|
|
|
/** @see node_type::stop */
|
2020-08-25 21:00:52 +02:00
|
|
|
int websocket_destroy(struct vnode *n);
|
2016-02-04 17:13:28 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::read */
|
2020-08-25 21:00:52 +02:00
|
|
|
int websocket_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::write */
|
2020-08-25 21:00:52 +02:00
|
|
|
int websocket_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
/** @} */
|