changed constructor

This commit is contained in:
Erik Frojdh
2019-01-24 11:12:24 +01:00
parent c3472f295b
commit a1c0d28ddb
16 changed files with 113 additions and 334 deletions

View File

@ -79,7 +79,7 @@ endif (USE_GUI)
add_subdirectory(integrationTests)
if (CALIBRATE)
if (DEFINED ENV{ROOTSYS})

View File

@ -5,30 +5,23 @@ MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
include_directories(
${PROJECT_SOURCE_DIR}/catch
${PROJECT_SOURCE_DIR}/slsSupportLib/include
${PROJECT_SOURCE_DIR}/slsDetectorSoftware/multiSlsDetector
${PROJECT_SOURCE_DIR}/slsDetectorSoftware/sharedMemory
)
if(USE_TESTS)
set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(TEST_SOURCES
${LOCAL_TEST_DIR}/test-container_utils.cpp
${LOCAL_TEST_DIR}/test-string_utils.cpp
${LOCAL_TEST_DIR}/test-MySocketTCP.cpp
${LOCAL_TEST_DIR}/test-CmdLineParser.cpp
#${LOCAL_TEST_DIR}/test-multiDetector.cpp
${LOCAL_TEST_DIR}/test.cpp
src/test-slsDetector.cpp
# src/test.cpp
)
add_executable(test ${TEST_SOURCES})
target_link_libraries(test
add_executable(a ${TEST_SOURCES})
target_link_libraries(a
slsDetectorShared
slsSupportLib
pthread
rt
)
set_target_properties(test PROPERTIES
set_target_properties(a PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
endif()

View File

@ -0,0 +1,30 @@
#include "catch.hpp"
#include "logger.h"
#include <iostream>
#include <vector>
#include "slsDetector.h"
#include "sls_detector_defs.h"
#define VERBOSE
int main(){
const std::string hostname = "beb083";
// const std::string hostname = "129.129.202.194";
auto type = slsDetector::getDetectorTypeAsEnum(hostname);
auto d = slsDetector(type);
d.setHostname(hostname.c_str());
// auto d = slsDetector();
std::cout << "type: " << d.getDetectorTypeAsString() << '\n';
std::cout << "hostname: " << d.getHostname() << '\n';
std::cout << "threshold: " << d.getThresholdEnergy() << '\n';
std::cout << "exptime: " << d.setTimer(slsDetectorDefs::timerIndex::ACQUISITION_TIME) << '\n';
// auto d2 = slsDetector(type, 0, 1);
// d2.setHostname("beb098");.
// auto d2 = slsDetector();
// std::cout << "hn: " << d2.getHostname() << '\n';
return 0;
}

View File

@ -9,16 +9,21 @@ set(SOURCES
set(HEADERS
)
include_directories(
multiSlsDetector
sharedMemory
slsDetector
)
# include_directories(
# multiSlsDetector
# sharedMemory
# slsDetector
# )
add_library(slsDetectorShared SHARED
${SOURCES}
${HEADERS}
)
target_include_directories(slsDetectorShared PUBLIC
multiSlsDetector
sharedMemory
slsDetector
)
target_link_libraries(slsDetectorShared
slsSupportLib

View File

@ -482,24 +482,9 @@ void multiSlsDetector::addSlsDetector(const std::string &hostname) {
}
}
// check entire shared memory if it doesnt exist?? needed?
// could be that detectors not loaded completely cuz of crash in new
// slsdetector in initsharedmemory
// get type by connecting
detectorType type = slsDetector::getDetectorTypeAsEnum(hostname.c_str(), DEFAULT_PORTNO);
if (type == GENERIC) {
FILE_LOG(logERROR) << "Could not connect to Detector " << hostname
<< " to determine the type!";
setErrorMask(getErrorMask() | MULTI_DETECTORS_NOT_ADDED);
appendNotAddedList(hostname.c_str());
return;
}
int pos = (int)detectors.size();
detectors.push_back(sls::make_unique<slsDetector>(type, detId, pos, false));
detectors.push_back(sls::make_unique<slsDetector>(hostname, detId, pos, false));
thisMultiDetector->numberOfDetectors = detectors.size();
detectors[pos]->setHostname(hostname.c_str()); // also updates client
thisMultiDetector->dataBytes += detectors[pos]->getDataBytes();
thisMultiDetector->dataBytesInclGapPixels +=
detectors[pos]->getDataBytesInclGapPixels();

View File

@ -1,5 +1,5 @@
#include "slsDetector.h"
#include "ClientInterface.h"
#include "ServerInterface.h"
#include "ClientSocket.h"
#include "MySocketTCP.h"
#include "SharedMemory.h"
@ -24,7 +24,7 @@
#define DEFAULT_HOSTNAME "localhost"
slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
slsDetector::slsDetector(const std::string& hostname, int multiId, int id, bool verify)
: detId(id) {
/* called from put hostname command,
* so sls shared memory will be created */
@ -37,11 +37,17 @@ slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
<< shm.GetName() << ". Freeing it again";
freeSharedMemory(multiId, id);
}
auto type = getDetectorTypeAsEnum(hostname);
if (type == GENERIC) {
FILE_LOG(logERROR) << "Could not connect to Detector " << hostname
<< " to determine the type!";
throw std::runtime_error("Cannot connect");
}
initSharedMemory(true, type, multiId, verify);
initializeDetectorStructure(type);
initializeMembers();
initializeDetectorStructurePointers();
setHostname(hostname.c_str());
}
slsDetector::slsDetector(int multiId, int id, bool verify)
@ -225,15 +231,15 @@ void slsDetector::freeSharedMemory() {
thisDetector = nullptr;
}
void slsDetector::setHostname(const char *name) {
setTCPSocket(std::string(name));
void slsDetector::setHostname(const std::string& hostname) {
setTCPSocket(hostname);
if (thisDetector->onlineFlag == ONLINE_FLAG) {
updateDetector();
}
}
std::string slsDetector::getHostname() {
return std::string(thisDetector->hostname);
return thisDetector->hostname;
}
/*
@ -502,9 +508,9 @@ void slsDetector::initializeMembers() {
delete thisReceiver;
thisReceiver = nullptr;
}
thisDetectorControl = new ClientInterface(controlSocket, detId, "Detector (Control server)");
thisDetectorStop = new ClientInterface(stopSocket, detId, "Detector (Stop server)");
thisReceiver = new ClientInterface(dataSocket, detId, "Receiver");
thisDetectorControl = new ServerInterface(controlSocket, detId, "Detector (Control server)");
thisDetectorStop = new ServerInterface(stopSocket, detId, "Detector (Stop server)");
thisReceiver = new ServerInterface(dataSocket, detId, "Receiver");
}
void slsDetector::initializeDetectorStructurePointers() {

View File

@ -17,7 +17,7 @@
class multiSlsDetector;
class SharedMemory;
class ClientInterface;
class ServerInterface;
class MySocketTCP;
#define SLS_SHMVERSION 0x181005
@ -273,7 +273,10 @@ public:
* @param id sls detector id (position in detectors list)
* @param verify true to verify if shared memory version matches existing one
*/
explicit slsDetector(detectorType type, int multiId = 0, int id = 0, bool verify = true);
explicit slsDetector(const std::string& hostname,
int multiId = 0,
int id = 0,
bool verify = true);
/**
* Constructor called when opening existing shared memory
@ -326,7 +329,7 @@ public:
* Connects to them to set up online flag
* @param name hostname
*/
void setHostname(const char *name);
void setHostname(const std::string& hostname);
/**
* Gets the hostname of detector
@ -1774,13 +1777,13 @@ private:
sharedSlsDetector *thisDetector {nullptr};
/** control socket interface */
ClientInterface *thisDetectorControl {nullptr};
ServerInterface *thisDetectorControl {nullptr};
/** stop socket interface */
ClientInterface *thisDetectorStop {nullptr};
ServerInterface *thisDetectorStop {nullptr};
/** receiver interface */
ClientInterface *thisReceiver {nullptr};
ServerInterface *thisReceiver {nullptr};
/** socket for control commands */
MySocketTCP *controlSocket {nullptr};

View File

@ -3,6 +3,7 @@
#include "sls_detector_defs.h"
#include "MySocketTCP.h"
#include "ClientSocket.h"
/**
@ -20,18 +21,13 @@ public:
* @param n for debugging purposes (useful only for client side)
* @param t string to identify type (Detector, Receiver) for printouts (useful only for client side)
*/
ClientInterface(MySocketTCP *socket, int n=-1, std::string t="");
ClientInterface(int n, sls::ClientSocket&& s);
/**
* destructor
*/
virtual ~ClientInterface() = default;
/**
* Set the datasocket
* @param socket the data socket
*/
void SetSocket(MySocketTCP *socket);
/**
* Receive ret, mess or retval from Server
@ -58,95 +54,18 @@ public:
char* mess = 0);
/** only Receiver
* Server sends result to client (also set ret to force_update if different clients)
* @param update true if one must update if different clients, else false
* @param ret success of operation
* @param retval pointer to result
* @param retvalSize size of result
* @param mess message
* @returns success of operation
*/
int Server_SendResult(bool update, int ret, void* retval, int retvalSize, char* mess = 0);
/** only Receiver
* Server receives arguments and checks if base object is null (if checkbase is true)
* checking base object is null (for receiver only when it has not been configured yet)
* @param ret pointer to success of operation
* @param mess message
* @param arg pointer to argument
* @param sizeofArg size of argument
* @param checkbase if true, checks if base object is null and sets ret and mess accordingly
* @param base pointer to base object
* @returns fail if socket crashes while reading arguments, else fail
*/
int Server_ReceiveArg(int& ret, char* mess, void* arg, int sizeofArg,bool checkbase=false, void* base=NULL);
/** only Receiver
* Server verifies if it is unlocked,
* sets and prints appropriate message if it is locked and different clients
* @param ret pointer to success
* @param mess message
* @param lockstatus status of lock
* @returns success of operaton
*/
int Server_VerifyLock(int& ret, char* mess, int lockstatus);
/** only Receiver
* Server verifies if it is unlocked and idle,
* sets and prints appropriate message if it is locked and different clients
* @param ret pointer to success
* @param mess message
* @param lockstatus status of lock
* @param status status of server
* @param fnum function number for error message
* @returns success of operaton
*/
int Server_VerifyLockAndIdle(int& ret, char* mess, int lockstatus, slsDetectorDefs::runStatus status, int fnum);
/** only Receiver
* Server sets and prints error message for null object error (receiver only)
* @param ret pointer to success that will be set to FAIL
* @param mess message
*/
void Server_NullObjectError(int& ret, char* mess);
/** only Receiver
* Servers prints error message for socket crash when reading
* @returns always FAIL
*/
int Server_SocketCrash();
/** only Receiver
* Servers sets and prints error message for locked server
* @param ret pointer to success that will be set to FAIL
* @param mess message
* @returns success of operaton
*/
int Server_LockedError(int& ret, char* mess);
/** only Receiver
* Servers sets and prints error message for server not being idle
* @param ret pointer to success that will be set to FAIL
* @param mess message
* @param fnum function number for error message
* @returns success of operaton
*/
int Server_NotIdleError(int& ret, char* mess, int fnum);
private:
/**
* socket for data acquisition
*/
MySocketTCP *mySocket;
sls::ClientSocket socket_;
/** index for client debugging purposes */
int index;
/** string for type to differentiate between Detector & Receiver in printouts */
std::string type;
};

View File

@ -10,8 +10,9 @@ namespace sls{
class ClientSocket: public DataSocket{
public:
ClientSocket(const std::string& hostname, uint16_t port_number);
int connect();
private:
struct sockaddr_in serverAddr {};
};
}; //namespace sls

View File

@ -7,7 +7,11 @@ namespace sls {
class DataSocket {
public:
DataSocket(int socketId);
DataSocket(DataSocket&& move) noexcept;
DataSocket& operator=(DataSocket&& move) noexcept;
void swap(DataSocket& other) noexcept;
DataSocket(DataSocket const&) = delete;
DataSocket& operator=(DataSocket const&) = delete;
int getSocketId() const{
return socketId_;
}

View File

@ -1,17 +1,12 @@
#include "ClientInterface.h"
#include "ClientSocket.h"
ClientInterface::ClientInterface(MySocketTCP *socket, int n, std::string t): mySocket(socket),
index(n),
type(t){}
void ClientInterface::SetSocket(MySocketTCP *socket) {
mySocket = socket;
}
ClientInterface::ClientInterface(int n, sls::ClientSocket&& s): socket_(std::move(s)),
index(n){}
void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int sizeOfRetval) {
// get result of operation
mySocket->ReceiveDataOnly(&ret,sizeof(ret));
socket_.receiveData(reinterpret_cast<char *>(&ret), sizeof(ret));
bool unrecognizedFunction = false;
if (ret == FAIL) {
@ -23,8 +18,8 @@ void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int siz
memset(mess, 0, MAX_STR_LENGTH);
}
// get error message
mySocket->ReceiveDataOnly(mess,MAX_STR_LENGTH);
cprintf(RED, "%s %d returned error: %s", type.c_str(), index, mess);
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)
@ -35,7 +30,7 @@ void ClientInterface::Client_Receive(int& ret, char* mess, void* retval, int siz
}
// get retval
if (!unrecognizedFunction)
mySocket->ReceiveDataOnly(retval, sizeOfRetval);
socket_.receiveData( reinterpret_cast<char *>(retval), sizeOfRetval);
}
@ -44,100 +39,8 @@ int ClientInterface::Client_Send(int fnum,
void* retval, int sizeOfRetval,
char* mess) {
int ret = FAIL;
mySocket->SendDataOnly(&fnum,sizeof(fnum));
mySocket->SendDataOnly(args, sizeOfArgs);
socket_.sendData(reinterpret_cast<char *>(&fnum),sizeof(fnum));
socket_.sendData(reinterpret_cast<char *>(args), sizeOfArgs);
Client_Receive(ret, mess, retval, sizeOfRetval);
return ret;
}
int ClientInterface::Server_SendResult(bool update, int ret,
void* retval, int retvalSize, char* mess) {
// update if different clients
if (update && ret == OK && mySocket->differentClients)
ret = FORCE_UPDATE;
// send success of operation
mySocket->SendDataOnly(&ret,sizeof(ret));
if(ret == FAIL) {
// send error message
if (mess)
mySocket->SendDataOnly(mess, MAX_STR_LENGTH);
// debugging feature. should not happen.
else
FILE_LOG(logERROR) << "No error message provided for this failure. Will mess up TCP\n";
}
// send return value
mySocket->SendDataOnly(retval, retvalSize);
return ret;
}
int ClientInterface::Server_ReceiveArg(int& ret, char* mess, void* arg, int sizeofArg, bool checkbase, void* base) {
// client socket crash, cannot receive arguments
if (sizeofArg && mySocket->ReceiveDataOnly(arg, sizeofArg) < 0)
return Server_SocketCrash();
// check if server object created
if (checkbase && base == nullptr)
Server_NullObjectError(ret, mess);
// no crash
return OK;
}
int ClientInterface::Server_VerifyLock(int& ret, char* mess, int lockstatus) {
// server locked
if (mySocket->differentClients && lockstatus)
return Server_LockedError(ret, mess);
return ret;
}
int ClientInterface::Server_VerifyLockAndIdle(int& ret, char* mess, int lockstatus, slsDetectorDefs::runStatus status, int fnum) {
// server locked
if (mySocket->differentClients && lockstatus)
return Server_LockedError(ret, mess);
// server not idle for this command
if (status != slsDetectorDefs::IDLE)
return Server_NotIdleError(ret, mess, fnum);
return ret;
}
void ClientInterface::Server_NullObjectError(int& ret, char* mess) {
ret=FAIL;
strcpy(mess,"Receiver not set up. Please use rx_hostname first.\n");
FILE_LOG(logERROR) << mess;
}
int ClientInterface::Server_SocketCrash() {
FILE_LOG(logERROR) << "Reading from socket failed. Possible socket crash";
return FAIL;
}
int ClientInterface::Server_LockedError(int& ret, char* mess) {
ret = FAIL;
sprintf(mess,"Receiver locked by %s\n", mySocket->lastClientIP);
FILE_LOG(logERROR) << mess;
return ret;
}
int ClientInterface::Server_NotIdleError(int& ret, char* mess, int fnum) {
ret = FAIL;
sprintf(mess,"Can not execute %s when receiver is not idle\n",
getFunctionNameFromEnum((enum detFuncs)fnum));
FILE_LOG(logERROR) << mess;
return ret;
}

View File

@ -19,17 +19,22 @@ ClientSocket::ClientSocket(const std::string &host, uint16_t port) : DataSocket(
}
//TODO! Erik, results could have multiple entries do we need to loop through them?
struct sockaddr_in serverAddr {};
// 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));
if (connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0){
if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0){
freeaddrinfo(result);
throw std::runtime_error("ClientSocket ERROR: cannot connect to host\n");
}
freeaddrinfo(result);
}
int ClientSocket::connect(){
//used to reconnect after closing may be removed
return ::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr));
}
}; //namespace sls

