exceptions TCP (#31)

* WIP

* first test

* format

* test frames

* also 0:

* WIP

* WIP

* first test

* format

* test frames

* also 0:

* test and less local

* pass ret by value

* level

* WIP

* WIP

* cleaning up interface

* removed ref

* another

* updated api version

* cleanup

* cleanup

* WIP

* fixes

* fixed tests
This commit is contained in:
Erik Fröjdh 2019-06-05 15:25:18 +02:00 committed by Dhanya Thattil
parent a6144f658e
commit 1db7521cfa
14 changed files with 1048 additions and 1752 deletions

View File

@ -788,6 +788,10 @@ class multiSlsDetector : public virtual slsDetectorDefs {
*/
int getDataBytes(int detPos = -1);
/**
* Returns the number of detectors in the multi structure*/
size_t size() const{ return detectors.size();}
/**
* Set/get dacs value
* @param val value (in V)

View File

@ -1,12 +1,13 @@
#include "versionAPI.h"
#include "multiSlsDetectorClient.h"
#include "versionAPI.h"
#include <cstdlib>
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
if (!(strcmp(argv[i], "--version")) || !(strcmp(argv[i], "-v"))) {
int64_t tempval = APILIB;
std::cout << argv[0] << " " << GITBRANCH << " (0x" << std::hex << tempval << ")" << std::endl;
std::cout << argv[0] << " " << GITBRANCH << " (0x" << std::hex
<< tempval << ")" << std::endl;
return 0;
}
}
@ -27,5 +28,8 @@ int main(int argc, char *argv[]) {
int action = slsDetectorDefs::HELP_ACTION;
#endif
multiSlsDetectorClient(argc, argv, action);
try {
multiSlsDetectorClient(argc, argv, action);
} catch (const sls::RuntimeError &e) {
}
}

View File

@ -24,7 +24,7 @@ TEST_CASE("rx_fifodepth", "[.cmd]") {
REQUIRE(oss.str() == "0:rx_fifodepth 100\n");
}
TEST_CASE("frames", "[.cmd]"){
TEST_CASE("frames", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("frames 1000", PUT, nullptr, oss);
REQUIRE(oss.str() == "frames 1000\n");
@ -36,5 +36,239 @@ TEST_CASE("frames", "[.cmd]"){
oss = std::ostringstream{};
multiSlsDetectorClient("frames 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "frames 1\n");
}
}
TEST_CASE("receiver", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("receiver start", PUT, nullptr, oss);
REQUIRE(oss.str() == "receiver running\n");
oss = std::ostringstream{};
multiSlsDetectorClient("receiver", GET, nullptr, oss);
REQUIRE(oss.str() == "receiver running\n");
oss = std::ostringstream{};
multiSlsDetectorClient("receiver stop", PUT, nullptr, oss);
REQUIRE(oss.str() == "receiver idle\n");
oss = std::ostringstream{};
multiSlsDetectorClient("receiver", GET, nullptr, oss);
REQUIRE(oss.str() == "receiver idle\n");
}
TEST_CASE("enablefwrite", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("enablefwrite 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "enablefwrite 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("enablefwrite", GET, nullptr, oss);
REQUIRE(oss.str() == "enablefwrite 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("enablefwrite 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "enablefwrite 0\n");
}
TEST_CASE("enableoverwrite", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("overwrite 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "overwrite 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("overwrite", GET, nullptr, oss);
REQUIRE(oss.str() == "overwrite 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("overwrite 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "overwrite 0\n");
}
TEST_CASE("activatecmd", "[.cmd]") {
// TODO! read padding from somewhere
auto oss = std::ostringstream{};
multiSlsDetectorClient("activate 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "activate 0 padding\n");
oss = std::ostringstream{};
multiSlsDetectorClient("activate", GET, nullptr, oss);
REQUIRE(oss.str() == "activate 0 padding\n");
oss = std::ostringstream{};
multiSlsDetectorClient("activate 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "activate 1 padding\n");
}
TEST_CASE("masterfile", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("masterfile 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "masterfile 0\n");
oss = std::ostringstream{};
multiSlsDetectorClient("masterfile", GET, nullptr, oss);
REQUIRE(oss.str() == "masterfile 0\n");
oss = std::ostringstream{};
multiSlsDetectorClient("masterfile 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "masterfile 1\n");
}
TEST_CASE("index", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("index 57", PUT, nullptr, oss);
REQUIRE(oss.str() == "index 57\n");
oss = std::ostringstream{};
multiSlsDetectorClient("index", GET, nullptr, oss);
REQUIRE(oss.str() == "index 57\n");
oss = std::ostringstream{};
multiSlsDetectorClient("index 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "index 0\n");
}
TEST_CASE("rx_tcpport", "[.cmd]") {
multiSlsDetector d;
int port = 1500;
int base = 1954;
for (size_t i = 0; i != d.size(); ++i) {
std::ostringstream oss;
std::string cmd =
std::to_string(i) + ":rx_tcpport " + std::to_string(port + i);
std::cout << cmd << "\n";
multiSlsDetectorClient(cmd, PUT, nullptr, oss);
REQUIRE(oss.str() == cmd + "\n");
}
{
std::ostringstream oss;
REQUIRE_THROWS(
multiSlsDetectorClient("rx_tcpport 15", PUT, nullptr, oss));
}
for (size_t i = 0; i != d.size(); ++i) {
std::ostringstream oss;
std::string cmd = std::to_string(i) + ":rx_tcpport";
multiSlsDetectorClient(cmd, GET, nullptr, oss);
REQUIRE(oss.str() == cmd + " " + std::to_string(port + i) + "\n");
}
for (size_t i = 0; i != d.size(); ++i) {
std::ostringstream oss;
std::string cmd =
std::to_string(i) + ":rx_tcpport " + std::to_string(base + i);
multiSlsDetectorClient(cmd, PUT, nullptr, oss);
REQUIRE(oss.str() == cmd + "\n");
}
}
TEST_CASE("fname", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("fname somename", PUT, nullptr, oss);
REQUIRE(oss.str() == "fname somename\n");
oss = std::ostringstream{};
multiSlsDetectorClient("fname", GET, nullptr, oss);
REQUIRE(oss.str() == "fname somename\n");
oss = std::ostringstream{};
multiSlsDetectorClient("fname run", PUT, nullptr, oss);
REQUIRE(oss.str() == "fname run\n");
}
TEST_CASE("resetframescaught get framescaught", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("resetframescaught 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "resetframescaught successful\n");
oss = std::ostringstream{};
multiSlsDetectorClient("framescaught", GET, nullptr, oss);
REQUIRE(oss.str() == "framescaught 0\n");
}
TEST_CASE("r_silent", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("r_silent 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_silent 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_silent", GET, nullptr, oss);
REQUIRE(oss.str() == "r_silent 1\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_silent 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_silent 0\n");
}
// TEST_CASE("rx_jsonaddheader", "[.cmd]") {
// auto oss = std::ostringstream{};
// multiSlsDetectorClient("rx_jsonaddheader \"hej\":\"5\"", PUT, nullptr,
// oss); REQUIRE(oss.str() == "rx_jsonaddheader \"hej\":\"5\"\n");
// oss = std::ostringstream{};
// multiSlsDetectorClient("rx_jsonaddheader", GET, nullptr, oss);
// REQUIRE(oss.str() == "rx_jsonaddheader \"hej\":\"5\"\n");
// oss = std::ostringstream{};
// multiSlsDetectorClient("rx_jsonaddheader \"\"", PUT, nullptr, oss);
// REQUIRE(oss.str() == "rx_jsonaddheader\n");
// }
// TEST_CASE("rx_udpsocksize", "[.cmd]") {
// auto oss = std::ostringstream{};
// multiSlsDetectorClient("rx_udpsocksize 4857600", PUT, nullptr, oss);
// REQUIRE(oss.str() == "rx_udpsocksize 4857600\n");
// oss = std::ostringstream{};
// multiSlsDetectorClient("rx_udpsocksize", GET, nullptr, oss);
// REQUIRE(oss.str() == "rx_udpsocksize 4857600\n");
// oss = std::ostringstream{};
// multiSlsDetectorClient("rx_udpsocksize 104857600", PUT, nullptr, oss);
// REQUIRE(oss.str() == "rx_udpsocksize 104857600\n");
// }
TEST_CASE("r_framesperfile", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("r_framesperfile 50", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_framesperfile 50\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_framesperfile", GET, nullptr, oss);
REQUIRE(oss.str() == "r_framesperfile 50\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_framesperfile 10000", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_framesperfile 10000\n");
}
TEST_CASE("r_discardpolicy", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("r_discardpolicy discardempty", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_discardpolicy discardempty\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_discardpolicy", GET, nullptr, oss);
REQUIRE(oss.str() == "r_discardpolicy discardempty\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_discardpolicy discardpartial", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_discardpolicy discardpartial\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_discardpolicy nodiscard", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_discardpolicy nodiscard\n");
}
TEST_CASE("r_padding", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("r_padding 0", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_padding 0\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_padding", GET, nullptr, oss);
REQUIRE(oss.str() == "r_padding 0\n");
oss = std::ostringstream{};
multiSlsDetectorClient("r_padding 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "r_padding 1\n");
}

View File

@ -125,11 +125,6 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
template <typename T>
void validate(T arg, T retval, std::string modename, numberMode hex);
/** Unrecognized Function */
int M_nofunc(sls::ServerInterface2 & socket);
/** Execute command */
int exec_command(sls::ServerInterface2 &socket);
@ -298,10 +293,6 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
/** set dbit offset */
int set_dbit_offset(sls::ServerInterface2 &socket);
int LogSocketCrash();
void NullObjectError(int& ret, char* mess);
/** detector type */
detectorType myDetectorType;
@ -390,6 +381,14 @@ protected:
std::unique_ptr<sls::ServerSocket> server{nullptr};
private:
int VerifyLock(int &ret, char *mess);
int VerifyLockAndIdle(int &ret, char *mess, int fnum);
void VerifyLock();
void VerifyIdle(sls::ServerInterface2& socket);
slsReceiverImplementation* impl(){
if (receiver!=nullptr){
return receiver.get();
}else{
throw sls::SocketError("Receiver not set up. Please use rx_hostname first.\n");
}
}
};

View File

@ -571,7 +571,7 @@ void slsReceiverImplementation::setFilePath(const char c[]) {
if(stat(c,&st) == 0)
strcpy(filePath,c);
else
FILE_LOG(logERROR) << "FilePath does not exist: " << filePath;
FILE_LOG(logERROR) << "FilePath does not exist: " << c;
}
FILE_LOG(logINFO) << "File path: " << filePath;
}
@ -1059,7 +1059,7 @@ int slsReceiverImplementation::setFifoDepth(const uint32_t i) {
if (fifoDepth != i) {
fifoDepth = i;
if (SetupFifoStructure() == FAIL)
return FAIL;
throw sls::RuntimeError("Failed to setup fifo structure");
}
FILE_LOG(logINFO) << "Fifo Depth: " << i;
return OK;

File diff suppressed because it is too large Load Diff

View File

@ -15,12 +15,16 @@ class DataSocket {
void swap(DataSocket &other) noexcept;
DataSocket(const DataSocket &) = delete;
DataSocket &operator=(DataSocket const &) = delete;
int getSocketId() const {
return socketId_;
}
int getSocketId() const { return socketId_; }
int sendData(const void *buffer, size_t size);
template <typename T> int sendData(T &&data) {
return sendData(&data, sizeof(data));
}
int receiveData(void *buffer, size_t size);
int read(void *buffer, size_t size);
int write(void *buffer, size_t size);
int setTimeOut(int t_seconds);
int setReceiveTimeout(int us);
void close();
@ -30,9 +34,12 @@ class DataSocket {
int socketId_ = -1;
};
int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res);
int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize);
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);
struct ::sockaddr_in
ConvertHostnameToInternetAddress(const std::string &hostname);
}; // namespace sls

