Skip to content
UDP_Protocol.h 6.94 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed

astri's avatar
astri committed
#pragma once
Valerio Pastore's avatar
Valerio Pastore committed

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

Valerio Pastore's avatar
Valerio Pastore committed
/**
 * @brief The UDPProtocol class is a derived class of BaseReceiver and represents a UDP protocol receiver.
 *  check the Base_Receiver.h file for more information.
 */
namespace inaf::oasbo::Receivers {

class UDPProtocol: public BaseReceiver {
protected:
	/**
	 * @brief Constructs a UDPProtocol object with the specified IP and port.
	 * 
	 * @param ip The IP address to bind the UDP socket to.
	 * @param prt The port number to bind the UDP socket to.
	 */
	UDPProtocol(std::string ip, int prt);

	/**
	 * @brief Constructs a UDPProtocol object with default IP and port.
	 */
	UDPProtocol();

	int srv_sock; /**< The server socket descriptor. */
	std::string ip { }; /**< The IP address to bind the UDP socket to. */
	int port; /**< The port number to bind the UDP socket to. */

	struct sockaddr_in cliaddr; /**< The client address structure. */
	struct sockaddr_in srvaddr; /**< The server address structure. */
	bool checkIncomingPackets = true; /**< Flag that tells if it has to check for incoming udp packets. Set to false when calling closeConnectionToClient. 
	 * Used to avoid the infinite loop in receiveAtLeastNbytes.
	*/
	/**
	 * @brief Splits the IP and port from the given IP:port string.
	 * 
	 * @param ip_port The IP:port string to split.
	 * @param ip The extracted IP address.
	 * @param port The extracted port number.
	 * @return true if the IP and port were successfully extracted, false otherwise.
	 */
	bool split_ip_port(const std::string &ip_port, std::string &ip, int &port);

	/**
	 * @brief Receives at least the specified number of bytes from the client.
	 *  Helper function to receive at least the header of the packet.
	 * @param buff The buffer to store the received data.
	 * @param n_bytes The number of bytes to receive.
	 * @param max_size The max size of bytes to receive.
	 * @return The number of bytes received.
	 */
	int receiveAtLeastNbytes(uint8_t *buff, int n_bytes, int max_size);

	/**
	 * @brief Resets the packet object and sets the number of bytes received.
	 * 
	 * @param packet The packet object to reset.
	 * @param bytes The number of bytes received.
	 */
	void resetPacket(PacketLib::BasePacket& packet, int bytes);

public:
	/**
	 * @brief Destroys the UDPProtocol object.
	 */
	~UDPProtocol();

	/**
	 * @brief Gets the host name or IP address that the UDP socket is bound to.
	 * 
	 * @return The host name or IP address.
	 */
	std::string getHost() override;

	/**
	 * @brief Sets the host name or IP address to bind the UDP socket to.
	 * 
	 * @param host The host name or IP address.
	 */
	void setHost(std::string host) override;

	/**
	 * @brief Sets the IP address to bind the UDP socket to.
	 * 
	 * @param ip The IP address.
	 */
	void setIp(std::string ip);

	/**
	 * @brief Gets the IP address that the UDP socket is bound to.
	 * 
	 * @return The IP address.
	 */
	std::string getIp(){return this->ip;}

	/**
	 * @brief Sets the port number to bind the UDP socket to.
	 * 
	 * @param port The port number.
	 */
	void setPort(int port);

	/**
	 * @brief Gets the port number that the UDP socket is bound to.
	 * 
	 * @return The port number.
	 */
	int getPort(){return this->port;}

	/**
	 * @brief Connects to the client.
	 * 
	 * @return 0 if the connection is successful, -1 otherwise.
	 */
	int connectToClient() override;

	/**
	 * @brief Closes the connection to the client.
	 * 
	 * @return 0 if the connection is closed successfully, -1 otherwise.
	 */
	int closeConnectionToClient() override;

	/**
	 * @brief Checks if the UDP socket is connected to the client.
	 * 
	 * @return true if the UDP socket is connected to the client, false otherwise.
	 */
	bool isConnectedToClient() const override;

