2021-06-16 10:36:19 -04:00
|
|
|
/** Redis node-type
|
|
|
|
*
|
|
|
|
* @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
|
2021-06-16 10:36:19 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include <sw/redis++/redis++.h>
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/config.hpp>
|
|
|
|
#include <villas/node.hpp>
|
|
|
|
#include <villas/timing.hpp>
|
2021-06-16 10:36:19 -04:00
|
|
|
#include <villas/format.hpp>
|
|
|
|
#include <villas/task.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/pool.hpp>
|
2021-06-16 10:36:19 -04:00
|
|
|
#include <villas/queue_signalled.h>
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2021-06-16 10:36:19 -04:00
|
|
|
enum class RedisMode {
|
|
|
|
KEY,
|
|
|
|
HASH,
|
|
|
|
CHANNEL
|
|
|
|
};
|
|
|
|
|
|
|
|
class RedisConnection {
|
|
|
|
|
|
|
|
public:
|
|
|
|
sw::redis::Redis context;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
enum State {
|
|
|
|
INITIALIZED,
|
|
|
|
RUNNING,
|
|
|
|
STOPPING
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread thread;
|
|
|
|
std::atomic<enum State> state;
|
|
|
|
|
2021-07-20 21:05:54 +02:00
|
|
|
void onMessage(const std::string &channel, const std::string &msg);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
|
|
|
void loop();
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
std::unordered_multimap<std::string, NodeCompat *> subscriberMap;
|
2021-06-16 10:36:19 -04:00
|
|
|
|
|
|
|
sw::redis::Subscriber subscriber;
|
|
|
|
|
|
|
|
villas::Logger logger;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
RedisConnection(const sw::redis::ConnectionOptions &opts);
|
|
|
|
|
|
|
|
static
|
|
|
|
RedisConnection * get(const sw::redis::ConnectionOptions &opts);
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void subscribe(NodeCompat *n, const std::string &channel);
|
|
|
|
void unsubscribe(NodeCompat *n, const std::string &channel);
|
2021-06-16 10:36:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct redis {
|
|
|
|
sw::redis::ConnectionOptions options;
|
|
|
|
|
|
|
|
RedisConnection *conn;
|
|
|
|
|
|
|
|
enum RedisMode mode;
|
|
|
|
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
bool notify; /**< Use Redis Keyspace notifications to listen for updates. */
|
|
|
|
|
|
|
|
struct Task task; /**< Timer for periodic events. */
|
|
|
|
double rate; /**< Rate for polling key updates if keyspace notifications are disabled. */
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
Format *formatter;
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Pool pool;
|
|
|
|
struct CQueueSignalled queue;
|
2021-06-16 10:36:19 -04:00
|
|
|
};
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_init(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_destroy(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_parse(NodeCompat *n, json_t *json);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
char * redis_print(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_check(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_prepare(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_start(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_stop(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_pause(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_resume(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_write(NodeCompat *n, struct Sample * const smps[], unsigned cnt);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_read(NodeCompat *n, struct Sample * const smps[], unsigned cnt);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_reverse(NodeCompat *n);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_poll_fds(NodeCompat *n, int fds[]);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int redis_netem_fds(NodeCompat *n, int fds[]);
|
2021-06-16 10:36:19 -04:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|