new socket overload

This commit is contained in:
Erik Frojdh 2019-03-19 17:49:20 +01:00
parent 6759b2eeb8
commit 4e56107015
4 changed files with 183 additions and 171 deletions

File diff suppressed because it is too large Load Diff

View File

@ -18,4 +18,16 @@ class ClientSocket : public DataSocket {
bool isReceiver;
};
class ReceiverSocket : public ClientSocket {
public:
ReceiverSocket(const std::string &hostname, uint16_t port_number)
: ClientSocket(true, hostname, port_number){};
};
class DetectorSocket : public ClientSocket {
public:
DetectorSocket(const std::string &hostname, uint16_t port_number)
: ClientSocket(false, hostname, port_number){};
};
}; //namespace sls

View File

@ -42,6 +42,18 @@ public:
};
struct DetectorError : public RuntimeError {
public:
DetectorError(std::string msg):RuntimeError(msg) {}
};
struct ReceiverError : public RuntimeError {
public:
ReceiverError(std::string msg):RuntimeError(msg) {}
};
}

View File

@ -1,13 +1,13 @@
#include "ClientSocket.h"
#include "logger.h"
#include "sls_detector_defs.h"
#include "sls_detector_exceptions.h"
#include <arpa/inet.h>
#include <cassert>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include "sls_detector_defs.h"
#include "sls_detector_exceptions.h"
#include "logger.h"
#include <unistd.h>
namespace sls {
ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)), isReceiver(isRx) {
@ -19,8 +19,8 @@ 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 ERROR: decode host:" + host + " on port " + std::to_string(port)+ "\n";
throw std::runtime_error(msg);
std::string msg = "ClientSocket ERROR: 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?
@ -32,7 +32,7 @@ ClientSocket::ClientSocket(const bool isRx, const std::string &host, uint16_t po
if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
freeaddrinfo(result);
std::string msg = "ClientSocket ERROR: cannot connect to host:" + host + " on port " + std::to_string(port)+ "\n";
std::string msg = "ClientSocket ERROR: cannot connect to host:" + host + " on port " + std::to_string(port) + "\n";
FILE_LOG(logERROR) << msg;
throw SocketError(msg);
}
@ -61,10 +61,18 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
// 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???
if (isReceiver) {
throw ReceiverError(mess);
} else {
throw DetectorError(mess);
}
}
// get retval
if (!unrecognizedFunction)
receiveData(retval, retval_size);
receiveData(retval, retval_size);
}
}; //namespace sls