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_Provider.h b/include/Redis_Provider.h index 601ca1f828ead9f97a0a036a0a392b85ee148085..5bde5514cf55a1dd3ac36f6d448821d032cac9dd 100755 --- a/include/Redis_Provider.h +++ b/include/Redis_Provider.h @@ -1,8 +1,3 @@ -/* - * - * Created on: Mar 1, 2021 - * Author: astrisw - */ #pragma once @@ -13,54 +8,124 @@ namespace inaf::oasbo::Providers { +/** + * @brief The RedisProvider class is a concrete implementation of the BaseProvider class. + * It provides functionality to write data to a Redis server. + * Check Base_Provider.h for more information. + */ class RedisProvider: public BaseProvider { protected: redisContext *context = nullptr; + /** + * @brief Default constructor for RedisProvider. + */ RedisProvider(); + + /** + * @brief Constructor for RedisProvider. + * @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 the Redis server. + */ RedisProvider(std::string ip, int port, std::string key); public: - std::string ip; - int port; - std::string key; + std::string ip; /**< The IP address of the Redis server. */ + int port; /**< The port number of the Redis server. */ + std::string key; /**< The key used to access the Redis server. */ + + int write(Packets::BasePacket&) override; + + int write(Packets::BasePacket&, std::string dest) override; - int write(PacketLib::BasePacket&) override; - int write(PacketLib::BasePacket&, std::string dest) override; void setDest(std::string dest) override; + std::string getDest() override; + int close() override; + int open() override; + bool isOpen() override; + + /** + * @brief Destructor for RedisProvider. + */ ~RedisProvider(); friend class RedisProviderBuilder; }; +/** + * @brief The RedisProviderBuilder class is used to build RedisProvider objects. + */ class RedisProviderBuilder { - protected: - RedisProvider *provider; - - public: - std::string config_target { "redisprovider" }; - std::string ip_key { "ip" }; - std::string port_key { "port" }; - std::string redis_key_key { "key" }; - - RedisProviderBuilder(); - RedisProviderBuilder(std::string ip, int port, std::string topic); - ~RedisProviderBuilder(); - - void reset(); - - RedisProviderBuilder* configFrom(Configurators::BaseConfigurator &conf); - - RedisProviderBuilder* setIp(std::string ip); - - RedisProviderBuilder* setPort(int port); +protected: + RedisProvider *provider; - RedisProviderBuilder* setKey(std::string key); +public: + std::string config_target { "redisprovider" }; + std::string ip_key { "ip" }; + std::string port_key { "port" }; + std::string redis_key_key { "key" }; + + /** + * @brief Default constructor for RedisProviderBuilder. + */ + RedisProviderBuilder(); + + /** + * @brief Constructor for RedisProviderBuilder. + * @param ip The IP address of the Redis server. + * @param port The port number of the Redis server. + * @param topic The topic to write packets to. + */ + RedisProviderBuilder(std::string ip, int port, std::string topic); + + /** + * @brief Destructor for RedisProviderBuilder. + */ + ~RedisProviderBuilder(); + + /** + * @brief Resets the builder to its initial state. + */ + void reset(); + + /** + * @brief Configures the builder from a BaseConfigurator object. + * @param conf The BaseConfigurator object to configure from. + * @return A pointer to the RedisProviderBuilder object. + */ + RedisProviderBuilder* configFrom(Configurators::BaseConfigurator &conf); + + /** + * @brief Sets the IP address of the Redis server. + * @param ip The IP address to set. + * @return A pointer to the RedisProviderBuilder object. + */ + RedisProviderBuilder* setIp(std::string ip); + + /** + * @brief Sets the port number of the Redis server. + * @param port The port number to set. + * @return A pointer to the RedisProviderBuilder object. + */ + RedisProviderBuilder* setPort(int port); + + /** + * @brief Sets the key used to access the Redis server. + * @param key The key to set. + * @return A pointer to the RedisProviderBuilder object. + */ + RedisProviderBuilder* setKey(std::string key); + + /** + * @brief Gets the built RedisProvider object. + * @return A pointer to the built RedisProvider object. + */ + RedisProvider* getProvider(); +}; - RedisProvider* getProvider(); - }; -} +} // namespace inaf::oasbo::Providers diff --git a/src/Redis_Provider.cpp b/src/Redis_Provider.cpp index 07f33c5978f35262f6dbbf834dd51f1c6ad6f680..9a2d7c315642cfcec6973cfa8699614c8dde4577 100755 --- a/src/Redis_Provider.cpp +++ b/src/Redis_Provider.cpp @@ -5,7 +5,6 @@ #include #include - using namespace inaf::oasbo::Providers; RedisProvider::RedisProvider() : RedisProvider("127.0.0.1", 6379, "Astri_ADAS_key") { @@ -15,11 +14,11 @@ RedisProvider::RedisProvider(std::string ip, int port, std::string key) : ip(ip), port(port), key(key) { } -int RedisProvider::write(PacketLib::BasePacket &packet) { +int RedisProvider::write(Packets::BasePacket &packet) { return write(packet, this->key); } -int RedisProvider::write(PacketLib::BasePacket &packet, std::string key) { +int RedisProvider::write(Packets::BasePacket &packet, std::string key) { if (!isOpen()) { if (context) { time_t now = time(nullptr); @@ -36,10 +35,10 @@ int RedisProvider::write(PacketLib::BasePacket &packet, std::string key) { return -1; } - uint size = packet.getHeaderSize() + packet.getPayloadSize() + uint packet_size = packet.getHeaderSize() + packet.getPayloadSize() + packet.getTailSize(); redisReply *r = (redisReply*) redisCommand(context, "LPUSH %s %b", - key.c_str(), (char*) packet.getPointerToMemory(), size); + key.c_str(), (char*) packet.getPointerToMemory(), packet_size); if (r == NULL) { // Error executing Redis command return -1; @@ -54,7 +53,7 @@ int RedisProvider::write(PacketLib::BasePacket &packet, std::string key) { return -1; } freeReplyObject(r); - return size; + return packet_size; } int RedisProvider::open() { @@ -113,6 +112,7 @@ bool RedisProvider::isOpen() { return (context != nullptr && !context->err); } +// dest must be in the form ip:port/key void RedisProvider::setDest(std::string dest) { std::regex pattern( R"((\d+\.\d+\.\d+\.\d+):(\d+)/(.+))");