	/**
	 * @brief Receives a packet from the client.
	 * 
	 * @param packet The packet object to store the received data.
	 * @return The number of bytes received.
	 */
	int receiveFromClient(PacketLib::BasePacket& packet) override;

	friend class UDPProtocolBuilder;
};

class UDPProtocolBuilder {
protected:
	UDPProtocol *protocol; /**< The UDPProtocol object being built. */
public:
	std::string config_target {"udpreceiver"}; /**< The configuration target. */
	std::string ip_key { "ip" }; /**< The configuration key for IP address. */
	std::string port_key { "port" }; /**< The configuration key for port number. */

	/**
	 * @brief Constructs a UDPProtocolBuilder object.
	 */
	UDPProtocolBuilder();

	/**
	 * @brief Constructs a UDPProtocolBuilder object with the specified IP and port.
	 * 
	 * @param ip The IP address to bind the UDP socket to.
	 * @param port The port number to bind the UDP socket to.
	 */
	UDPProtocolBuilder(std::string ip, int port);

	/**
	 * @brief Destroys the UDPProtocolBuilder object.
	 */
	~UDPProtocolBuilder();

	/**
	 * @brief Resets the UDPProtocolBuilder object.
	 */
	void reset();

	/**
	 * @brief Configures the UDPProtocolBuilder object from a configurator.
	 * 
	 * @param conf The configurator object.
	 * @return A pointer to the UDPProtocolBuilder object.
	 */
	UDPProtocolBuilder* configFrom(Configurators::BaseConfigurator &conf);

	/**
	 * @brief Sets the IP address to bind the UDP socket to.
	 * 
	 * @param ip The IP address.
	 * @return A pointer to the UDPProtocolBuilder object.
	 */
	UDPProtocolBuilder* setIp(std::string ip);

	/**
	 * @brief Sets the port number to bind the UDP socket to.
	 * 
	 * @param port The port number.
	 * @return A pointer to the UDPProtocolBuilder object.
	 */
	UDPProtocolBuilder* setPort(int port);

	/**
	 * @brief Gets the UDPProtocol object that has been built.
	 * 
	 * @return A pointer to the UDPProtocol object.
	 */
	UDPProtocol* getReceiver();
};

}
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
class UDPProtocol: public BaseReceiver {
Valerio Pastore's avatar
Valerio Pastore committed
protected:
	UDPProtocol(std::string ip, int prt);
	UDPProtocol();
	int srv_sock;
	std::string ip { };
	int port;
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	struct sockaddr_in cliaddr;
	struct sockaddr_in srvaddr;
	bool split_ip_port(const std::string &ip_port, std::string &ip,
Valerio Pastore's avatar
Valerio Pastore committed
			int &port);
astri's avatar
astri committed
	int receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize,
			int packetSize);
	void resetPacket(PacketLib::BasePacket&, int bytes);
astri's avatar
astri committed
public:
astri's avatar
astri committed

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

	void setIp(std::string);
	std::string getIp(){return this->ip;}
	void setPort(int);
	int getPort(){return this->port;}
	int connectToClient() override;
	int closeConnectionToClient() override;
	bool isConnectedToClient() const override;
astri's avatar
astri committed
	int receiveFromClient(PacketLib::BasePacket&) override;

Valerio Pastore's avatar
Valerio Pastore committed
	friend class UDPProtocolBuilder;
};
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
class UDPProtocolBuilder {
protected:
	UDPProtocol *protocol;
public:
astri's avatar
astri committed

Valerio Pastore's avatar
ok  
Valerio Pastore committed
	std::string config_target {"udpreceiver"};
Valerio Pastore's avatar
Valerio Pastore committed
	std::string ip_key { "ip" };
	std::string port_key { "port" };
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	UDPProtocolBuilder();
	UDPProtocolBuilder(std::string ip, int port);
	~UDPProtocolBuilder();
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	void reset();
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	UDPProtocolBuilder* configFrom(Configurators::BaseConfigurator &conf);
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	UDPProtocolBuilder* setIp(std::string ip);
astri's avatar
astri committed

Valerio Pastore's avatar
Valerio Pastore committed
	UDPProtocolBuilder* setPort(int port);

Valerio Pastore's avatar
Valerio Pastore committed
	UDPProtocol* getReceiver();