2015-12-02 13:55:58 +01:00
|
|
|
/** Node type: WebSockets
|
|
|
|
*
|
|
|
|
* This file implements the websocket type for nodes.
|
|
|
|
* It's based on the libwebsockets library.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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
|
|
|
|
2016-11-08 00:24:57 -05:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2015-12-04 01:47:49 +01:00
|
|
|
#include "node.h"
|
2016-07-11 18:18:20 +02:00
|
|
|
#include "pool.h"
|
2017-04-07 12:25:17 +02:00
|
|
|
#include "queue_signalled.h"
|
2017-04-02 13:02:07 +02:00
|
|
|
#include "common.h"
|
2017-04-15 22:50:37 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#define DEFAULT_WEBSOCKET_QUEUELEN (DEFAULT_QUEUELEN * 64)
|
|
|
|
#define DEFAULT_WEBSOCKET_SAMPLELEN DEFAULT_SAMPLELEN
|
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 {
|
2016-11-08 00:24:57 -05:00
|
|
|
struct list connections; /**< List of active libwebsocket connections in server mode (struct websocket_connection). */
|
|
|
|
struct list destinations; /**< List of websocket servers connect to in client mode (struct websocket_destination). */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-07-11 18:18:20 +02:00
|
|
|
struct pool pool;
|
2017-04-18 17:58:23 +02:00
|
|
|
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 {
|
|
|
|
struct node *node;
|
2016-07-11 18:18:20 +02:00
|
|
|
struct lws *wsi;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
|
|
|
struct queue queue; /**< For samples which are sent to the WebSocket */
|
|
|
|
|
2016-07-11 18:18:20 +02:00
|
|
|
struct {
|
|
|
|
char name[64];
|
|
|
|
char ip[64];
|
|
|
|
} peer;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-24 19:28:45 +02:00
|
|
|
enum {
|
|
|
|
WEBSOCKET_MODE_CLIENT,
|
|
|
|
WEBSOCKET_MODE_SERVER,
|
|
|
|
} mode;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-02 13:02:07 +02:00
|
|
|
enum state state;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
char *_name;
|
2015-12-02 13:55:58 +01:00
|
|
|
};
|
|
|
|
|
2017-03-29 06:01:50 +02:00
|
|
|
struct websocket_destination {
|
|
|
|
char *uri;
|
|
|
|
struct lws_client_connect_info info;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2015-12-02 13:55:58 +01:00
|
|
|
/** @see node_vtable::init */
|
2017-04-07 17:32:36 +02:00
|
|
|
int websocket_init(struct super_node *sn);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::deinit */
|
2015-12-02 13:55:58 +01:00
|
|
|
int websocket_deinit();
|
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::open */
|
2017-03-11 23:30:24 -03:00
|
|
|
int websocket_start(struct node *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::close */
|
2017-03-11 23:30:24 -03:00
|
|
|
int websocket_stop(struct node *n);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::close */
|
2016-02-04 17:13:28 +01:00
|
|
|
int websocket_destroy(struct node *n);
|
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::read */
|
2016-06-08 22:39:17 +02:00
|
|
|
int websocket_read(struct node *n, struct sample *smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-18 17:58:23 +02:00
|
|
|
/** @see node_type::write */
|
2016-06-08 22:39:17 +02:00
|
|
|
int websocket_write(struct node *n, struct sample *smps[], unsigned cnt);
|
2015-12-02 13:55:58 +01:00
|
|
|
|
2017-04-07 12:25:17 +02:00
|
|
|
/** @} */
|