Skip to content
Redis_Provider.cpp 1.13 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
	setIp("127.0.0.1");
	setPort(6379);
Valerio Pastore's avatar
Valerio Pastore committed
	setKey("DAQ_key");
}

Valerio Pastore's avatar
Valerio Pastore committed
RedisProvider::RedisProvider(std::string ip, int port, std::string key) {
Valerio Pastore's avatar
Valerio Pastore committed
	setIp(ip);
	setPort(port);
Valerio Pastore's avatar
Valerio Pastore committed
	setKey(key);
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::write(BasePacket &packet) {
Valerio Pastore's avatar
Valerio Pastore committed
	redisReply *r = (redisReply *) redisCommand(context, "LPUSH %s %b","DAQ_key", (char *)packet.getBufferPtr(), (size_t) packet.getPacketSize());
	if(r)
		return 1;
	return -1;
Valerio Pastore's avatar
Valerio Pastore committed
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::write(BasePacket &packet, std::string key) {
Valerio Pastore's avatar
Valerio Pastore committed
	redisReply *r = (redisReply *) redisCommand(context, "LPUSH %s %b",key, (char *)packet.getBufferPtr(), (size_t) packet.getPacketSize());
	if(r)
		return 1;
	return -1;
Valerio Pastore's avatar
Valerio Pastore committed
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::open() {
Valerio Pastore's avatar
Valerio Pastore committed
	context = redisConnect(getIp().c_str(), getPort());
	if (context == NULL || context->err) {
	    if (context) {
	        printf("Error: %s\n", context->errstr);
	    } else {
	        printf("Can't allocate redis context\n");
	    }
Valerio Pastore's avatar
Valerio Pastore committed
	}
Valerio Pastore's avatar
Valerio Pastore committed
	return 1;
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;
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;
}