From 94c4e4c352d762444aab8b2ae68db7e8d167fe17 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 28 Mar 2019 15:48:26 +0100 Subject: [PATCH] new constuctor --- CMakeLists.txt | 8 +++ cmake/project_version.cmake | 48 ++++++------- integrationTests/src/a.cpp | 54 ++++++++++++-- slsSupportLib/include/ClientInterface.h | 3 - slsSupportLib/include/ClientSocket.h | 12 ++-- slsSupportLib/include/DataSocket.h | 3 + slsSupportLib/src/ClientInterface.cpp | 26 +++---- slsSupportLib/src/ClientSocket.cpp | 13 ++++ slsSupportLib/src/DataSocket.cpp | 96 +++++++++++++++---------- 9 files changed, 172 insertions(+), 91 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d11b8a104..4ef5db153 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 2.8) # cmake_minimum_required(VERSION 3.5) project(slsDetectorPackage) +set(PROJECT_VERSION 5.0.0) +# set(PACKAGE_VERSION ${}) + include(cmake/project_version.cmake) # Include additional modules that are used unconditionally @@ -41,6 +44,11 @@ option(SLS_USE_PYTHON "Python bindings" OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE) +endif() + #Testing for minimum version for compilers if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # clang does not support -Wno-misleading-indentation diff --git a/cmake/project_version.cmake b/cmake/project_version.cmake index 6ac61def1..e7a2bd8ec 100644 --- a/cmake/project_version.cmake +++ b/cmake/project_version.cmake @@ -57,28 +57,28 @@ if (GIT_CMD AND NOT "${GIT_TOPLEVEL}" STREQUAL "") ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) #message(STATUS "GIT_DESCRIBE: " ${GIT_DESCRIBE}) - if (GIT_DESCRIBE) - string(REGEX REPLACE "v?([0-9.]+).*" "\\1" GIT_VERSION ${GIT_DESCRIBE}) - message(STATUS "GIT_VERSION: " ${GIT_VERSION}) + # if (GIT_DESCRIBE) + # string(REGEX REPLACE "v?([0-9.]+).*" "\\1" GIT_VERSION ${GIT_DESCRIBE}) + # message(STATUS "GIT_VERSION: " ${GIT_VERSION}) - # as package version we use the full version from git describe: 1.7.1+7+ge324c81 - if (GIT_DESCRIBE MATCHES ".*-g.*") - # convert a git describe string to usable debian version, e.g. v1.7.1-7-ge324c81 to 1.7.1+7+ge324c81 - string(REGEX REPLACE "v?([0-9]*.[0-9.]*).*-([0-9]*)-([a-g0-9]*)" "\\1+\\2+\\3" GIT_FULL_VERSION ${GIT_DESCRIBE}) - else() - # current HEAD is git tag (i.e. releaase), directly use the version - set(GIT_FULL_VERSION ${GIT_VERSION}) - endif() - else () - # no (suitable) tag found - set(GIT_VERSION "0.0.0") - # get number of commits in repo - execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD - WORKING_DIRECTORY ${GIT_TOPLEVEL} - OUTPUT_VARIABLE GIT_COMMIT_COUNT - OUTPUT_STRIP_TRAILING_WHITESPACE) - set(GIT_FULL_VERSION 0.0.0+${GIT_COMMIT_COUNT}+g${GIT_SHA1}) - endif () + # # as package version we use the full version from git describe: 1.7.1+7+ge324c81 + # if (GIT_DESCRIBE MATCHES ".*-g.*") + # # convert a git describe string to usable debian version, e.g. v1.7.1-7-ge324c81 to 1.7.1+7+ge324c81 + # string(REGEX REPLACE "v?([0-9]*.[0-9.]*).*-([0-9]*)-([a-g0-9]*)" "\\1+\\2+\\3" GIT_FULL_VERSION ${GIT_DESCRIBE}) + # else() + # # current HEAD is git tag (i.e. releaase), directly use the version + # set(GIT_FULL_VERSION ${GIT_VERSION}) + # endif() + # else () + # # no (suitable) tag found + # set(GIT_VERSION "0.0.0") + # # get number of commits in repo + # execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD + # WORKING_DIRECTORY ${GIT_TOPLEVEL} + # OUTPUT_VARIABLE GIT_COMMIT_COUNT + # OUTPUT_STRIP_TRAILING_WHITESPACE) + # set(GIT_FULL_VERSION 0.0.0+${GIT_COMMIT_COUNT}+g${GIT_SHA1}) + # endif () endif () # get version from package.xml if it exists @@ -104,10 +104,10 @@ if (NOT PROJECT_VERSION) set(PROJECT_VERSION "0.0.0") endif () endif () -if (NOT PACKAGE_VERSION) - message(WARNING "PACKAGE_VERSION not set! Falling back to (${PROJECT_VERSION})") +# if (NOT PACKAGE_VERSION) + # message(WARNING "PACKAGE_VERSION not set! Falling back to (${PROJECT_VERSION})") set(PACKAGE_VERSION ${PROJECT_VERSION}) -endif () +# endif () # warn if versions don't match if (GIT_VERSION AND NOT GIT_VERSION MATCHES ${PROJECT_VERSION}) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index 5e5fd6fcc..b37efdc19 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -11,6 +11,12 @@ #include #include +#include +#include +#include +#include +#include + #define VERBOSE using sls::RuntimeError; @@ -21,13 +27,47 @@ using sls::DetectorError; int main() { - const std::string hostname = "beb083"; - auto type = slsDetector::getTypeFromDetector(hostname); - slsDetector d(type); - d.setHostname(hostname); - d.setOnline(true); - std::cout << "hostname: " << d.getHostname() << '\n'; - d.setThresholdTemperature(50); + std::string hostname; + std::cout << "Enter hostname: "; + std::cin >> hostname; + + struct addrinfo hints, *result; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_CANONNAME; + + struct sockaddr_in serverAddr {}; + // std::cout << "sizeof(result):" << sizeof(hints) << '\n'; + // std::cout << "sizeof(serverAddr):" << sizeof(serverAddr) << '\n'; + + int port = 1952; + + if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { + std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + + std::to_string(port) + "\n"; + throw 5; + } + + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(port); + memcpy(&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + sizeof(in_addr_t)); + freeaddrinfo(result); + + char address[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &serverAddr.sin_addr, address, INET_ADDRSTRLEN); + std::cout << "ip of host is: " << address << '\n'; + + sls::ClientSocket(false, serverAddr); + + // const std::string hostname = "beb083"; + // auto type = slsDetector::getTypeFromDetector(hostname); + // slsDetector d(type); + // d.setHostname(hostname); + // d.setOnline(true); + // std::cout << "hostname: " << d.getHostname() << '\n'; + // d.setThresholdTemperature(50); // try{ // d.setThresholdTemperature(50); // }catch(const DetectorError &e){ diff --git a/slsSupportLib/include/ClientInterface.h b/slsSupportLib/include/ClientInterface.h index 96945831b..379d10bd6 100644 --- a/slsSupportLib/include/ClientInterface.h +++ b/slsSupportLib/include/ClientInterface.h @@ -66,9 +66,6 @@ private: */ sls::ClientSocket* socket_; - /** index for client debugging purposes */ - int index; - }; diff --git a/slsSupportLib/include/ClientSocket.h b/slsSupportLib/include/ClientSocket.h index df562347b..76d680699 100644 --- a/slsSupportLib/include/ClientSocket.h +++ b/slsSupportLib/include/ClientSocket.h @@ -10,7 +10,9 @@ namespace sls { class ClientSocket : public DataSocket { public: ClientSocket(const bool isRx, const std::string &hostname, uint16_t port_number); - int sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, size_t retval_size); + ClientSocket(const bool isRx, struct sockaddr_in addr); + int sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, + size_t retval_size); private: void readReply(int &ret, void *retval, size_t retval_size); @@ -19,15 +21,17 @@ class ClientSocket : public DataSocket { }; class ReceiverSocket : public ClientSocket { - public: + public: ReceiverSocket(const std::string &hostname, uint16_t port_number) : ClientSocket(true, hostname, port_number){}; + ReceiverSocket(struct sockaddr_in addr) : ClientSocket(true, addr){}; }; class DetectorSocket : public ClientSocket { - public: + public: DetectorSocket(const std::string &hostname, uint16_t port_number) : ClientSocket(false, hostname, port_number){}; + DetectorSocket(struct sockaddr_in addr) : ClientSocket(false, addr){}; }; -}; //namespace sls +}; // namespace sls diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index fc88129d7..95b0c7bb6 100644 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace sls { class DataSocket { @@ -29,4 +30,6 @@ class DataSocket { int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res); int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize); +struct ::sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname); + }; // namespace sls diff --git a/slsSupportLib/src/ClientInterface.cpp b/slsSupportLib/src/ClientInterface.cpp index c50f3f8f3..a09ff6002 100644 --- a/slsSupportLib/src/ClientInterface.cpp +++ b/slsSupportLib/src/ClientInterface.cpp @@ -1,10 +1,9 @@ #include "ClientInterface.h" #include "ClientSocket.h" -ClientInterface::ClientInterface(sls::ClientSocket* socket, int n): socket_(socket), - index(n){} +ClientInterface::ClientInterface(sls::ClientSocket *socket, int n) : socket_(socket){} -void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int sizeOfRetval) { +void ClientInterface::Client_Receive(int &ret, char *mess, void *retval, int sizeOfRetval) { // get result of operation socket_->receiveData(reinterpret_cast(&ret), sizeof(ret)); @@ -12,35 +11,32 @@ void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int siz if (ret == FAIL) { bool created = false; // allocate mess if null - if (!mess){ + if (!mess) { created = true; mess = new char[MAX_STR_LENGTH]; memset(mess, 0, MAX_STR_LENGTH); } // get error message - socket_->receiveData(mess,MAX_STR_LENGTH); + socket_->receiveData(mess, MAX_STR_LENGTH); // cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess); // unrecognized function, do not ask for retval - if(strstr(mess,"Unrecognized Function") != nullptr) + if (strstr(mess, "Unrecognized Function") != nullptr) unrecognizedFunction = true; // delete allocated mess if (created) - delete [] mess; + delete[] mess; } // get retval if (!unrecognizedFunction) - socket_->receiveData( reinterpret_cast(retval), sizeOfRetval); + socket_->receiveData(reinterpret_cast(retval), sizeOfRetval); } - -int ClientInterface::Client_Send(int fnum, - void* args, int sizeOfArgs, - void* retval, int sizeOfRetval, - char* mess) { +int ClientInterface::Client_Send(int fnum, void *args, int sizeOfArgs, void *retval, + int sizeOfRetval, char *mess) { int ret = FAIL; - socket_->sendData(reinterpret_cast(&fnum),sizeof(fnum)); + socket_->sendData(reinterpret_cast(&fnum), sizeof(fnum)); socket_->sendData(reinterpret_cast(args), sizeOfArgs); Client_Receive(ret, mess, retval, sizeOfRetval); - return ret; + return ret; } \ No newline at end of file diff --git a/slsSupportLib/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index ed30597c3..99f7a00ea 100644 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -42,6 +42,19 @@ ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t po freeaddrinfo(result); } +ClientSocket::ClientSocket(const bool isRx, struct sockaddr_in addr) + : DataSocket(socket(AF_INET, SOCK_STREAM, 0)), isReceiver(isRx) { + + if (::connect(getSocketId(), (struct sockaddr *)&addr, sizeof(addr)) != 0) { + char address[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &addr.sin_addr, address, INET_ADDRSTRLEN); + const std::string name{(isReceiver ? "Receiver" : "Detector")}; + std::string msg = "ClientSocket: Cannot connect to " + name + ":" + address + " on port " + + std::to_string(addr.sin_port) + "\n"; + throw SocketError(msg); + } +} + int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, size_t retval_size) { int ret = slsDetectorDefs::FAIL; diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index 4815057ab..651b507b0 100644 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -1,13 +1,15 @@ #include "DataSocket.h" #include "logger.h" +#include "sls_detector_exceptions.h" #include #include #include #include -#include +#include #include #include -#include +#include + namespace sls { @@ -20,18 +22,14 @@ DataSocket::~DataSocket() { try { close(); } catch (...) { - //pass + // pass } } } -void DataSocket::swap(DataSocket &other) noexcept { - std::swap(socketId_, other.socketId_); -} +void DataSocket::swap(DataSocket &other) noexcept { std::swap(socketId_, other.socketId_); } -DataSocket::DataSocket(DataSocket &&move) noexcept { - move.swap(*this); -} +DataSocket::DataSocket(DataSocket &&move) noexcept { move.swap(*this); } DataSocket &DataSocket::operator=(DataSocket &&move) noexcept { move.swap(*this); return *this; @@ -41,7 +39,8 @@ size_t DataSocket::receiveData(void *buffer, size_t size) { // std::cout << "Sending\n"; size_t dataRead = 0; while (dataRead < size) { - dataRead += read(getSocketId(), reinterpret_cast(buffer) + dataRead, size - dataRead); + dataRead += + read(getSocketId(), reinterpret_cast(buffer) + dataRead, size - dataRead); } return dataRead; } @@ -50,7 +49,8 @@ size_t DataSocket::sendData(void *buffer, size_t size) { // std::cout << "Receiving\n"; size_t dataSent = 0; while (dataSent < size) { - dataSent += write(getSocketId(), reinterpret_cast(buffer) + dataSent, size - dataSent); + dataSent += + write(getSocketId(), reinterpret_cast(buffer) + dataSent, size - dataSent); } return dataSent; } @@ -62,17 +62,15 @@ int DataSocket::setTimeOut(int t_seconds) { struct timeval t; t.tv_sec = 0; t.tv_usec = 0; - //Receive timeout indefinet - if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, - &t, sizeof(struct timeval)) < 0) { + // Receive timeout indefinet + if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval)) < 0) { FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0; } t.tv_sec = t_seconds; t.tv_usec = 0; - //Sending timeout in seconds - if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, - &t, sizeof(struct timeval)) < 0) { + // Sending timeout in seconds + if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(struct timeval)) < 0) { FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds; } return 0; @@ -86,6 +84,26 @@ void DataSocket::close() { } } +struct sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname) { + struct addrinfo hints, *result; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_CANONNAME; + + struct sockaddr_in serverAddr {}; + if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { + freeaddrinfo(result); + std::string msg = "ClientSocket cannot decode host:" + hostname + "\n"; + throw SocketError(msg); + } + serverAddr.sin_family = AF_INET; + memcpy((char *)&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + sizeof(in_addr_t)); + freeaddrinfo(result); + return serverAddr; +} + int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res) { // criteria in selecting socket address structures returned by res struct ::addrinfo hints; @@ -95,11 +113,13 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin // get host info into res int errcode = getaddrinfo(hostname, NULL, &hints, res); if (errcode != 0) { - FILE_LOG(logERROR) << "Could not convert hostname (" << hostname << ") to internet address (zmq):" << gai_strerror(errcode); + FILE_LOG(logERROR) << "Could not convert hostname (" << hostname + << ") to internet address (zmq):" << gai_strerror(errcode); } else { if (*res == NULL) { - FILE_LOG(logERROR) << "Could not converthostname (" << hostname << ") to internet address (zmq):" - "gettaddrinfo returned null"; + FILE_LOG(logERROR) << "Could not converthostname (" << hostname + << ") to internet address (zmq):" + "gettaddrinfo returned null"; } else { return 0; } @@ -108,23 +128,23 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin return 1; }; - - /** - * Convert Internet Address structure pointer to ip string (char*) - * Clears the internet address structure as well - * @param res pointer to internet address structure - * @param ip pointer to char array to store result in - * @param ipsize size available in ip buffer - * @return 1 for fail, 0 for success - */ - // Do not make this static (for multi threading environment) - int ConvertInternetAddresstoIpString (struct ::addrinfo *res, char* ip, const int ipsize) { - if (inet_ntop (res->ai_family, &((struct sockaddr_in *) res->ai_addr)->sin_addr, ip, ipsize) != NULL) { - ::freeaddrinfo(res); - return 0; - } - FILE_LOG(logERROR) << "Could not convert internet address to ip string"; - return 1; - } +/** + * Convert Internet Address structure pointer to ip string (char*) + * Clears the internet address structure as well + * @param res pointer to internet address structure + * @param ip pointer to char array to store result in + * @param ipsize size available in ip buffer + * @return 1 for fail, 0 for success + */ +// Do not make this static (for multi threading environment) +int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize) { + if (inet_ntop(res->ai_family, &((struct sockaddr_in *)res->ai_addr)->sin_addr, ip, ipsize) != + NULL) { + ::freeaddrinfo(res); + return 0; + } + FILE_LOG(logERROR) << "Could not convert internet address to ip string"; + return 1; +} } // namespace sls