new constuctor

This commit is contained in:
Erik Frojdh
2019-03-28 15:48:26 +01:00
parent 5a4122ae7c
commit 94c4e4c352
9 changed files with 172 additions and 91 deletions

View File

@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
# cmake_minimum_required(VERSION 3.5) # cmake_minimum_required(VERSION 3.5)
project(slsDetectorPackage) project(slsDetectorPackage)
set(PROJECT_VERSION 5.0.0)
# set(PACKAGE_VERSION ${})
include(cmake/project_version.cmake) include(cmake/project_version.cmake)
# Include additional modules that are used unconditionally # Include additional modules that are used unconditionally
@ -41,6 +44,11 @@ option(SLS_USE_PYTHON "Python bindings" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 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 #Testing for minimum version for compilers
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# clang does not support -Wno-misleading-indentation # clang does not support -Wno-misleading-indentation

View File

@ -57,28 +57,28 @@ if (GIT_CMD AND NOT "${GIT_TOPLEVEL}" STREQUAL "")
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
#message(STATUS "GIT_DESCRIBE: " ${GIT_DESCRIBE}) #message(STATUS "GIT_DESCRIBE: " ${GIT_DESCRIBE})
if (GIT_DESCRIBE) # if (GIT_DESCRIBE)
string(REGEX REPLACE "v?([0-9.]+).*" "\\1" GIT_VERSION ${GIT_DESCRIBE}) # string(REGEX REPLACE "v?([0-9.]+).*" "\\1" GIT_VERSION ${GIT_DESCRIBE})
message(STATUS "GIT_VERSION: " ${GIT_VERSION}) # message(STATUS "GIT_VERSION: " ${GIT_VERSION})
# as package version we use the full version from git describe: 1.7.1+7+ge324c81 # # as package version we use the full version from git describe: 1.7.1+7+ge324c81
if (GIT_DESCRIBE MATCHES ".*-g.*") # 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 # # 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}) # string(REGEX REPLACE "v?([0-9]*.[0-9.]*).*-([0-9]*)-([a-g0-9]*)" "\\1+\\2+\\3" GIT_FULL_VERSION ${GIT_DESCRIBE})
else() # else()
# current HEAD is git tag (i.e. releaase), directly use the version # # current HEAD is git tag (i.e. releaase), directly use the version
set(GIT_FULL_VERSION ${GIT_VERSION}) # set(GIT_FULL_VERSION ${GIT_VERSION})
endif() # endif()
else () # else ()
# no (suitable) tag found # # no (suitable) tag found
set(GIT_VERSION "0.0.0") # set(GIT_VERSION "0.0.0")
# get number of commits in repo # # get number of commits in repo
execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD # execute_process(COMMAND ${GIT_CMD} rev-list --count HEAD
WORKING_DIRECTORY ${GIT_TOPLEVEL} # WORKING_DIRECTORY ${GIT_TOPLEVEL}
OUTPUT_VARIABLE GIT_COMMIT_COUNT # OUTPUT_VARIABLE GIT_COMMIT_COUNT
OUTPUT_STRIP_TRAILING_WHITESPACE) # OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GIT_FULL_VERSION 0.0.0+${GIT_COMMIT_COUNT}+g${GIT_SHA1}) # set(GIT_FULL_VERSION 0.0.0+${GIT_COMMIT_COUNT}+g${GIT_SHA1})
endif () # endif ()
endif () endif ()
# get version from package.xml if it exists # get version from package.xml if it exists
@ -104,10 +104,10 @@ if (NOT PROJECT_VERSION)
set(PROJECT_VERSION "0.0.0") set(PROJECT_VERSION "0.0.0")
endif () endif ()
endif () endif ()
if (NOT PACKAGE_VERSION) # if (NOT PACKAGE_VERSION)
message(WARNING "PACKAGE_VERSION not set! Falling back to (${PROJECT_VERSION})") # message(WARNING "PACKAGE_VERSION not set! Falling back to (${PROJECT_VERSION})")
set(PACKAGE_VERSION ${PROJECT_VERSION}) set(PACKAGE_VERSION ${PROJECT_VERSION})
endif () # endif ()
# warn if versions don't match # warn if versions don't match
if (GIT_VERSION AND NOT GIT_VERSION MATCHES ${PROJECT_VERSION}) if (GIT_VERSION AND NOT GIT_VERSION MATCHES ${PROJECT_VERSION})

View File