View File

@ -13,16 +13,32 @@ class ServerInterface2 : public DataSocket {
using defs = slsDetectorDefs;
public:
ServerInterface2(int socketId) : DataSocket(socketId){}
ServerInterface2(int socketId) : DataSocket(socketId) {}
int sendResult(bool update, int ret, void *retval, int retvalSize,
char *mess = nullptr);
int receiveArg(int &ret, char *mess, void *arg, int sizeofArg);
int sendResult(int ret, void *retval, int retvalSize, char *mess = nullptr);
template <typename T> int sendResult(int ret, T &retval) {
return sendResult(ret, &retval, sizeof(retval, nullptr));
}
template <typename T> int sendResult(T &&retval) {
sendData(defs::OK);
sendData(retval);
return defs::OK;
}
private:
int receiveArg(void *arg, int sizeofArg);
template <typename T> int receiveArg(T &arg) {
return receiveArg(&arg, sizeof(arg));
}
template <typename T> T receive() {
T arg;
receiveArg(&arg, sizeof(arg));
return arg;
}
private:
};
} // namespace sls

View File

@ -15,13 +15,13 @@ class ServerSocket : public DataSocket {
public:
ServerSocket(int port);
ServerInterface2 accept();
IpAddr getLastClient() noexcept { return lastClient; }
IpAddr getThisClient() noexcept { return thisClient; }
IpAddr getLockedBy() noexcept { return lockedBy; }
IpAddr getLastClient() const noexcept { return lastClient; }
IpAddr getThisClient() const noexcept { return thisClient; }
IpAddr getLockedBy() const noexcept { return lockedBy; }
bool differentClients() const noexcept {return lastClient != thisClient;}
void setLockedBy(IpAddr addr) { lockedBy = addr; }
void setLastClient(IpAddr addr) { lastClient = addr; }
int getPort() const;
void SendResult(int &ret, void *retval, int retvalSize, char *mess);
int getPort() const noexcept { return serverPort; }
private:
IpAddr thisClient;

View File

@ -1,10 +1,10 @@
/** API versions */
#define GITBRANCH "refgui"
#define APIMOENCH 0x181108
#define APILIB 0x190405
#define APIRECEIVER 0x190405
#define APILIB 0x190604
#define APIRECEIVER 0x190604
#define APIGUI 0x190405
#define APIGOTTHARD 0x190604
#define APIJUNGFRAU 0x190604
#define APIEIGER 0x190604
#define APICTB 0x190604
#define APIEIGER 0x190604
#define APIJUNGFRAU 0x190604
#define APIGOTTHARD 0x190604

View File

@ -70,11 +70,11 @@ int ClientSocket::sendCommandThenRead(int fnum, const void *args,
void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
receiveData(&ret, sizeof(ret));
read(&ret, sizeof(ret));
if (ret == slsDetectorDefs::FAIL) {
char mess[MAX_STR_LENGTH]{};
// get error message
receiveData(mess, sizeof(mess));
read(mess, sizeof(mess));
FILE_LOG(logERROR) << socketType << " returned error: " << mess;
std::cout << "\n"; // needed to reset the color.
@ -89,7 +89,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
}
}
// get retval
receiveData(retval, retval_size);
read(retval, retval_size);
}
}; // namespace sls

