From c16f9b8c307932427ede9acc54d421c1f77b3c9d Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 20 Mar 2019 17:41:46 +0100 Subject: [PATCH] specify detector or receiver in error --- slsSupportLib/src/ClientSocket.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/slsSupportLib/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index 89c817129..ed30597c3 100644 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -10,7 +10,8 @@ #include namespace sls { -ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)), isReceiver(isRx) { +ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t port) + : DataSocket(socket(AF_INET, SOCK_STREAM, 0)), isReceiver(isRx) { struct addrinfo hints, *result; memset(&hints, 0, sizeof(hints)); @@ -19,27 +20,30 @@ ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t po hints.ai_flags |= AI_CANONNAME; if (getaddrinfo(host.c_str(), NULL, &hints, &result) != 0) { - std::string msg = "ClientSocket cannot decode host:" + host + " on port " + std::to_string(port) + "\n"; + std::string msg = + "ClientSocket cannot decode host:" + host + " on port " + std::to_string(port) + "\n"; throw SocketError(msg); } - //TODO! Erik, results could have multiple entries do we need to loop through them? + // TODO! Erik, results could have multiple entries do we need to loop through them? // struct sockaddr_in serverAddr {}; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); - memcpy((char *)&serverAddr.sin_addr.s_addr, - &((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t)); + memcpy((char *)&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + sizeof(in_addr_t)); if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) { freeaddrinfo(result); - std::string msg = "ClientSocket: cannot connect to host:" + host + " on port " + std::to_string(port) + "\n"; - FILE_LOG(logERROR) << msg; + const std::string name{(isReceiver ? "Receiver" : "Detector")}; + std::string msg = "ClientSocket: Cannot connect to " + name + ":" + host + " on port " + + std::to_string(port) + "\n"; throw SocketError(msg); } freeaddrinfo(result); } -int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, size_t retval_size) { +int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, + size_t retval_size) { int ret = slsDetectorDefs::FAIL; sendData(&fnum, sizeof(fnum)); sendData(args, args_size); @@ -53,18 +57,18 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { bool unrecognizedFunction = false; if (ret == slsDetectorDefs::FAIL) { char mess[MAX_STR_LENGTH]{}; - //get error message + // get error message receiveData(mess, sizeof(mess)); // cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess); cprintf(RED, "%s returned error: %s", (isReceiver ? "Receiver" : "Detector"), mess); - std::cout << "\n"; //needed to reset the color. + std::cout << "\n"; // needed to reset the color. // unrecognized function, do not ask for retval if (strstr(mess, "Unrecognized Function") != nullptr) unrecognizedFunction = true; - //Do we need to know hostname here? - //In that case save it??? + // Do we need to know hostname here? + // In that case save it??? if (isReceiver) { throw ReceiverError(mess); } else { @@ -76,4 +80,4 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { receiveData(retval, retval_size); } -}; //namespace sls +}; // namespace sls