@ -11,6 +11,12 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <netdb.h>
#include <string>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#define VERBOSE #define VERBOSE
using sls::RuntimeError; using sls::RuntimeError;
@ -21,13 +27,47 @@ using sls::DetectorError;
int main() { int main() {
const std::string hostname = "beb083"; std::string hostname;
auto type = slsDetector::getTypeFromDetector(hostname); std::cout << "Enter hostname: ";
slsDetector d(type); std::cin >> hostname;
d.setHostname(hostname);
d.setOnline(true); struct addrinfo hints, *result;
std::cout << "hostname: " << d.getHostname() << '\n'; memset(&hints, 0, sizeof(hints));
d.setThresholdTemperature(50); 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{ // try{
// d.setThresholdTemperature(50); // d.setThresholdTemperature(50);
// }catch(const DetectorError &e){ // }catch(const DetectorError &e){

View File

@ -66,9 +66,6 @@ private:
*/ */
sls::ClientSocket* socket_; sls::ClientSocket* socket_;
/** index for client debugging purposes */
int index;
}; };

View File

@ -10,7 +10,9 @@ namespace sls {
class ClientSocket : public DataSocket { class ClientSocket : public DataSocket {
public: public:
ClientSocket(const bool isRx, const std::string &hostname, uint16_t port_number); 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: private:
void readReply(int &ret, void *retval, size_t retval_size); void readReply(int &ret, void *retval, size_t retval_size);
@ -19,15 +21,17 @@ class ClientSocket : public DataSocket {
}; };
class ReceiverSocket : public ClientSocket { class ReceiverSocket : public ClientSocket {
public: public:
ReceiverSocket(const std::string &hostname, uint16_t port_number) ReceiverSocket(const std::string &hostname, uint16_t port_number)
: ClientSocket(true, hostname, port_number){}; : ClientSocket(true, hostname, port_number){};
ReceiverSocket(struct sockaddr_in addr) : ClientSocket(true, addr){};
}; };
class DetectorSocket : public ClientSocket { class DetectorSocket : public ClientSocket {
public: public:
DetectorSocket(const std::string &hostname, uint16_t port_number) DetectorSocket(const std::string &hostname, uint16_t port_number)
: ClientSocket(false, hostname, port_number){}; : ClientSocket(false, hostname, port_number){};
DetectorSocket(struct sockaddr_in addr) : ClientSocket(false, addr){};
}; };
}; //namespace sls }; // namespace sls

View File

@ -3,6 +3,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <netdb.h> #include <netdb.h>
#include <string>
namespace sls { namespace sls {
class DataSocket { class DataSocket {
@ -29,4 +30,6 @@ class DataSocket {
int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res); int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res);
int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize); int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize);
struct ::sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname);
}; // namespace sls }; // namespace sls

View File

@ -1,10 +1,9 @@
#include "ClientInterface.h" #include "ClientInterface.h"
#include "ClientSocket.h" #include "ClientSocket.h"
ClientInterface::ClientInterface(sls::ClientSocket* socket, int n): socket_(socket), ClientInterface::ClientInterface(sls::ClientSocket *socket, int n) : socket_(socket){}
index(n){}
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 // get result of operation
socket_->receiveData(reinterpret_cast<char *>(&ret), sizeof(ret)); socket_->receiveData(reinterpret_cast<char *>(&ret), sizeof(ret));
@ -12,35 +11,32 @@ void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int siz
if (ret == FAIL) { if (ret == FAIL) {
bool created = false; bool created = false;
// allocate mess if null // allocate mess if null
if (!mess){ if (!mess) {
created = true; created = true;
mess = new char[MAX_STR_LENGTH]; mess = new char[MAX_STR_LENGTH];
memset(mess, 0, MAX_STR_LENGTH); memset(mess, 0, MAX_STR_LENGTH);
} }
// get error message // 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); // cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess);
// unrecognized function, do not ask for retval // unrecognized function, do not ask for retval
if(strstr(mess,"Unrecognized Function") != nullptr) if (strstr(mess, "Unrecognized Function") != nullptr)
unrecognizedFunction = true; unrecognizedFunction = true;
// delete allocated mess // delete allocated mess
if (created) if (created)
delete [] mess; delete[] mess;
} }
// get retval // get retval
if (!unrecognizedFunction) if (!unrecognizedFunction)
socket_->receiveData( reinterpret_cast<char *>(retval), sizeOfRetval); socket_->receiveData(reinterpret_cast<char *>(retval), sizeOfRetval);
} }
int ClientInterface::Client_Send(int fnum, void *args, int sizeOfArgs, void *retval,
int ClientInterface::Client_Send(int fnum, int sizeOfRetval, char *mess) {
void* args, int sizeOfArgs,
void* retval, int sizeOfRetval,
char* mess) {
int ret = FAIL; int ret = FAIL;
socket_->sendData(reinterpret_cast<char *>(&fnum),sizeof(fnum)); socket_->sendData(reinterpret_cast<char *>(&fnum), sizeof(fnum));
socket_->sendData(reinterpret_cast<char *>(args), sizeOfArgs); socket_->sendData(reinterpret_cast<char *>(args), sizeOfArgs);
Client_Receive(ret, mess, retval, sizeOfRetval); Client_Receive(ret, mess, retval, sizeOfRetval);
return ret; return ret;
} }

View File

