diff --git a/CMakeLists.txt b/CMakeLists.txt index eda4541cfe7d468324553a1c600acb0ad0675d2b..851432231d7371aa403647c87371466efb830db7 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.9) project(Redis_Receiver) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") option(REDISRECEIVER_BUILD_SHARED "Build Redisreceiver as a shared library." OFF) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index 8a0ea2d0e699863df5fe1c91caf2d7b0855957be..a00f9a27afbf5f75dab7db2368b9b9b6fcb395e1 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit 8a0ea2d0e699863df5fe1c91caf2d7b0855957be +Subproject commit a00f9a27afbf5f75dab7db2368b9b9b6fcb395e1 diff --git a/include/Redis_Receiver.h b/include/Redis_Receiver.h index 879f712fdee7d51b54826696636dd1c1cd4f6c76..d9e03caed3de09218988970c4f19739a8a108f9c 100755 --- a/include/Redis_Receiver.h +++ b/include/Redis_Receiver.h @@ -1,74 +1,201 @@ - #pragma once #include #include #include - +/** + * @brief Namespace containing classes related to receiving data from Redis. + */ namespace inaf::oasbo::Receivers { +/** + * @brief Class for receiving data from Redis. + * @details This class inherits from BaseReceiver and provides functionality to connect to a Redis server, + * receive data from it, and manage the connection. + * check the Base_Receiver.h file for more information. + */ class RedisReceiver: public BaseReceiver { protected: - redisContext *context = nullptr; - std::string ip { }; - int port; - std::string key; + redisContext *context = nullptr; /**< Pointer to the Redis context. */ + std::string ip { }; /**< IP address of the Redis server. */ + int port; /**< Port number of the Redis server. */ + std::string key; /**< Key used to access data in the Redis server. */ + /** + * @brief Constructor for RedisReceiver. + * @param ip The IP address of the Redis server. + * @param prt The port number of the Redis server. + * @param key The key used to access data in the Redis server. + */ RedisReceiver(std::string ip, int prt, std::string key); + + /** + * @brief Default constructor for RedisReceiver. + */ RedisReceiver(); + + /** + * @brief Splits the IP address, port number, and key from a string. + * @param ip_port_key The string containing the IP address, port number, and key. + * @param ip The extracted IP address. + * @param port The extracted port number. + * @param key The extracted key. + * @return True if the string was successfully split, false otherwise. + */ bool split_ip_port_key(const std::string &ip_port_key, std::string &ip, int &port, std::string &key); - void resetPacket(PacketLib::BasePacket&, int bytes); + + /** + * @brief Resets the first bytes of the packet to 0. + * @param pack The packet to reset. + * @param bytes The number of bytes to reset. + */ + void resetPacket(Packets::BasePacket &packet, int bytes); public: + /** + * @brief Destructor for RedisReceiver. + */ ~RedisReceiver(); - int check_interval; + int check_interval; /**< The interval at which to check for new data in Redis. */ + + /** + * @brief Get the host name of the Redis server. + * @return The host name. + */ std::string getHost() override; + + /** + * @brief Set the host name of the Redis server. + * @param host The host name to set. + */ void setHost(std::string host) override; - void setIp(std::string); + /** + * @brief Set the IP address of the Redis server. + * @param ip The IP address to set. + */ + void setIp(std::string ip); + + /** + * @brief Get the IP address of the Redis server. + * @return The IP address. + */ std::string getIp() { return this->ip; } - void setPort(int); + + /** + * @brief Set the port number of the Redis server. + * @param port The port number to set. + */ + void setPort(int port); + + /** + * @brief Get the port number of the Redis server. + * @return The port number. + */ int getPort() { return this->port; } - void setKey(std::string); + + /** + * @brief Set the key used to access data in the Redis server. + * @param key The key to set. + */ + void setKey(std::string key); + + /** + * @brief Get the key used to access data in the Redis server. + * @return The key. + */ std::string getKey() { return this->key; } int connectToClient() override; + int closeConnectionToClient() override; + bool isConnectedToClient() const override; - int receiveFromClient(PacketLib::BasePacket&) override; + + int receiveFromClient(Packets::BasePacket &packet) override; friend class RedisReceiverBuilder; }; + +/** + * @brief Builder class for creating RedisReceiver objects. + */ class RedisReceiverBuilder { protected: - RedisReceiver *receiver; + RedisReceiver *receiver; /**< Pointer to the RedisReceiver object being built. */ public: - std::string config_target { "redisreceiver" }; - std::string ip_key { "ip" }; - std::string port_key { "port" }; - std::string key_key { "key" }; - std::string checkinterval_key { "checkinterval" }; + std::string config_target { "redisreceiver" }; /**< The configuration target for the builder. */ + std::string ip_key { "ip" }; /**< The configuration key for the IP address. */ + std::string port_key { "port" }; /**< The configuration key for the port number. */ + std::string key_key { "key" }; /**< The configuration key for the Redis key. */ + std::string checkinterval_key { "checkinterval" }; /**< The configuration key for the check interval. */ + /** + * @brief Default constructor for RedisReceiverBuilder. + */ RedisReceiverBuilder(); + + /** + * @brief Constructor for RedisReceiverBuilder. + * @param ip The IP address of the Redis server. + * @param port The port number of the Redis server. + * @param key The key used to access data in the Redis server. + */ RedisReceiverBuilder(std::string ip, int port, std::string key); + + /** + * @brief Destructor for RedisReceiverBuilder. + */ ~RedisReceiverBuilder(); + + /** + * @brief Reset the builder to its initial state. + */ void reset(); + + /** + * @brief Configure the builder from a configurator object. + * @param conf The configurator object to configure from. + * @return A pointer to the builder. + */ RedisReceiverBuilder* configFrom(Configurators::BaseConfigurator &conf); + + /** + * @brief Set the IP address of the Redis server. + * @param ip The IP address to set. + * @return A pointer to the builder. + */ RedisReceiverBuilder* setIp(std::string ip); + + /** + * @brief Set the port number of the Redis server. + * @param port The port number to set. + * @return A pointer to the builder. + */ RedisReceiverBuilder* setPort(int port); + + /** + * @brief Set the key used to access data in the Redis server. + * @param key The key to set. + * @return A pointer to the builder. + */ RedisReceiverBuilder* setKey(std::string key); + /** + * @brief Get the built RedisReceiver object. + * @return A pointer to the RedisReceiver object. + */ RedisReceiver* getReceiver(); }; } diff --git a/src/Redis_Receiver.cpp b/src/Redis_Receiver.cpp index bef1feb0da33f3c49d0f418ee93e5a4adf5ce03b..44b9b463eecebe7ede4aeddb44ecfffd17930401 100755 --- a/src/Redis_Receiver.cpp +++ b/src/Redis_Receiver.cpp @@ -51,8 +51,7 @@ int RedisReceiver::connectToClient() { } time_t now = time(nullptr); std::cout << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") - << "]\t[Redis Receiver]\t" << "Connected" - << std::endl; + << "]\t[Redis Receiver]\t" << "Connected" << std::endl; return 1; } @@ -68,7 +67,7 @@ bool RedisReceiver::isConnectedToClient() const { return !(context == nullptr || context->err); } -int RedisReceiver::receiveFromClient(PacketLib::BasePacket &pack) { +int RedisReceiver::receiveFromClient(Packets::BasePacket &pack) { if (!isConnectedToClient()) { if (context) { time_t now = time(nullptr); @@ -92,25 +91,24 @@ int RedisReceiver::receiveFromClient(PacketLib::BasePacket &pack) { std::this_thread::sleep_for(std::chrono::seconds(this->check_interval)); return -1; } - if(r->type == REDIS_REPLY_NIL){ + if (r->type == REDIS_REPLY_NIL) { freeReplyObject(r); std::this_thread::sleep_for(std::chrono::seconds(this->check_interval)); return 0; } - if (r->type == REDIS_REPLY_ERROR ) { + if (r->type == REDIS_REPLY_ERROR) { freeReplyObject(r); std::this_thread::sleep_for(std::chrono::seconds(this->check_interval)); return -1; } - if (r->type == REDIS_REPLY_STRING && r->type != REDIS_REPLY_NIL) { + if (r->type == REDIS_REPLY_STRING && r->type != REDIS_REPLY_NIL) { // Received int size = r->len; ssize_t header_size = pack.getHeaderSize(); if (size < header_size) { freeReplyObject(r); return -1; } - pack.copyToMemory(reinterpret_cast(r->str), - header_size); + pack.copyToMemory(reinterpret_cast(r->str), header_size); ssize_t tot_packet_size = pack.getHeaderSize() + pack.getPayloadSize() + pack.getTailSize(); @@ -203,7 +201,7 @@ bool RedisReceiver::split_ip_port_key(const std::string &ip_port_key, return true; } -void RedisReceiver::resetPacket(PacketLib::BasePacket &pack, int bytes) { +void RedisReceiver::resetPacket(Packets::BasePacket &pack, int bytes) { uint8_t *buff = new uint8_t[bytes]; std::memset(buff, 0, bytes); int toBeReset = std::min(