Newer
Older
RedisProvider::RedisProvider(std::string ip, int port, std::string key) {
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;
}
exit(EXIT_FAILURE);
}
int RedisProvider::write(PacketLib::BasePacket &packet) {
uint size = packet.getHeaderSize() + packet.getPayloadSize() + packet.getTailSize();
redisReply *r = (redisReply *) redisCommand(context, "LPUSH %s %b", this->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);
int RedisProvider::write(PacketLib::BasePacket &packet, std::string key) {
uint size = packet.getHeaderSize() + packet.getPayloadSize() + packet.getTailSize();
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);
if (context != nullptr && !context->err ){
redisFree(this->context);
}