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

Valerio Pastore's avatar
Valerio Pastore committed
using namespace inaf::oasbo::Providers;
Valerio Pastore's avatar
Valerio Pastore committed
RedisProvider::RedisProvider() {
astri's avatar
astri committed
	RedisProvider("127.0.0.1",6379,"DAQ_key");
Valerio Pastore's avatar
Valerio Pastore committed
}

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);
astri's avatar
astri committed
}

int RedisProvider::write(PacketLib::BasePacket &packet) {
	return write(packet,this->key);
}

int RedisProvider::write(PacketLib::BasePacket &packet, std::string key) {
	if (context == nullptr || context->err){
astri's avatar
astri committed
	    if (context) {
	        std::cout << "Redis Provider Error: "<< context->errstr << std::endl;
	    } else {
	        std::cout << "Redis Provider Error: Can't allocate Redis context" << std::endl;
	    }
Valerio Pastore's avatar
Valerio Pastore committed
	    return -1;
	}
Valerio Pastore's avatar
Valerio Pastore committed
	uint size = packet.getHeaderSize() + packet.getPayloadSize() + packet.getTailSize();
Valerio Pastore's avatar
Valerio Pastore committed
	redisReply *r = (redisReply *) redisCommand(context, "LPUSH %s %b", key.c_str(), (char*) packet.getBinaryPointer(), size);
	if (r == NULL) {
	    // Error executing Redis command
	    return -1;
	}
	// Check the type of the Redis reply
	if (r->type == REDIS_REPLY_ERROR) {
	    // Error executing Redis command
		std::cout << r->str << std::endl;
	    freeReplyObject(r);
	    return -1;
	}
    freeReplyObject(r);
Valerio Pastore's avatar
Valerio Pastore committed
	return size;
Valerio Pastore's avatar
Valerio Pastore committed
}

Valerio Pastore's avatar
Valerio Pastore committed
int RedisProvider::open() {
astri's avatar
astri committed
	if (context != nullptr && !context->err)
		close();
	context = redisConnect(getIp().c_str(), getPort());
	if (context == nullptr || context->err) {
	    if (context) {
	        std::cout << "Redis Provider Error: "<< context->errstr << std::endl;
	    } else {
	        std::cout << "Redis Provider Error: Can't allocate Redis context" << std::endl;
	    }
	    return -1;
	}
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() {
astri's avatar
astri committed
	if (context != nullptr && !context->err ){
		redisFree(this->context);
		context = nullptr;
	}
Valerio Pastore's avatar
Valerio Pastore committed
	return 1;
Valerio Pastore's avatar
Valerio Pastore committed
}
Valerio Pastore's avatar
Valerio Pastore committed

astri's avatar
astri committed
bool RedisProvider::isOpen() {
	return (context != nullptr && !context->err );
}

Valerio Pastore's avatar
Valerio Pastore committed
RedisProvider::~RedisProvider(){
astri's avatar
astri committed
	close();
Valerio Pastore's avatar
Valerio Pastore committed
}