Skip to content
Redis_Provider.cpp 1.05 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
#include <Redis_Provider.h>

Valerio Pastore's avatar
Valerio Pastore committed
RedisProvider::RedisProvider() {
Valerio Pastore's avatar
Valerio Pastore committed
	setKey("DAQ_key");
	this->stream = rediscpp::make_stream("localhost", "6379");

}

Valerio Pastore's avatar
Valerio Pastore committed
RedisProvider::RedisProvider(std::string ip, int port, std::string key) {
Valerio Pastore's avatar
Valerio Pastore committed
	setKey(key);
Valerio Pastore's avatar
Valerio Pastore committed
	this->stream = rediscpp::make_stream(ip, std::to_string(port));
Valerio Pastore's avatar
Valerio Pastore committed
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::write(BasePacket &packet) {
Valerio Pastore's avatar
Valerio Pastore committed
	auto response = rediscpp::execute(*stream, "set",
	                getKey(), (char *) packet.getBufferPtr());
    std::flush(*stream);
Valerio Pastore's avatar
Valerio Pastore committed
	return 1;
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::write(BasePacket &packet, std::string key) {
Valerio Pastore's avatar
Valerio Pastore committed
	return 1;
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::open() {
	try {
		auto response = rediscpp::execute(*stream, "ping");
Valerio Pastore's avatar
Valerio Pastore committed
        std::cout << response.as<std::string>() << std::endl;
Valerio Pastore's avatar
Valerio Pastore committed
		return 1;
	} catch (std::exception const &e) {
Valerio Pastore's avatar
Valerio Pastore committed
		std::cerr << "Redis Connection Error: " << e.what() << std::endl;
Valerio Pastore's avatar
Valerio Pastore committed
		return EXIT_FAILURE;
	}
Valerio Pastore's avatar
Valerio Pastore committed
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::close() {
Valerio Pastore's avatar
Valerio Pastore committed
	std::cout << "Flushing..." << std::endl;
    std::flush(*stream);
Valerio Pastore's avatar
Valerio Pastore committed
	return 1;
Valerio Pastore's avatar
Valerio Pastore committed
}
Valerio Pastore's avatar
Valerio Pastore committed

RedisProvider::~RedisProvider(){
	std::cout << "Deleting Redis stream" << std::endl;
}