Skip to content
UDP_Protocol.h 1.66 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
/*
 *
 *  Created on: Mar 1, 2021
 *      Author: astrisw
 */
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
 * class UDPProtocol
Valerio Pastore's avatar
Valerio Pastore committed
 * Implementation of a UDP socket
 * @member field fd_sock: socket file descriptor
 * @member field sockaddr_in: struct of the client address.
 */
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();
Valerio Pastore's avatar
Valerio Pastore committed
};