mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-22 22:40:02 +02:00
Merge branch 'developer' of github.com:slsdetectorgroup/slsDetectorPackage into developer
This commit is contained in:
commit
c90a7b0e8a
@ -3,11 +3,12 @@
|
||||
#include <string>
|
||||
|
||||
#include "CmdLineParser.h"
|
||||
#include "CmdProxy.h"
|
||||
#include "container_utils.h"
|
||||
#include "string_utils.h"
|
||||
#include "multiSlsDetector.h"
|
||||
#include "slsDetectorCommand.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
#include "string_utils.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
@ -21,59 +22,69 @@ inline int dummyCallback(detectorData *d, int p, void *) {
|
||||
|
||||
class multiSlsDetectorClient {
|
||||
public:
|
||||
multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr):
|
||||
action_(action),
|
||||
detPtr(myDetector){
|
||||
multiSlsDetectorClient(int argc, char *argv[], int action,
|
||||
multiSlsDetector *myDetector = nullptr)
|
||||
: action_(action), detPtr(myDetector) {
|
||||
parser.Parse(argc, argv);
|
||||
runCommand();
|
||||
|
||||
}
|
||||
multiSlsDetectorClient(const std::string& args, int action, multiSlsDetector *myDetector = nullptr):
|
||||
action_(action),
|
||||
detPtr(myDetector){
|
||||
multiSlsDetectorClient(const std::string &args, int action,
|
||||
multiSlsDetector *myDetector = nullptr)
|
||||
: action_(action), detPtr(myDetector) {
|
||||
parser.Parse(args);
|
||||
runCommand();
|
||||
}
|
||||
private:
|
||||
int action_;
|
||||
CmdLineParser parser;
|
||||
multiSlsDetector* detPtr = nullptr;
|
||||
|
||||
void runCommand(){
|
||||
private:
|
||||
int action_;
|
||||
CmdLineParser parser;
|
||||
multiSlsDetector *detPtr = nullptr;
|
||||
|
||||
void runCommand() {
|
||||
bool verify = true;
|
||||
bool update = true;
|
||||
if (action_ == slsDetectorDefs::PUT_ACTION && parser.n_arguments() == 0) {
|
||||
std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl;
|
||||
if (action_ == slsDetectorDefs::PUT_ACTION &&
|
||||
parser.n_arguments() == 0) {
|
||||
std::cout << "Wrong usage - should be: " << parser.executable()
|
||||
<< "[id-][pos:]channel arg" << std::endl;
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
};
|
||||
if (action_ == slsDetectorDefs::GET_ACTION && parser.command().empty()) {
|
||||
std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl;
|
||||
if (action_ == slsDetectorDefs::GET_ACTION &&
|
||||
parser.command().empty()) {
|
||||
std::cout << "Wrong usage - should be: " << parser.executable()
|
||||
<< "[id-][pos:]channel arg" << std::endl;
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
};
|
||||
|
||||
if (action_ == slsDetectorDefs::READOUT_ACTION && parser.detector_id() != -1) {
|
||||
std::cout << "detector_id: " << parser.detector_id() << " ,readout of individual detectors is not allowed!" << std::endl;
|
||||
if (action_ == slsDetectorDefs::READOUT_ACTION &&
|
||||
parser.detector_id() != -1) {
|
||||
std::cout << "detector_id: " << parser.detector_id()
|
||||
<< " ,readout of individual detectors is not allowed!"
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// special commands
|
||||
if (parser.command() == "free") {
|
||||
multiSlsDetector::freeSharedMemory(parser.multi_id(), parser.detector_id());
|
||||
multiSlsDetector::freeSharedMemory(parser.multi_id(),
|
||||
parser.detector_id());
|
||||
return;
|
||||
} // get user details without verify sharedMultiSlsDetector version
|
||||
else if ((parser.command() == "user") && (action_ == slsDetectorDefs::GET_ACTION)) {
|
||||
else if ((parser.command() == "user") &&
|
||||
(action_ == slsDetectorDefs::GET_ACTION)) {
|
||||
verify = false;
|
||||
update = false;
|
||||
}
|
||||
|
||||
//std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
|
||||
// std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
|
||||
// create multiSlsDetector class if required
|
||||
std::unique_ptr<multiSlsDetector> localDet;
|
||||
if (detPtr == nullptr) {
|
||||
try {
|
||||
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update);
|
||||
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(),
|
||||
verify, update);
|
||||
detPtr = localDet.get();
|
||||
} catch (const RuntimeError &e) {
|
||||
/*std::cout << e.GetMessage() << std::endl;*/
|
||||
@ -88,11 +99,27 @@ class multiSlsDetectorClient {
|
||||
return;
|
||||
}
|
||||
|
||||
// call multi detector command line
|
||||
slsDetectorCommand myCmd(detPtr);
|
||||
std::string answer = myCmd.executeLine(parser.n_arguments()+1, parser.argv().data(), action_, parser.detector_id());
|
||||
// Call CmdProxy which execute the command if it exists, on success
|
||||
// returns an empty string If the command is not in CmdProxy but
|
||||
// deprecated the new command is returned
|
||||
if (action_ != slsDetectorDefs::READOUT_ACTION) {
|
||||
sls::CmdProxy<multiSlsDetector> proxy(detPtr);
|
||||
auto cmd = proxy.Call(parser.command(), parser.arguments(),
|
||||
parser.detector_id());
|
||||
if (cmd.empty()) {
|
||||
return;
|
||||
} else {
|
||||
parser.setCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
if (parser.multi_id()!=0)
|
||||
// call multi detector command line
|
||||
slsDetectorCommand myCmd(detPtr);
|
||||
std::string answer =
|
||||
myCmd.executeLine(parser.n_arguments() + 1, parser.argv().data(),
|
||||
action_, parser.detector_id());
|
||||
|
||||
if (parser.multi_id() != 0)
|
||||
std::cout << parser.multi_id() << '-';
|
||||
if (parser.detector_id() != -1)
|
||||
std::cout << parser.detector_id() << ':';
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "slsDetector.h"
|
||||
#include "ClientSocket.h"
|
||||
#include "ServerInterface.h"
|
||||
#include "SharedMemory.h"
|
||||
#include "file_utils.h"
|
||||
#include "multiSlsDetector.h"
|
||||
@ -3216,10 +3215,10 @@ int slsDetector::setReceiverOnline(int value) {
|
||||
} else {
|
||||
shm()->rxOnlineFlag = OFFLINE_FLAG;
|
||||
if (value == ONLINE_FLAG) {
|
||||
// connect and set offline flag
|
||||
auto receiver =
|
||||
ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
|
||||
receiver.close();
|
||||
// Connect and ask for receiver id to verify that
|
||||
// it's online and working
|
||||
int64_t retval{0};
|
||||
sendToReceiver(F_GET_RECEIVER_ID, nullptr, retval);
|
||||
shm()->rxOnlineFlag = ONLINE_FLAG;
|
||||
if (shm()->receiverAPIVersion == 0) {
|
||||
checkReceiverVersionCompatibility();
|
||||
|
@ -26,8 +26,5 @@ int main(int argc, char *argv[]) {
|
||||
#ifdef HELP
|
||||
int action = slsDetectorDefs::HELP_ACTION;
|
||||
#endif
|
||||
|
||||
// if (argc > 1)
|
||||
// argv++;
|
||||
multiSlsDetectorClient(argc, argv, action);
|
||||
}
|
||||
|
@ -10,8 +10,8 @@
|
||||
|
||||
class MySocketTCP;
|
||||
class ServerInterface;
|
||||
class slsReceiverImplementation;
|
||||
|
||||
#include "slsReceiverImplementation.h"
|
||||
#include "ServerSocket.h"
|
||||
|
||||
|
||||
/**
|
||||
@ -113,7 +113,7 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
|
||||
int function_table();
|
||||
|
||||
/** Decodes Function */
|
||||
int decode_function();
|
||||
int decode_function(sls::ServerInterface2 &socket);
|
||||
|
||||
/** function not implemented for specific detector */
|
||||
void functionNotImplemented();
|
||||
@ -126,208 +126,211 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
|
||||
void validate(T arg, T retval, std::string modename, numberMode hex);
|
||||
|
||||
/** Unrecognized Function */
|
||||
int M_nofunc();
|
||||
int M_nofunc(sls::ServerInterface2 & socket);
|
||||
|
||||
|
||||
|
||||
/** Execute command */
|
||||
int exec_command();
|
||||
int exec_command(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Exit Receiver Server */
|
||||
int exit_server();
|
||||
int exit_server(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Locks Receiver */
|
||||
int lock_receiver();
|
||||
int lock_receiver(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Get Last Client IP*/
|
||||
int get_last_client_ip();
|
||||
int get_last_client_ip(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set port */
|
||||
int set_port();
|
||||
int set_port(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Updates Client if different clients connect */
|
||||
int update_client();
|
||||
int update_client(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Sends the updated parameters to client */
|
||||
int send_update();
|
||||
int send_update(sls::ServerInterface2 &socket);
|
||||
|
||||
/** get version, calls get_version */
|
||||
int get_id();
|
||||
int get_id(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set detector type */
|
||||
int set_detector_type();
|
||||
int set_detector_type(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set detector hostname */
|
||||
int set_detector_hostname();
|
||||
int set_detector_hostname(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set roi */
|
||||
int set_roi();
|
||||
int set_roi(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set up UDP Details */
|
||||
int setup_udp();
|
||||
int setup_udp(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set acquisition period, frame number etc */
|
||||
int set_timer();
|
||||
int set_timer(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set dynamic range */
|
||||
int set_dynamic_range();
|
||||
int set_dynamic_range(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Sets the receiver streaming frequency */
|
||||
int set_streaming_frequency();
|
||||
int set_streaming_frequency(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Gets receiver status */
|
||||
int get_status();
|
||||
int get_status(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Start Receiver - starts listening to udp packets from detector */
|
||||
int start_receiver();
|
||||
int start_receiver(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Stop Receiver - stops listening to udp packets from detector*/
|
||||
int stop_receiver();
|
||||
int stop_receiver(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set File path */
|
||||
int set_file_dir();
|
||||
int set_file_dir(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set File name without frame index, file index and extension */
|
||||
int set_file_name();
|
||||
int set_file_name(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Set File index */
|
||||
int set_file_index();
|
||||
int set_file_index(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Gets frame index for each acquisition */
|
||||
int get_frame_index();
|
||||
int get_frame_index(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Gets Total Frames Caught */
|
||||
int get_frames_caught();
|
||||
int get_frames_caught(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Resets Total Frames Caught */
|
||||
int reset_frames_caught();
|
||||
int reset_frames_caught(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Enable File Write*/
|
||||
int enable_file_write();
|
||||
int enable_file_write(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Enable Master File Write */
|
||||
int enable_master_file_write();
|
||||
int enable_master_file_write(sls::ServerInterface2 &socket);
|
||||
|
||||
/** enable compression */
|
||||
int enable_compression();
|
||||
int enable_compression(sls::ServerInterface2 &socket);
|
||||
|
||||
/** enable overwrite */
|
||||
int enable_overwrite();
|
||||
int enable_overwrite(sls::ServerInterface2 &socket);
|
||||
|
||||
/** enable 10Gbe */
|
||||
int enable_tengiga();
|
||||
int enable_tengiga(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set fifo depth */
|
||||
int set_fifo_depth();
|
||||
int set_fifo_depth(sls::ServerInterface2 &socket);
|
||||
|
||||
/** activate/ deactivate */
|
||||
int set_activate();
|
||||
int set_activate(sls::ServerInterface2 &socket);
|
||||
|
||||
/* Set the data stream enable */
|
||||
int set_data_stream_enable();
|
||||
int set_data_stream_enable(sls::ServerInterface2 &socket);
|
||||
|
||||
/** Sets the steadming timer when frequency is set to 0 */
|
||||
int set_streaming_timer();
|
||||
int set_streaming_timer(sls::ServerInterface2 &socket);
|
||||
|
||||
/** enable flipped data */
|
||||
int set_flipped_data();
|
||||
int set_flipped_data(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set file format */
|
||||
int set_file_format();
|
||||
int set_file_format(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set position id */
|
||||
int set_detector_posid();
|
||||
int set_detector_posid(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set multi detector size */
|
||||
int set_multi_detector_size();
|
||||
int set_multi_detector_size(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set streaming port */
|
||||
int set_streaming_port();
|
||||
int set_streaming_port(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set streaming source ip */
|
||||
int set_streaming_source_ip();
|
||||
int set_streaming_source_ip(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set silent mode */
|
||||
int set_silent_mode();
|
||||
int set_silent_mode(sls::ServerInterface2 &socket);
|
||||
|
||||
/** enable gap pixels */
|
||||
int enable_gap_pixels();
|
||||
int enable_gap_pixels(sls::ServerInterface2 &socket);
|
||||
|
||||
/** restream stop packet */
|
||||
int restream_stop();
|
||||
int restream_stop(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set additional json header */
|
||||
int set_additional_json_header();
|
||||
int set_additional_json_header(sls::ServerInterface2 &socket);
|
||||
|
||||
/** get additional json header */
|
||||
int get_additional_json_header();
|
||||
int get_additional_json_header(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set udp socket buffer size */
|
||||
int set_udp_socket_buffer_size();
|
||||
int set_udp_socket_buffer_size(sls::ServerInterface2 &socket);
|
||||
|
||||
/** get real udp socket buffer size */
|
||||
int get_real_udp_socket_buffer_size();
|
||||
int get_real_udp_socket_buffer_size(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set frames per file */
|
||||
int set_frames_per_file();
|
||||
int set_frames_per_file(sls::ServerInterface2 &socket);
|
||||
|
||||
/** check version compatibility */
|
||||
int check_version_compatibility();
|
||||
int check_version_compatibility(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set frame discard policy */
|
||||
int set_discard_policy();
|
||||
int set_discard_policy(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set partial frame padding enable*/
|
||||
int set_padding_enable();
|
||||
int set_padding_enable(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set deactivated receiver padding enable */
|
||||
int set_deactivated_padding_enable();
|
||||
int set_deactivated_padding_enable(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set readout flags */
|
||||
int set_readout_flags();
|
||||
int set_readout_flags(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set adc mask */
|
||||
int set_adc_mask();
|
||||
int set_adc_mask(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set receiver dbit list */
|
||||
int set_dbit_list();
|
||||
int set_dbit_list(sls::ServerInterface2 &socket);
|
||||
|
||||
/** get receiver dbit list */
|
||||
int get_dbit_list();
|
||||
int get_dbit_list(sls::ServerInterface2 &socket);
|
||||
|
||||
/** set dbit offset */
|
||||
int set_dbit_offset();
|
||||
int set_dbit_offset(sls::ServerInterface2 &socket);
|
||||
|
||||
|
||||
int LogSocketCrash();
|
||||
void NullObjectError(int& ret, char* mess);
|
||||
|
||||
/** detector type */
|
||||
detectorType myDetectorType;
|
||||
|
||||
/** slsReceiverBase object */
|
||||
slsReceiverImplementation *receiver;
|
||||
std::unique_ptr<slsReceiverImplementation> receiver{nullptr};
|
||||
|
||||
/** Function List */
|
||||
int (slsReceiverTCPIPInterface::*flist[NUM_REC_FUNCTIONS])();
|
||||
int (slsReceiverTCPIPInterface::*flist[NUM_REC_FUNCTIONS])(sls::ServerInterface2& socket);
|
||||
|
||||
/** Message */
|
||||
char mess[MAX_STR_LENGTH];
|
||||
char mess[MAX_STR_LENGTH]{};
|
||||
|
||||
/** success/failure */
|
||||
int ret;
|
||||
int ret{OK};
|
||||
|
||||
/** function index */
|
||||
int fnum;
|
||||
int fnum{-1};
|
||||
|
||||
/** Lock Status if server locked to a client */
|
||||
int lockStatus;
|
||||
int lockStatus{0};
|
||||
|
||||
/** kill tcp server thread */
|
||||
int killTCPServerThread;
|
||||
int killTCPServerThread{0};
|
||||
|
||||
/** thread for TCP server */
|
||||
pthread_t TCPServer_thread;
|
||||
|
||||
/** tcp thread created flag*/
|
||||
bool tcpThreadCreated;
|
||||
bool tcpThreadCreated{false};
|
||||
|
||||
/** port number */
|
||||
int portNumber;
|
||||
@ -345,16 +348,16 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
|
||||
* we write depending on file write enable
|
||||
* users get data to write depending on call backs registered
|
||||
*/
|
||||
int (*startAcquisitionCallBack)(char*, char*, uint64_t, uint32_t, void*);
|
||||
void *pStartAcquisition;
|
||||
int (*startAcquisitionCallBack)(char*, char*, uint64_t, uint32_t, void*) = nullptr;
|
||||
void *pStartAcquisition{nullptr};
|
||||
|
||||
/**
|
||||
* Call back for acquisition finished
|
||||
* callback argument is
|
||||
* total frames caught
|
||||
*/
|
||||
void (*acquisitionFinishedCallBack)(uint64_t, void*);
|
||||
void *pAcquisitionFinished;
|
||||
void (*acquisitionFinishedCallBack)(uint64_t, void*) = nullptr;
|
||||
void *pAcquisitionFinished{nullptr};
|
||||
|
||||
|
||||
/**
|
||||
@ -365,7 +368,7 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
|
||||
* dataSize in bytes is the size of the data in bytes.
|
||||
*/
|
||||
void (*rawDataReadyCallBack)(char* ,
|
||||
char*, uint32_t, void*);
|
||||
char*, uint32_t, void*) = nullptr;
|
||||
|
||||
/**
|
||||
* Call back for raw data (modified)
|
||||
@ -375,17 +378,18 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
|
||||
* revDatasize is the reference of data size in bytes. Can be modified to the new size to be written/streamed. (only smaller value).
|
||||
*/
|
||||
void (*rawDataModifyReadyCallBack)(char* ,
|
||||
char*, uint32_t &, void*);
|
||||
char*, uint32_t &, void*) = nullptr;
|
||||
|
||||
void *pRawDataReady;
|
||||
void *pRawDataReady{nullptr};
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
/** Socket */
|
||||
MySocketTCP* mySock;
|
||||
|
||||
/** client interface */
|
||||
ServerInterface* interface;
|
||||
std::unique_ptr<sls::ServerSocket> server{nullptr};
|
||||
|
||||
private:
|
||||
int VerifyLock(int &ret, char *mess);
|
||||
int VerifyLockAndIdle(int &ret, char *mess, int fnum);
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -6,6 +6,7 @@ set(SOURCES
|
||||
src/DataSocket.cpp
|
||||
src/ServerSocket.cpp
|
||||
src/ServerInterface.cpp
|
||||
src/ServerInterface2.cpp
|
||||
src/network_utils.cpp
|
||||
)
|
||||
|
||||
@ -27,7 +28,7 @@ set(PUBLICHEADERS
|
||||
include/ClientSocket.h
|
||||
include/DataSocket.h
|
||||
include/ServerSocket.h
|
||||
include/ServerInterface.h
|
||||
include/ServerInterface2.h
|
||||
include/network_utils.h
|
||||
include/FixedCapacityContainer.h
|
||||
)
|
||||
|
@ -14,6 +14,7 @@ class CmdLineParser {
|
||||
int detector_id() const { return detector_id_; };
|
||||
int n_arguments() const { return arguments_.size(); }
|
||||
const std::string &command() const { return command_; }
|
||||
void setCommand(std::string cmd){command_ = cmd;}
|
||||
const std::string &executable() const { return executable_; }
|
||||
const std::vector<std::string> &arguments() const { return arguments_; };
|
||||
std::vector<const char *> argv() const;
|
||||
|
100
slsSupportLib/include/CmdProxy.h
Normal file
100
slsSupportLib/include/CmdProxy.h
Normal file
@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "logger.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
template <typename T> class CmdProxy {
|
||||
public:
|
||||
explicit CmdProxy(T *detectorPtr) : det(detectorPtr) {}
|
||||
|
||||
std::string Call(const std::string &command,
|
||||
const std::vector<std::string> &arguments,
|
||||
int detector_id) {
|
||||
cmd = command;
|
||||
args = arguments;
|
||||
det_id = detector_id;
|
||||
|
||||
ReplaceIfDepreciated(cmd);
|
||||
|
||||
auto it = functions.find(cmd);
|
||||
if (it != functions.end()) {
|
||||
std::cout << ((*this).*(it->second))();
|
||||
return {};
|
||||
} else {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
|
||||
bool ReplaceIfDepreciated(std::string &command) {
|
||||
auto d_it = depreciated_functions.find(command);
|
||||
if (d_it != depreciated_functions.end()) {
|
||||
FILE_LOG(logWARNING)
|
||||
<< "WARNING: " << command
|
||||
<< " is depreciated and will be removed. Please migrate to: "
|
||||
<< d_it->second;
|
||||
command = d_it->second;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t GetFunctionMapSize() const noexcept { return functions.size(); };
|
||||
|
||||
private:
|
||||
T *det;
|
||||
std::string cmd;
|
||||
std::vector<std::string> args;
|
||||
int det_id{-1};
|
||||
|
||||
using FunctionMap = std::map<std::string, std::string (CmdProxy::*)()>;
|
||||
using StringMap = std::map<std::string, std::string>;
|
||||
|
||||
// Initialize maps for translating name and function
|
||||
FunctionMap functions{{"newfunc", &CmdProxy::NewFunction}};
|
||||
|
||||
StringMap depreciated_functions{{"oldvrfcmd", "vrf"},
|
||||
{"veryveryold", "vcp"},
|
||||
{"anothercmd", "vrs"},
|
||||
{"this_as_well", "enablefwrite"}};
|
||||
|
||||
|
||||
template <typename U> std::string ResultToString(const U &ret) {
|
||||
std::ostringstream os;
|
||||
if (det_id != -1)
|
||||
os << det_id << ":";
|
||||
os << cmd << " " << ret << "\n";
|
||||
return os.str();
|
||||
}
|
||||
|
||||
void WrongNumberOfParameters(size_t expected) {
|
||||
throw RuntimeError("ERROR: Expected " + std::to_string(expected) +
|
||||
" parameters but got " + std::to_string(args.size()) +
|
||||
"\n");
|
||||
}
|
||||
|
||||
// Mapped functions
|
||||
|
||||
//example
|
||||
std::string NewFunction() {
|
||||
if(args.size() == 0){
|
||||
std::cout << "This is the new function function\n";
|
||||
return ResultToString(det->setExposureTime(-1, true));
|
||||
}else if(args.size() == 1){
|
||||
std::cout << "Setting exposure time to " << args[0] << "s\n";
|
||||
return ResultToString(det->setExposureTime(std::stod(args[0]), true, det_id));
|
||||
}else{
|
||||
WrongNumberOfParameters(1);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sls
|
@ -18,9 +18,11 @@ class DataSocket {
|
||||
int getSocketId() const {
|
||||
return socketId_;
|
||||
}
|
||||
size_t sendData(const void *buffer, size_t size);
|
||||
size_t receiveData(void *buffer, size_t size);
|
||||
int sendData(const void *buffer, size_t size);
|
||||
int receiveData(void *buffer, size_t size);
|
||||
int read(void *buffer, size_t size);
|
||||
int setTimeOut(int t_seconds);
|
||||
int setReceiveTimeout(int us);
|
||||
void close();
|
||||
void shutDownSocket();
|
||||
|
||||
|
28
slsSupportLib/include/ServerInterface2.h
Normal file
28
slsSupportLib/include/ServerInterface2.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "DataSocket.h"
|
||||
namespace sls {
|
||||
class ServerInterface2;
|
||||
}
|
||||
|
||||
#include "ServerSocket.h"
|
||||
#include "sls_detector_defs.h"
|
||||
namespace sls {
|
||||
|
||||
class ServerInterface2 : public DataSocket {
|
||||
using defs = slsDetectorDefs;
|
||||
|
||||
public:
|
||||
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);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
} // namespace sls
|
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "DataSocket.h"
|
||||
|
||||
#include "ServerInterface2.h"
|
||||
#include "network_utils.h"
|
||||
#include <cstdint>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
@ -13,16 +14,20 @@ namespace sls {
|
||||
class ServerSocket : public DataSocket {
|
||||
public:
|
||||
ServerSocket(int port);
|
||||
DataSocket accept();
|
||||
const std::string &getLastClient();
|
||||
ServerInterface2 accept();
|
||||
IpAddr getLastClient() noexcept { return lastClient; }
|
||||
IpAddr getThisClient() noexcept { return thisClient; }
|
||||
IpAddr getLockedBy() noexcept { return lockedBy; }
|
||||
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);
|
||||
void SendResult(int &ret, void *retval, int retvalSize, char *mess);
|
||||
|
||||
private:
|
||||
std::string lastClient_ = std::string(INET_ADDRSTRLEN, '\0');
|
||||
std::string thisClient_ = std::string(INET_ADDRSTRLEN, '\0');
|
||||
IpAddr thisClient;
|
||||
IpAddr lastClient;
|
||||
IpAddr lockedBy;
|
||||
int serverPort;
|
||||
// char lastClient_[INET_ADDRSTRLEN]{};
|
||||
};
|
||||
|
||||
}; // namespace sls
|
@ -4,13 +4,12 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
|
||||
class IpAddr {
|
||||
private:
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
constexpr IpAddr() noexcept{}
|
||||
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
@ -57,6 +56,10 @@ class MacAddr {
|
||||
constexpr uint64_t uint64() const noexcept { return addr_; }
|
||||
};
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
std::string IpToInterfaceName(const std::string& ip);
|
||||
MacAddr InterfaceNameToMac(std::string inf);
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr);
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -39,18 +40,31 @@ DataSocket &DataSocket::operator=(DataSocket &&move) noexcept {
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t DataSocket::receiveData(void *buffer, size_t size) {
|
||||
int DataSocket::receiveData(void *buffer, size_t size) {
|
||||
size_t dataRead = 0;
|
||||
while (dataRead < size) {
|
||||
dataRead +=
|
||||
read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead,
|
||||
::read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead,
|
||||
size - dataRead);
|
||||
}
|
||||
return dataRead;
|
||||
}
|
||||
|
||||
size_t DataSocket::sendData(const void *buffer, size_t size) {
|
||||
size_t dataSent = 0;
|
||||
int DataSocket::read(void *buffer, size_t size){
|
||||
return ::read(getSocketId(), reinterpret_cast<char *>(buffer), size);
|
||||
}
|
||||
|
||||
int DataSocket::setReceiveTimeout(int us) {
|
||||
timeval t{};
|
||||
t.tv_sec = 0;
|
||||
t.tv_usec = us;
|
||||
return ::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t,
|
||||
sizeof(struct timeval));
|
||||
}
|
||||
|
||||
|
||||
int DataSocket::sendData(const void *buffer, size_t size) {
|
||||
int dataSent = 0;
|
||||
while (dataSent < size) {
|
||||
dataSent +=
|
||||
write(getSocketId(), reinterpret_cast<const char *>(buffer) + dataSent,
|
||||
|
38
slsSupportLib/src/ServerInterface2.cpp
Normal file
38
slsSupportLib/src/ServerInterface2.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "ServerInterface2.h"
|
||||
#include <cassert>
|
||||
namespace sls {
|
||||
|
||||
int ServerInterface2::sendResult(bool update, int ret, void *retval,
|
||||
int retvalSize, char *mess) {
|
||||
|
||||
// if (update && ret == defs::OK && server_->DifferentClients()) {
|
||||
// ret = defs::FORCE_UPDATE;
|
||||
// }
|
||||
sendData(&ret, sizeof(ret));
|
||||
if (ret == defs::FAIL) {
|
||||
// send error message
|
||||
if (mess)
|
||||
sendData(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";
|
||||
}
|
||||
sendData(retval, retvalSize);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ServerInterface2::receiveArg(int &ret, char *mess, 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;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sls
|
@ -1,4 +1,6 @@
|
||||
#include "ServerInterface2.h"
|
||||
#include "ServerSocket.h"
|
||||
|
||||
#include "DataSocket.h"
|
||||
#include "logger.h"
|
||||
#include "sls_detector_defs.h"
|
||||
@ -27,7 +29,7 @@ ServerSocket::ServerSocket(int port)
|
||||
if (bind(getSocketId(), (struct sockaddr *)&serverAddr,
|
||||
sizeof(serverAddr)) != 0) {
|
||||
close();
|
||||
throw std::runtime_error("Server ERROR: cannot bind socket");
|
||||
throw sls::SocketError("Server ERROR: cannot bind socket");
|
||||
}
|
||||
if (listen(getSocketId(), DEFAULT_BACKLOG) != 0) {
|
||||
close();
|
||||
@ -35,25 +37,21 @@ ServerSocket::ServerSocket(int port)
|
||||
}
|
||||
}
|
||||
|
||||
DataSocket ServerSocket::accept() {
|
||||
ServerInterface2 ServerSocket::accept() {
|
||||
lastClient = thisClient; //update from previous connection
|
||||
struct sockaddr_in clientAddr;
|
||||
socklen_t addr_size = sizeof clientAddr;
|
||||
int newSocket =
|
||||
::accept(getSocketId(), (struct sockaddr *)&clientAddr, &addr_size);
|
||||
if (newSocket == -1) {
|
||||
throw std::runtime_error("Server ERROR: socket accept failed\n");
|
||||
throw sls::SocketError("Server ERROR: socket accept failed\n");
|
||||
}
|
||||
inet_ntop(AF_INET, &(clientAddr.sin_addr), &thisClient_.front(),
|
||||
INET_ADDRSTRLEN);
|
||||
std::cout << "lastClient: " << lastClient_ << " thisClient: " << thisClient_
|
||||
<< '\n';
|
||||
// Here goes any check for locks etc
|
||||
lastClient_ = thisClient_;
|
||||
|
||||
return DataSocket(newSocket);
|
||||
char tc[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &(clientAddr.sin_addr), tc, INET_ADDRSTRLEN);
|
||||
thisClient = tc;
|
||||
return ServerInterface2(newSocket);
|
||||
}
|
||||
|
||||
const std::string &ServerSocket::getLastClient() { return lastClient_; }
|
||||
|
||||
int ServerSocket::getPort() const { return serverPort; }
|
||||
|
||||
|
@ -6,16 +6,20 @@
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include <sys/prctl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include "network_utils.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
|
||||
IpAddr::IpAddr(const std::string &address) {
|
||||
inet_pton(AF_INET, address.c_str(), &addr_);
|
||||
}
|
||||
@ -86,4 +90,60 @@ uint32_t HostnameToIp(const char *hostname) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
std::string IpToInterfaceName(const std::string &ip) {
|
||||
//TODO! Copied from genericSocket needs to be refactored!
|
||||
struct ifaddrs *addrs, *iap;
|
||||
struct sockaddr_in *sa;
|
||||
|
||||
char buf[32];
|
||||
const int buf_len = sizeof(buf);
|
||||
memset(buf, 0, buf_len);
|
||||
strcpy(buf, "none");
|
||||
|
||||
getifaddrs(&addrs);
|
||||
for (iap = addrs; iap != NULL; iap = iap->ifa_next) {
|
||||
if (iap->ifa_addr && (iap->ifa_flags & IFF_UP) &&
|
||||
iap->ifa_addr->sa_family == AF_INET) {
|
||||
sa = (struct sockaddr_in *)(iap->ifa_addr);
|
||||
inet_ntop(iap->ifa_addr->sa_family, (void *)&(sa->sin_addr), buf,
|
||||
buf_len);
|
||||
if (ip == std::string(buf)) {
|
||||
strcpy(buf, iap->ifa_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
freeifaddrs(addrs);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
MacAddr InterfaceNameToMac(std::string inf) {
|
||||
//TODO! Copied from genericSocket needs to be refactored!
|
||||
struct ifreq ifr;
|
||||
char mac[32];
|
||||
const int mac_len = sizeof(mac);
|
||||
memset(mac,0,mac_len);
|
||||
|
||||
int sock=socket(PF_INET, SOCK_STREAM, 0);
|
||||
strncpy(ifr.ifr_name,inf.c_str(),sizeof(ifr.ifr_name)-1);
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';
|
||||
|
||||
|
||||
if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
|
||||
perror("ioctl(SIOCGIFHWADDR) ");
|
||||
return std::string("00:00:00:00:00:00");
|
||||
}
|
||||
for (int j=0, k=0; j<6; j++) {
|
||||
k+=snprintf(mac+k, mac_len-k-1, j ? ":%02X" : "%02X",
|
||||
(int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
|
||||
}
|
||||
mac[mac_len-1]='\0';
|
||||
|
||||
if(sock!=1){
|
||||
close(sock);
|
||||
}
|
||||
return MacAddr(mac);
|
||||
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
@ -101,4 +101,4 @@ TEST_CASE("MAC Output operator gives same result as string", "[support]") {
|
||||
CHECK(os.str() == addr.str());
|
||||
}
|
||||
|
||||
//TODO!(Erik) Look up a real hostname and verify the IP
|
||||
//TODO!(Erik) Look up a real hostname and verify the IP
|
||||
|
Loading…
x
Reference in New Issue
Block a user