Skip to content
Snippets Groups Projects
File_Receiver.h 4.01 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
#pragma once

Valerio Pastore's avatar
Valerio Pastore committed
#include <Base_Receiver.h>
Valerio Pastore's avatar
Valerio Pastore committed
#include <Base_Configurator.h>
#include <fstream>

Valerio Pastore's avatar
.  
Valerio Pastore committed
#ifndef RAW_FILES_PATH
#define RAW_FILES_PATH std::string(std::getenv("HOME")).append("/BIAS/config/RawTestFiles")
#endif
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
/**
 * @brief The namespace inaf::oasbo::Receivers contains classes related to receiving data.
 */
Valerio Pastore's avatar
Valerio Pastore committed
namespace inaf::oasbo::Receivers {
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
/**
 * @brief The FileReceiver class is a derived class of BaseReceiver and is responsible for receiving packets from files.
 * check the Base_Receiver.h file for more information.
 */
Valerio Pastore's avatar
Valerio Pastore committed
class FileReceiver: public BaseReceiver {
Valerio Pastore's avatar
Valerio Pastore committed
protected:
Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Constructs a FileReceiver object with a source and a rate.
	 * @param source The source files to receive.
	 * @param rate The rate at which the packets are received.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiver(std::string source, int rate);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Constructs a FileReceiver object with a source.
	 * @param source The source files to receive.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiver(std::string source);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Constructs a FileReceiver object.
	 */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	FileReceiver();
Valerio Pastore's avatar
Valerio Pastore committed

	std::ifstream *ifile = nullptr; /**< Pointer to the input file stream. */
	ssize_t filesize = 0; /**< The size of the file being received. */
	std::vector<std::string> filesToProcess; /**< Vector of files to process. */
	size_t filesCount = 0; /**< The number of files to process. */

	/**
	 * @brief Opens the next file to process.
	 * @return integer representing the status of the operation.
	 */
	int openNextFile();
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Resets the first bytes of the packet to 0.
	 * @param pack The packet to reset.
	 * @param bytes The number of bytes to reset.
	 */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	void resetPacket(Packets::BasePacket &pack, int bytes);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Resolves the environment variable in the given path.
	 * @param path The path containing the environment variable.
	 * @return The resolved path.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	std::string resolveEnvVar(std::string path);
Valerio Pastore's avatar
Valerio Pastore committed

public:
Valerio Pastore's avatar
Valerio Pastore committed
	int rate; /**< The rate at which the packets are received. */
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Destructor for the FileReceiver class.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	~FileReceiver();

	std::string getHost() override;
	void setHost(std::string host) override;

	int connectToClient() override;
	int closeConnectionToClient() override;
	bool isConnectedToClient() const override;
Valerio Pastore's avatar
.  
Valerio Pastore committed
	int receiveFromClient(Packets::BasePacket&) override;
Valerio Pastore's avatar
Valerio Pastore committed

	friend class FileReceiverBuilder;
};
Valerio Pastore's avatar
Valerio Pastore committed

/**
 * @brief The FileReceiverBuilder class is responsible for building FileReceiver objects.
 */
Valerio Pastore's avatar
Valerio Pastore committed
class FileReceiverBuilder {
protected:
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiver *rcv; /**< Pointer to the FileReceiver object being built. */
	std::string sourceFile; /**< The source file for the FileReceiver object. */

Valerio Pastore's avatar
Valerio Pastore committed
public:
Valerio Pastore's avatar
Valerio Pastore committed
	std::string config_target { "filereceiver" }; /**< The configuration target for the FileReceiverBuilder. */
	std::string source_key { "source" }; /**< The source key for the FileReceiverBuilder. */
	std::string rate_key { "rate" }; /**< The rate key for the FileReceiverBuilder. */
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Constructs a FileReceiverBuilder object.
	 */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	FileReceiverBuilder();
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Destructor for the FileReceiverBuilder class.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	~FileReceiverBuilder();
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Resets the FileReceiverBuilder object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void reset();
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Configures the FileReceiverBuilder object from a BaseConfigurator object.
	 * @param conf The BaseConfigurator object to configure from.
	 * @return A pointer to the FileReceiverBuilder object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiverBuilder* configFrom(Configurators::BaseConfigurator &conf);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the source for the FileReceiverBuilder object.
	 * @param source The source of the files to receive.
	 * @return A pointer to the FileReceiverBuilder object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiverBuilder* setSource(std::string source);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the rate for the FileReceiverBuilder object.
	 * @param rate The rate at which the files are received.
	 * @return A pointer to the FileReceiverBuilder object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiverBuilder* setRate(int rate);
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets the built FileReceiver object.
	 * @return A pointer to the built FileReceiver object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	FileReceiver* getReceiver();
};
Valerio Pastore's avatar
Valerio Pastore committed

} // namespace inaf::oasbo::Receivers