@ -42,6 +42,19 @@ ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t po
freeaddrinfo(result); 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, int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval,
size_t retval_size) { size_t retval_size) {
int ret = slsDetectorDefs::FAIL; int ret = slsDetectorDefs::FAIL;

View File

@ -1,13 +1,15 @@
#include "DataSocket.h" #include "DataSocket.h"
#include "logger.h" #include "logger.h"
#include "sls_detector_exceptions.h"
#include <algorithm> #include <algorithm>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <unistd.h> #include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netdb.h> #include <unistd.h>
namespace sls { namespace sls {
@ -20,18 +22,14 @@ DataSocket::~DataSocket() {
try { try {
close(); close();
} catch (...) { } catch (...) {
//pass // pass
} }
} }
} }
void DataSocket::swap(DataSocket &other) noexcept { void DataSocket::swap(DataSocket &other) noexcept { std::swap(socketId_, other.socketId_); }
std::swap(socketId_, other.socketId_);
}
DataSocket::DataSocket(DataSocket &&move) noexcept { DataSocket::DataSocket(DataSocket &&move) noexcept { move.swap(*this); }
move.swap(*this);
}
DataSocket &DataSocket::operator=(DataSocket &&move) noexcept { DataSocket &DataSocket::operator=(DataSocket &&move) noexcept {
move.swap(*this); move.swap(*this);
return *this; return *this;
@ -41,7 +39,8 @@ size_t DataSocket::receiveData(void *buffer, size_t size) {
// std::cout << "Sending\n"; // std::cout << "Sending\n";
size_t dataRead = 0; size_t dataRead = 0;
while (dataRead < size) { while (dataRead < size) {
dataRead += read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead, size - dataRead); dataRead +=
read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead, size - dataRead);
} }
return dataRead; return dataRead;
} }
@ -50,7 +49,8 @@ size_t DataSocket::sendData(void *buffer, size_t size) {
// std::cout << "Receiving\n"; // std::cout << "Receiving\n";
size_t dataSent = 0; size_t dataSent = 0;
while (dataSent < size) { while (dataSent < size) {
dataSent += write(getSocketId(), reinterpret_cast<char *>(buffer) + dataSent, size - dataSent); dataSent +=
write(getSocketId(), reinterpret_cast<char *>(buffer) + dataSent, size - dataSent);
} }
return dataSent; return dataSent;
} }
@ -62,17 +62,15 @@ int DataSocket::setTimeOut(int t_seconds) {
struct timeval t; struct timeval t;
t.tv_sec = 0; t.tv_sec = 0;
t.tv_usec = 0; t.tv_usec = 0;
//Receive timeout indefinet // Receive timeout indefinet
if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval)) < 0) {
&t, sizeof(struct timeval)) < 0) {
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0; FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
} }
t.tv_sec = t_seconds; t.tv_sec = t_seconds;
t.tv_usec = 0; t.tv_usec = 0;
//Sending timeout in seconds // Sending timeout in seconds
if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(struct timeval)) < 0) {
&t, sizeof(struct timeval)) < 0) {
FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds; FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds;
} }
return 0; 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) { int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res) {
// criteria in selecting socket address structures returned by res // criteria in selecting socket address structures returned by res
struct ::addrinfo hints; struct ::addrinfo hints;
@ -95,11 +113,13 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin
// get host info into res // get host info into res
int errcode = getaddrinfo(hostname, NULL, &hints, res); int errcode = getaddrinfo(hostname, NULL, &hints, res);
if (errcode != 0) { 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 { } else {
if (*res == NULL) { if (*res == NULL) {
FILE_LOG(logERROR) << "Could not converthostname (" << hostname << ") to internet address (zmq):" FILE_LOG(logERROR) << "Could not converthostname (" << hostname
"gettaddrinfo returned null"; << ") to internet address (zmq):"
"gettaddrinfo returned null";
} else { } else {
return 0; return 0;
} }
@ -108,23 +128,23 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin
return 1; return 1;
}; };
/**
/** * Convert Internet Address structure pointer to ip string (char*)
* Convert Internet Address structure pointer to ip string (char*) * Clears the internet address structure as well
* Clears the internet address structure as well * @param res pointer to internet address structure
* @param res pointer to internet address structure * @param ip pointer to char array to store result in
* @param ip pointer to char array to store result in * @param ipsize size available in ip buffer
* @param ipsize size available in ip buffer * @return 1 for fail, 0 for success
* @return 1 for fail, 0 for success */
*/ // Do not make this static (for multi threading environment)
// Do not make this static (for multi threading environment) int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize) {
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) !=
if (inet_ntop (res->ai_family, &((struct sockaddr_in *) res->ai_addr)->sin_addr, ip, ipsize) != NULL) { NULL) {
::freeaddrinfo(res); ::freeaddrinfo(res);
return 0; return 0;
} }
FILE_LOG(logERROR) << "Could not convert internet address to ip string"; FILE_LOG(logERROR) << "Could not convert internet address to ip string";
return 1; return 1;
} }
} // namespace sls } // namespace sls