View File

@ -51,7 +51,11 @@ int DataSocket::receiveData(void *buffer, size_t size) {
}
int DataSocket::read(void *buffer, size_t size){
return ::read(getSocketId(), reinterpret_cast<char *>(buffer), size);
return ::read(getSocketId(), buffer, size);
}
int DataSocket::write(void *buffer, size_t size){
return ::write(getSocketId(), buffer, size);
}
int DataSocket::setReceiveTimeout(int us) {
@ -64,13 +68,11 @@ int DataSocket::setReceiveTimeout(int us) {
int DataSocket::sendData(const void *buffer, size_t size) {
int dataSent = 0;
while (dataSent < (int)size) {
dataSent +=
write(getSocketId(), reinterpret_cast<const char *>(buffer) + dataSent,
size - dataSent);
}
return dataSent;
// int dataSent = 0;
// while (dataSent < (int)size) {
return ::write(getSocketId(), buffer, size);
// }
// return dataSent;
}
int DataSocket::setTimeOut(int t_seconds) {

View File

@ -1,37 +1,37 @@
#include "ServerInterface2.h"
#include <cassert>
#include <cstring>
#include <sstream>
namespace sls {
int ServerInterface2::sendResult(bool update, int ret, void *retval,
int retvalSize, char *mess) {
int ServerInterface2::sendResult(int ret, void *retval, int retvalSize,
char *mess) {
// if (update && ret == defs::OK && server_->DifferentClients()) {
// ret = defs::FORCE_UPDATE;
// }
sendData(&ret, sizeof(ret));
write(&ret, sizeof(ret));
if (ret == defs::FAIL) {
// send error message
if (mess)
sendData(mess, MAX_STR_LENGTH);
// debugging feature. should not happen.
else
if (mess != nullptr) {
write(mess, MAX_STR_LENGTH);
} else {
FILE_LOG(logERROR) << "No error message provided for this "
"failure. Will mess up TCP\n";
}
} else {
write(retval, retvalSize);
}
sendData(retval, retvalSize);
return ret;
}
int ServerInterface2::receiveArg(int &ret, char *mess, void *arg,
int sizeofArg) {
int ServerInterface2::receiveArg(void *arg, int sizeofArg) {
assert(sizeofArg > 0);
int bytes_read = read(arg, sizeofArg);
if (bytes_read == sizeofArg) {
return defs::OK;
} else {
FILE_LOG(logERROR) << "Read: " << bytes_read << " instead of "
<< sizeofArg;
return defs::FAIL;
std::ostringstream ss;
ss << "TCP socket error read " << bytes_read << " bytes instead of "
<< sizeofArg << " bytes";
throw sls::SocketError(ss.str());
}
}

View File

@ -52,26 +52,4 @@ ServerInterface2 ServerSocket::accept() {
return ServerInterface2(newSocket);
}
int ServerSocket::getPort() const { return serverPort; }
void ServerSocket::SendResult(int &ret, void* retval, int retvalSize, char* mess) {
// send success of operation
sendData(&ret, sizeof(ret));
if (ret == slsDetectorDefs::FAIL) {
// create error message if empty
if (!strlen(mess)) {
strcpy(mess, "No error message provided for this failure in server. Will mess up TCP.");
}
sendData(mess, MAX_STR_LENGTH);
throw sls::RuntimeError(mess);
}
// send return value
sendData(retval, retvalSize);
}
}; // namespace sls