mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-19 00:07:13 +02:00
changed constructor
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -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
|
@ -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;
|
||||
|
Reference in New Issue
Block a user