View File

@ -4,11 +4,23 @@
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <algorithm>
namespace sls {
DataSocket::DataSocket(int socketId) : socketId_(socketId) {}
void DataSocket::swap(DataSocket& other) noexcept{
std::swap(socketId_, other.socketId_);
}
DataSocket::DataSocket(DataSocket&& move) noexcept{
move.swap(*this);
}
DataSocket& DataSocket::operator=(DataSocket&& move)noexcept{
move.swap(*this);
return *this;
}
size_t DataSocket::receiveData(char *buffer, size_t size) {
std::cout << "Sending\n";
size_t dataRead = 0;

View File

@ -1,87 +0,0 @@
#include "MySocketTCP.h"
#include "catch.hpp"
// #include "multiSlsDetector.h"
#include "logger.h"
#include <iostream>
#include <vector>
#define VERBOSE
TEST_CASE("Sending and receiving data with two sockets") {
const int port_number{1966}; //TODO! Avoid hardcoded port number!!!
auto sender = MySocketTCP("localhost", port_number);
auto receiver = MySocketTCP(port_number);
auto s = sender.Connect();
auto r = receiver.Connect();
REQUIRE(s > 0);
REQUIRE(r > 0);
REQUIRE(sender.getPortNumber() == port_number);
REQUIRE(receiver.getPortNumber() == port_number);
std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
std::vector<char> received_message(message_to_send.size());
auto sent = sender.SendDataOnly(message_to_send.data(), message_to_send.size());
auto received = receiver.ReceiveDataOnly(received_message.data(), message_to_send.size());
REQUIRE(sent == message_to_send.size());
REQUIRE(received == received_message.size());
REQUIRE(sent == received);
REQUIRE(message_to_send == received_message);
receiver.CloseServerTCPSocketDescriptor();
receiver.Disconnect();
sender.Disconnect();
REQUIRE(receiver.getsocketDescriptor() == -1);
REQUIRE(receiver.getFileDes() == -1);
REQUIRE(sender.getFileDes() == -1);
}
TEST_CASE("Open two sockets on the same port fails and throws") {
const int port_number{1966};
auto server = MySocketTCP(port_number);
CHECK_THROWS(MySocketTCP(port_number));
}
// TEST_CASE("Conversions"){
// std::cout << "name " << MySocketTCP::nameToMac("enp10s0u1u3u3") << '\n';
// }
// TEST_CASE("Have two clients connect to the same server") {
// const int port_number{1966};
// auto server = MySocketTCP(port_number);
// auto client1 = MySocketTCP("localhost", port_number);
// auto client2 = MySocketTCP("localhost", port_number);
// client1.SetTimeOut(1);
// client2.SetTimeOut(1);
// server.SetTimeOut(1);
// auto fd1 = client1.Connect();
// auto fd2 = client2.Connect();
// server.Connect();
// REQUIRE(fd1 > 0);
// REQUIRE(fd2 > 0);
// std::cout << "fd1 " << fd1 << '\n';
// std::cout << "fd2 " << fd2 << '\n';
// std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
// std::vector<char> received_message(message_to_send.size());
// client1.SendDataOnly(message_to_send.data(), message_to_send.size());
// auto n1 = server.ReceiveDataOnly(received_message.data(), received_message.size());
// std::cout << "n1 " << n1 << '\n';
// client2.SendDataOnly(message_to_send.data(), message_to_send.size());
// auto n2 = server.ReceiveDataOnly(received_message.data(), received_message.size());
// std::cout << "n2 " << n2 << '\n';
// }