From e439520e7e44807274add26433dd4feba8d062ef Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 20:06:18 +0200 Subject: [PATCH 1/4] Configuration refactoring --- CMakeLists.txt | 1 - include/UDP_Protocol.h | 21 ++++++-------- src/UDP_Protocol.cpp | 62 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e68253..b4fef02 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,6 @@ option(UDPPROTOCOL_BUILD_SHARED "Build udpprotocol as a shared library." OFF) set(SOURCES src/UDP_Protocol.cpp - src/UDP_Protocol_Configurator.cpp ) # target diff --git a/include/UDP_Protocol.h b/include/UDP_Protocol.h index f4a9d1e..e06ffb8 100755 --- a/include/UDP_Protocol.h +++ b/include/UDP_Protocol.h @@ -3,8 +3,7 @@ * Created on: Mar 1, 2021 * Author: astrisw */ -#ifndef UDP_PROTOCOL_H_ -#define UDP_PROTOCOL_H_ +#pragma once #include @@ -20,24 +19,23 @@ class UDPProtocol : public BaseProtocol{ protected: int srv_sock; int cli_sock; - std::string ip{}; - int port; + struct sockaddr_in cliaddr; struct sockaddr_in srvaddr; void split_ip_port(const std::string& ip_port, std::string& ip, std::string& port); int receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, int packetSize); void resetPacket(PacketLib::BasePacket &, int bytes); - -public: UDPProtocol(std::string ip, int prt); UDPProtocol(); - ~UDPProtocol(); - std::string getIp(){return ip;} - int getPort(){ return port;} - void setIp(std::string ip){ this->ip = ip;} - void setPort(int port){this->port = port;} + class Builder; + +public: + std::string ip{}; + int port; + + ~UDPProtocol(); int connectToServer() override; int connectToClient() override; @@ -49,4 +47,3 @@ public: int sendToServer(PacketLib::BasePacket &) override; }; } -#endif /* UDP_PROTOCOL_H_ */ diff --git a/src/UDP_Protocol.cpp b/src/UDP_Protocol.cpp index c32df45..a6c305b 100755 --- a/src/UDP_Protocol.cpp +++ b/src/UDP_Protocol.cpp @@ -9,6 +9,7 @@ #include #include +#include using namespace inaf::oasbo::ConnectionProtocols; @@ -136,7 +137,7 @@ int UDPProtocol::connectToClient() { // Creating socket file descriptor if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("socket creation failed"); + std::cerr << "UDP Connection: socket creation failed"<< std::endl; return -1; } @@ -148,7 +149,7 @@ int UDPProtocol::connectToClient() { // Bind the socket with the server address if (bind(sockfd, (const struct sockaddr*) &srvaddr, sizeof(srvaddr)) < 0) { - perror("bind failed"); + std::cerr << "UDP Connection: bind failed"<< std::endl; return -1; } @@ -160,11 +161,12 @@ int UDPProtocol::connectToServer() { int sockfd = 0; // Creating socket file descriptor if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("socket creation failed"); + std::cerr << "UDP Connection: socket creation failed " << std::endl; exit(EXIT_FAILURE); } - printf("Connection made\n"); + std::cout << "UDP Connection: socket creation success " << std::endl; + this->cli_sock = sockfd; return sockfd; @@ -216,3 +218,55 @@ void UDPProtocol::split_ip_port(const std::string &ip_port, ip_address = ip_port.substr(0, colon_pos); port = ip_port.substr(colon_pos + 1); } + + +class UDPProtocol::Builder { +protected: + UDPProtocol *protocol; + +public: + + std::string ip_key{"ip"}; + std::string port_key{"port"}; + + Builder() { + this->reset(); + } + Builder(std::string ip, int port) { + this->protocol = new UDPProtocol(ip, port); + + } + ~Builder() { + delete protocol; + } + + void reset() { + this->protocol = new UDPProtocol(); + } + + Builder* configFrom(Configurators::BaseConfigurator *conf) { + std::map params = + conf->readConfig(); + if (params.count(ip_key) > 0) + protocol->ip = params[ip_key]; + if (params.count(port_key) > 0) + protocol->port = std::stoi(params[port_key]); + return this; + } + + Builder* setIp(std::string ip) { + protocol->ip = ip; + return this; + } + + Builder* setPort(int port) { + protocol->port = port; + return this; + } + + UDPProtocol* getProtocol() { + UDPProtocol *result = this->protocol; + this->reset(); + return result; + } +}; -- GitLab From eb5ab08f2118530ee9d032936bad28be4cf6e9aa Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 20:09:58 +0200 Subject: [PATCH 2/4] fix public builder --- include/UDP_Protocol.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/UDP_Protocol.h b/include/UDP_Protocol.h index e06ffb8..d78f751 100755 --- a/include/UDP_Protocol.h +++ b/include/UDP_Protocol.h @@ -29,7 +29,6 @@ protected: UDPProtocol(std::string ip, int prt); UDPProtocol(); - class Builder; public: std::string ip{}; @@ -45,5 +44,8 @@ public: bool isConnectedToServer() const override; int receiveFromClient(PacketLib::BasePacket &) override; int sendToServer(PacketLib::BasePacket &) override; + + class Builder; + }; } -- GitLab From 429beb358042f1ade65bc635dc4b85dc2d9b9610 Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 25 May 2023 15:55:49 +0200 Subject: [PATCH 3/4] builder refactor --- deps/Base-DAQ | 2 +- include/UDP_Protocol.h | 43 ++++++++++--- src/UDP_Protocol.cpp | 137 +++++++++++++++++++---------------------- 3 files changed, 97 insertions(+), 85 deletions(-) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index a2095e5..f118b02 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit a2095e59d57271e3bef3422a67c6905fd237467c +Subproject commit f118b029ff73dc23989c5b648cd6175b968a9a14 diff --git a/include/UDP_Protocol.h b/include/UDP_Protocol.h index d78f751..6f18e14 100755 --- a/include/UDP_Protocol.h +++ b/include/UDP_Protocol.h @@ -6,6 +6,7 @@ #pragma once #include +#include /* * class UDPProtocol @@ -13,25 +14,26 @@ * @member field fd_sock: socket file descriptor * @member field sockaddr_in: struct of the client address. */ -namespace inaf::oasbo::ConnectionProtocols{ +namespace inaf::oasbo::ConnectionProtocols { -class UDPProtocol : public BaseProtocol{ +class UDPProtocol: public BaseProtocol { protected: int srv_sock; int cli_sock; struct sockaddr_in cliaddr; struct sockaddr_in srvaddr; - void split_ip_port(const std::string& ip_port, std::string& ip, std::string& port); - int receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, int packetSize); - void resetPacket(PacketLib::BasePacket &, int bytes); + void split_ip_port(const std::string &ip_port, std::string &ip, + std::string &port); + int receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, + int packetSize); + void resetPacket(PacketLib::BasePacket&, int bytes); UDPProtocol(std::string ip, int prt); UDPProtocol(); - public: - std::string ip{}; + std::string ip { }; int port; ~UDPProtocol(); @@ -42,10 +44,31 @@ public: int closeConnectionToClient() override; bool isConnectedToClient() const override; bool isConnectedToServer() const override; - int receiveFromClient(PacketLib::BasePacket &) override; - int sendToServer(PacketLib::BasePacket &) override; + int receiveFromClient(PacketLib::BasePacket&) override; + int sendToServer(PacketLib::BasePacket&) override; + + friend class Builder; + class Builder { + protected: + UDPProtocol *protocol; + public: + std::string ip_key { "ip" }; + std::string port_key { "port" }; + + Builder(); + Builder(std::string ip, int port); + ~Builder(); + + void reset(); + + Builder* configFrom(Configurators::BaseConfigurator *conf); + + Builder* setIp(std::string ip); + + Builder* setPort(int port); - class Builder; + UDPProtocol* getProtocol(); + }; }; } diff --git a/src/UDP_Protocol.cpp b/src/UDP_Protocol.cpp index a6c305b..3f1b351 100755 --- a/src/UDP_Protocol.cpp +++ b/src/UDP_Protocol.cpp @@ -11,11 +11,10 @@ #include #include - using namespace inaf::oasbo::ConnectionProtocols; UDPProtocol::UDPProtocol() { - UDPProtocol(std::string("127.0.0.1"),9003); + UDPProtocol(std::string("127.0.0.1"), 9003); } UDPProtocol::UDPProtocol(std::string ip, int port) { @@ -48,11 +47,12 @@ int UDPProtocol::sendToServer(PacketLib::BasePacket &pack) { return val; } -int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, int packetSize) { +int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, + int packetSize) { int bytercv = 0; socklen_t len = sizeof(cliaddr); while (bytercv < headerSize) { - int rcv = recvfrom(srv_sock, &buff[bytercv], packetSize+1, // +1 to recognized if there are more bytes than expected + int rcv = recvfrom(srv_sock, &buff[bytercv], packetSize + 1, // +1 to recognized if there are more bytes than expected MSG_WAITFORONE, (struct sockaddr*) &cliaddr, &len); bytercv += rcv; if (rcv < 0) { @@ -62,7 +62,6 @@ int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, in return bytercv; } - int UDPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { bool headerFlag = false; int headerSize = pack.getHeaderSize(); @@ -71,18 +70,18 @@ int UDPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { uint payloadSize = 0; uint totPacketSize = 0; ssize_t to_be_received = 0; - uint8_t *buff = new uint8_t[(packSize+headerSize)*headerSize]; // to avoid overflow + uint8_t *buff = new uint8_t[(packSize + headerSize) * headerSize]; // to avoid overflow int tot_byte_rcv = 0; - while(true){ - while(!headerFlag){ // until the header has not received - int rcv = receiveAtLeastHeaderSizeBytes(buff,headerSize, packSize); // receive at least headerSize byte, maximum packSize for each udp rcv. - if(rcv == -1){ + while (true) { + while (!headerFlag) { // until the header has not received + int rcv = receiveAtLeastHeaderSizeBytes(buff, headerSize, packSize); // receive at least headerSize byte, maximum packSize for each udp rcv. + if (rcv == -1) { delete buff; return -1; } - tot_byte_rcv+=rcv; + tot_byte_rcv += rcv; pack.copyToBinaryPointer(buff, headerSize); //copy the header into packet to be able to read it. - if(!pack.isRecognizedHeader()){ // reset buffer and exit + if (!pack.isRecognizedHeader()) { // reset buffer and exit resetPacket(pack, headerSize); delete buff; return -1; @@ -93,8 +92,9 @@ int UDPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { to_be_received = totPacketSize - tot_byte_rcv; // Calculate how much is still left to read } - if (to_be_received == 0){ // whole packet has been received. - pack.copyToBinaryPointer(&buff[headerSize],tot_byte_rcv-headerSize, headerSize); // copy the buffer into packet except already copied header + if (to_be_received == 0) { // whole packet has been received. + pack.copyToBinaryPointer(&buff[headerSize], + tot_byte_rcv - headerSize, headerSize); // copy the buffer into packet except already copied header delete buff; return tot_byte_rcv; } @@ -103,30 +103,29 @@ int UDPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { delete buff; return -1; } - uint8_t *tmp_buff = new uint8_t[packSize+headerSize]; // maximum receivable in receiveAtLeastHeaderSizeBytes + uint8_t *tmp_buff = new uint8_t[packSize + headerSize]; // maximum receivable in receiveAtLeastHeaderSizeBytes int rcv = receiveAtLeastHeaderSizeBytes(tmp_buff, headerSize, packSize); - if(rcv == -1){ + if (rcv == -1) { delete buff; delete tmp_buff; resetPacket(pack, tot_byte_rcv); return -1; } - std::vector vec; - std::copy(tmp_buff, tmp_buff + headerSize, std::back_inserter(vec)); - if(pack.isRecognizedHeader(vec)){ //another header received, save it and discard previous data. - std::memset(buff,0,tot_byte_rcv); + std::vector vec; + std::copy(tmp_buff, tmp_buff + headerSize, std::back_inserter(vec)); + if (pack.isRecognizedHeader(vec)) { //another header received, save it and discard previous data. + std::memset(buff, 0, tot_byte_rcv); std::memcpy(buff, tmp_buff, rcv); headerFlag = true; pack.copyToBinaryPointer(buff, headerSize); payloadSize = pack.getPayloadSize(); totPacketSize = headerSize + payloadSize + tailSize; to_be_received = totPacketSize - rcv; - tot_byte_rcv=rcv; - } - else{ // append to current buff - tot_byte_rcv+=rcv; - std::memcpy(&buff[tot_byte_rcv-rcv], tmp_buff, rcv); - to_be_received-=rcv; + tot_byte_rcv = rcv; + } else { // append to current buff + tot_byte_rcv += rcv; + std::memcpy(&buff[tot_byte_rcv - rcv], tmp_buff, rcv); + to_be_received -= rcv; } delete tmp_buff; } @@ -137,7 +136,7 @@ int UDPProtocol::connectToClient() { // Creating socket file descriptor if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - std::cerr << "UDP Connection: socket creation failed"<< std::endl; + std::cerr << "UDP Connection: socket creation failed" << std::endl; return -1; } @@ -149,7 +148,7 @@ int UDPProtocol::connectToClient() { // Bind the socket with the server address if (bind(sockfd, (const struct sockaddr*) &srvaddr, sizeof(srvaddr)) < 0) { - std::cerr << "UDP Connection: bind failed"<< std::endl; + std::cerr << "UDP Connection: bind failed" << std::endl; return -1; } @@ -197,7 +196,8 @@ bool UDPProtocol::isConnectedToServer() const { void UDPProtocol::resetPacket(PacketLib::BasePacket &pack, int bytes) { uint8_t *buff = new uint8_t[bytes]; std::memset(buff, 0, bytes); - int toBeReset = std::min(static_cast(pack.getPacketStructureByteSize()), bytes); + int toBeReset = std::min( + static_cast(pack.getPacketStructureByteSize()), bytes); pack.copyToBinaryPointer(buff, toBeReset); delete buff; } @@ -219,54 +219,43 @@ void UDPProtocol::split_ip_port(const std::string &ip_port, port = ip_port.substr(colon_pos + 1); } +using U_B = UDPProtocol::Builder; +U_B::Builder() { + this->reset(); +} +U_B::Builder(std::string ip, int port) { + this->protocol = new UDPProtocol(ip, port); -class UDPProtocol::Builder { -protected: - UDPProtocol *protocol; - -public: - - std::string ip_key{"ip"}; - std::string port_key{"port"}; - - Builder() { - this->reset(); - } - Builder(std::string ip, int port) { - this->protocol = new UDPProtocol(ip, port); - - } - ~Builder() { - delete protocol; - } +} +U_B::~Builder() { + delete protocol; +} - void reset() { - this->protocol = new UDPProtocol(); - } +void U_B::reset() { + this->protocol = new UDPProtocol(); +} - Builder* configFrom(Configurators::BaseConfigurator *conf) { - std::map params = - conf->readConfig(); - if (params.count(ip_key) > 0) - protocol->ip = params[ip_key]; - if (params.count(port_key) > 0) - protocol->port = std::stoi(params[port_key]); - return this; - } +U_B* U_B::configFrom(Configurators::BaseConfigurator *conf) { + std::map params = conf->readConfig(); + if (params.count(ip_key) > 0) + protocol->ip = params[ip_key]; + if (params.count(port_key) > 0) + protocol->port = std::stoi(params[port_key]); + return this; +} - Builder* setIp(std::string ip) { - protocol->ip = ip; - return this; - } +U_B* U_B::setIp(std::string ip) { + protocol->ip = ip; + return this; +} - Builder* setPort(int port) { - protocol->port = port; - return this; - } +U_B* U_B::setPort(int port) { + protocol->port = port; + return this; +} - UDPProtocol* getProtocol() { - UDPProtocol *result = this->protocol; - this->reset(); - return result; - } -}; +UDPProtocol* U_B::getProtocol() { + UDPProtocol *result = this->protocol; + this->reset(); + return result; +} -- GitLab From 4bf4bb2abaf26e92a64cdf2b8c2ef69e3d782ee8 Mon Sep 17 00:00:00 2001 From: astri Date: Sat, 3 Jun 2023 18:48:31 +0200 Subject: [PATCH 4/4] config refactorig ok --- deps/Base-DAQ | 2 +- src/UDP_Protocol.cpp | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index f118b02..2b4e8b0 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit f118b029ff73dc23989c5b648cd6175b968a9a14 +Subproject commit 2b4e8b039ec5da9ddd53720991c1b95f034e86fb diff --git a/src/UDP_Protocol.cpp b/src/UDP_Protocol.cpp index 3f1b351..d164cda 100755 --- a/src/UDP_Protocol.cpp +++ b/src/UDP_Protocol.cpp @@ -13,16 +13,12 @@ using namespace inaf::oasbo::ConnectionProtocols; -UDPProtocol::UDPProtocol() { - UDPProtocol(std::string("127.0.0.1"), 9003); +UDPProtocol::UDPProtocol() : + UDPProtocol(std::string("127.0.0.1"), 9003) { } -UDPProtocol::UDPProtocol(std::string ip, int port) { - this->host = std::string(ip).append(":").append(std::to_string(port)); - this->ip = ip; - this->port = port; - this->srv_sock = -1; - this->cli_sock = -1; +UDPProtocol::UDPProtocol(std::string ip, int port) : + srv_sock(-1), cli_sock(-1), ip(ip), port(port) { memset(&cliaddr, 0, sizeof(cliaddr)); memset(&srvaddr, 0, sizeof(srvaddr)); // Filling server information @@ -236,11 +232,17 @@ void U_B::reset() { } U_B* U_B::configFrom(Configurators::BaseConfigurator *conf) { - std::map params = conf->readConfig(); - if (params.count(ip_key) > 0) - protocol->ip = params[ip_key]; - if (params.count(port_key) > 0) - protocol->port = std::stoi(params[port_key]); + std::string target = std::string("UDPProtocol"); + conf->readConfigFromSource(target); + std::map params = conf->getConfig(); + + std::string key = target+"_"+ip_key; + if (params.count(key) > 0) + protocol->ip = params[key]; + + key = target+"_"+port_key; + if (params.count(key) > 0) + protocol->port = std::stoi(params[key]); return this; } -- GitLab