diff --git a/slsDetectorServers/matterhonServer/include/MatterhornClientInterface.h b/slsDetectorServers/matterhonServer/include/MatterhornClientInterface.h index 3fb707511..fa22983fb 100644 --- a/slsDetectorServers/matterhonServer/include/MatterhornClientInterface.h +++ b/slsDetectorServers/matterhonServer/include/MatterhornClientInterface.h @@ -15,6 +15,10 @@ class MatterhornClientInterface : public ClientInterface { ReturnCode get_detector_type(ServerInterface &socket); + ReturnCode initial_checks(ServerInterface &socket); + + ReturnCode get_num_udp_interfaces(ServerInterface &socket); + static std::string getMatterhornServerVersion(); }; diff --git a/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp index 29ab873d7..1fd73a154 100644 --- a/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp @@ -18,8 +18,6 @@ using namespace sls; -sem_t semaphore; - pid_t child_pid = -1; /** @@ -31,12 +29,7 @@ void sigInterruptHandler(int signal) { if (child_pid > 0) { kill(child_pid, SIGTERM); // tell child to exit } - // sem_post(&semaphore); -} - -void childSigTermHandler(int signal) { - (void)signal; // suppress unused warning if needed - // sem_post(&semaphore); + std::exit(EXIT_SUCCESS); } // TODO: should be a generic ServerApp for all detectors @@ -54,15 +47,11 @@ int main(int argc, char *argv[]) { } // Register Ctrl+C handler - // std::signal(SIGINT, sigInterruptHandler); - - // LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]"; + std::signal(SIGINT, sigInterruptHandler); // handle locally on socket crash // sls::setupSignalHandler(SIGPIPE, SIG_IGN); / what is this? - // sem_init(&semaphore, 1, 0); - child_pid = fork(); // fork process for control and stop server if (child_pid == 0) { @@ -73,10 +62,7 @@ int main(int argc, char *argv[]) { LOG(TLogLevel::logINFOBLUE) << "Stop Server [" << opts.port + 1 << "]"; try { MatterhornServer stopServer(opts.port + 1); - // sem_wait(&semaphore); // wait until parent signals to exit - // sem_destroy(&semaphore); } catch (...) { - // sem_destroy(&semaphore); LOG(TLogLevel::logINFOBLUE) << "Exiting Stop Server [ Tid: " << gettid() << " ]"; // TODO: maybe also terminate the control server !!!! @@ -92,17 +78,9 @@ int main(int argc, char *argv[]) { LOG(TLogLevel::logINFOBLUE) << "Control Server [" << opts.port << "]\n"; - if (opts.updateFlag == 0) { - // update flag if update file exists (command line arg overwrites) - } - try { sls::MatterhornServer server(opts.port); - LOG(sls::logINFO) << "[ Press \'Ctrl+c\' to exit ]"; - // sem_wait(&semaphore); - // sem_destroy(&semaphore); } catch (...) { - // sem_destroy(&semaphore); kill(child_pid, SIGTERM); // tell child to exit LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]"; std::exit(EXIT_FAILURE); diff --git a/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp b/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp index b47641fc0..5c4a92151 100644 --- a/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp +++ b/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp @@ -17,17 +17,9 @@ MatterhornClientInterface::MatterhornClientInterface(const uint16_t portNumber) {detFuncs::F_GET_SERVER_VERSION, [this](ServerInterface &si) { return this->get_version(si); }}, {detFuncs::F_GET_DETECTOR_TYPE, - [this](ServerInterface &si) { return this->get_detector_type(si); }}}; - - LOG(logDEBUG1) - << "Function table from child class MatterhornClientInterface: "; - std::for_each(functionTable.begin(), functionTable.end(), - [](const auto &pair) { - LOG(logDEBUG1) - << "Function id: " << pair.first - << ", Function name: " - << getFunctionNameFromEnum((enum detFuncs)pair.first); - }); + [this](ServerInterface &si) { return this->get_detector_type(si); }}, + {detFuncs::F_INITIAL_CHECKS, + [this](ServerInterface &si) { return this->initial_checks(si); }}}; startTCPServer(); } @@ -37,10 +29,7 @@ ReturnCode MatterhornClientInterface::get_version(ServerInterface &socket) { auto version = getMatterhornServerVersion(); char version_cstr[MAX_STR_LENGTH]{}; strncpy(version_cstr, version.c_str(), version.size()); - // version.resize(MAX_STR_LENGTH); - LOG(TLogLevel::logINFO) << "Matterhorn Server Version: " << version; - LOG(TLogLevel::logDEBUG1) - << "size of version: " << sizeof(version) << " bytes"; + LOG(TLogLevel::logDEBUG) << "Matterhorn Server Version: " << version; return static_cast(socket.sendResult( version_cstr)); // TODO: check what would be possible return codes!!! } @@ -55,4 +44,12 @@ std::string MatterhornClientInterface::getMatterhornServerVersion() { return APIMATTERHORN; } +ReturnCode MatterhornClientInterface::initial_checks(ServerInterface &socket) { + + // TODO: add more checks here, for now just return true to be able to test + // the should check firmware -client compatibility + bool initial_checks_passed = true; + return static_cast(socket.sendResult(initial_checks_passed)); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp b/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp index b161a9897..38c539234 100644 --- a/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp +++ b/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp @@ -1,8 +1,8 @@ #include "ClientInterface.h" +#include "fmt/format.h" #include "sls/logger.h" #include "sls/string_utils.h" - #include namespace sls { @@ -10,7 +10,6 @@ namespace sls { ClientInterface::ClientInterface(const uint16_t portNumber) : portNumber(portNumber), server(portNumber) { validatePortNumber(portNumber); - // parentThreadId = gettid(); } ClientInterface::~ClientInterface() { @@ -18,12 +17,6 @@ ClientInterface::~ClientInterface() { LOG(logINFORED) << "Shutting down TCP Socket on port " << portNumber; server.shutdown(); LOG(logDEBUG) << "TCP Socket closed on port " << portNumber; - - /* - if (receiver) { - receiver->shutDownUDPSockets(); - } - */ } void ClientInterface::startTCPServer() { @@ -37,23 +30,20 @@ void ClientInterface::startTCPServer() { try { auto socket = server.accept(); try { - // is this to check if I can process a command? or what is that? - /* - if (checkifReceiverLocked()) { - throw SocketError("Receiver locked\n"); - } - */ + socket.Receive(function_id); if (function_id < 0 || function_id >= NUM_DET_FUNCTIONS) { - throw RuntimeError(UNRECOGNIZED_FNUM_ENUM + - std::to_string(function_id)); + throw RuntimeError(fmt::format( + UNRECOGNIZED_FNUM_ENUM, + getFunctionNameFromEnum((enum detFuncs)function_id))); } auto returncode = processReceivedData( static_cast(function_id), socket); if (returncode == FAIL) { - LOG(logERROR) << "Error processing command with fnum: " - << function_id; + throw RuntimeError(fmt::format( + "Error processing command with fnum: {}", + getFunctionNameFromEnum((enum detFuncs)function_id))); } } catch (const RuntimeError &e) { @@ -78,26 +68,19 @@ ReturnCode ClientInterface::processReceivedData(const detFuncs function_id, // TODO: is NUM_DET_FUNCTIONS correct? LOG(logDEBUG1) << "calling function fnum: " << function_id << " (" - << getFunctionNameFromEnum((enum detFuncs)function_id) << ")" - << "from thread: " << gettid(); - - LOG(logDEBUG1) << "Available functions in the server:"; - std::for_each(functionTable.begin(), functionTable.end(), - [](const auto &pair) { - LOG(logDEBUG1) - << "Function id: " << pair.first - << ", Function name: " - << getFunctionNameFromEnum((enum detFuncs)pair.first); - }); + << getFunctionNameFromEnum((enum detFuncs)function_id) + << ")"; auto function = functionTable.find(function_id); if (function == functionTable.end()) { - throw RuntimeError("unrecognized Function id: " + - std::to_string(function_id)); + throw RuntimeError( + fmt::format("Function {} not found not implemented", + getFunctionNameFromEnum((enum detFuncs)function_id))); } ReturnCode returncode = function->second(socket); // how does it pass input arguments? + LOG(logDEBUG1) << "Function " << getFunctionNameFromEnum((enum detFuncs)function_id) << " finished"; @@ -105,8 +88,4 @@ ReturnCode ClientInterface::processReceivedData(const detFuncs function_id, return returncode; } -bool ClientInterface::checkifReceiverLocked() { - return lockedByClient && server.getThisClient() != server.getLockedBy(); -} - } // namespace sls \ No newline at end of file diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 32cb304f5..61bc33d60 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -91,9 +91,7 @@ std::string Module::getControlServerLongVersion() const { } throw; - LOG(logINFORED) << "Well it should have thrown by now"; } - LOG(logINFORED) << "After catch, should not be here"; } void Module::throwDeprecatedServerVersion() const { @@ -3118,7 +3116,6 @@ void Module::sendToDetector(int fnum, const void *args, size_t args_size, template void Module::sendToDetector(int fnum, const Arg &args, Ret &retval) const { - std::cout << "in line 3075\n"; LOG(logDEBUG1) << "Sending: [" << getFunctionNameFromEnum(static_cast(fnum)) << ", nullptr, 0, " << typeid(Ret).name() << ", " @@ -3151,7 +3148,6 @@ void Module::sendToDetector(int fnum, const Arg &args, std::nullptr_t) { template void Module::sendToDetector(int fnum, std::nullptr_t, Ret &retval) const { - std::cout << "in line 3107\n"; LOG(logDEBUG1) << "Sending: [" << getFunctionNameFromEnum(static_cast(fnum)) << ", nullptr, 0, " << typeid(Ret).name() << ", " @@ -3178,7 +3174,6 @@ void Module::sendToDetector(int fnum) { } template Ret Module::sendToDetector(int fnum) const { - std::cout << "in line 3135\n"; LOG(logDEBUG1) << "Sending: [" << getFunctionNameFromEnum(static_cast(fnum)) << ", nullptr, 0, " << typeid(Ret).name() << ", " diff --git a/slsSupportLib/include/sls/DataSocket.h b/slsSupportLib/include/sls/DataSocket.h index 6f14945d3..31d9afa72 100644 --- a/slsSupportLib/include/sls/DataSocket.h +++ b/slsSupportLib/include/sls/DataSocket.h @@ -44,8 +44,6 @@ class DataSocket { std::string>::value, int>::type Send(T &&data) { - LOG(TLogLevel::logDEBUG1) - << "Sending result in DataSocket: " << sizeof(data) << " bytes"; return Send(&data, sizeof(data)); } diff --git a/slsSupportLib/include/sls/ServerInterface.h b/slsSupportLib/include/sls/ServerInterface.h index c36caedf2..d4ed04576 100644 --- a/slsSupportLib/include/sls/ServerInterface.h +++ b/slsSupportLib/include/sls/ServerInterface.h @@ -27,9 +27,6 @@ class ServerInterface : public DataSocket { template int sendResult(T &&retval) { Send(defs::OK); - LOG(TLogLevel::logDEBUG1) - << "Sending result to Datasocket::Send: " << sizeof(retval) - << " bytes"; Send(retval); return defs::OK; } diff --git a/slsSupportLib/include/sls/logger.h b/slsSupportLib/include/sls/logger.h index 5754286e7..ebd75a678 100644 --- a/slsSupportLib/include/sls/logger.h +++ b/slsSupportLib/include/sls/logger.h @@ -97,8 +97,9 @@ class Logger { gettimeofday(&tv, nullptr); constexpr size_t result_len = 100; char result[result_len]; - snprintf(result, result_len, "%s.%03ld", buffer, - (long)tv.tv_usec / 1000); + // snprintf(result, result_len, "%s.%03ld", buffer, + //(long)tv.tv_usec / 1000); + snprintf(result, result_len, "%s.%03ld", buffer, (long)tv.tv_usec); result[result_len - 1] = '\0'; return result; } diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index 6fbdc1303..308913811 100644 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -50,7 +50,8 @@ int DataSocket::Receive(void *buffer, size_t size) { // TODO!(Erik) Add sleep? how many reties? int bytes_expected = static_cast(size); // signed size int bytes_read = 0; - LOG(logDEBUG1) << "Receiving " << bytes_expected << " bytes on socket "; + LOG(logDEBUG1) << "Should receive " << bytes_expected + << " bytes on socket "; while (bytes_read < bytes_expected) { auto this_read = ::read(getSocketId(), reinterpret_cast(buffer) + bytes_read, @@ -98,10 +99,7 @@ int DataSocket::Send(const void *buffer, size_t size) { return bytes_sent; } -int DataSocket::Send(const std::string &s) { - LOG(logDEBUG1) << "Sending string of size: " << sizeof(s) << " bytes"; - return Send(&s[0], s.size()); -} +int DataSocket::Send(const std::string &s) { return Send(&s[0], s.size()); } int DataSocket::write(void *buffer, size_t size) { return ::write(getSocketId(), buffer, size); @@ -151,6 +149,8 @@ void DataSocket::close() { } else { throw std::runtime_error("Socket ERROR: close called on bad socket\n"); } + + LOG(logDEBUG1) << "Closed socket with id: " << getSocketId(); } void DataSocket::shutDownSocket() { diff --git a/slsSupportLib/src/ServerSocket.cpp b/slsSupportLib/src/ServerSocket.cpp index 08c12b562..58f6c70ac 100644 --- a/slsSupportLib/src/ServerSocket.cpp +++ b/slsSupportLib/src/ServerSocket.cpp @@ -44,6 +44,7 @@ ServerSocket::ServerSocket(int port) } ServerInterface ServerSocket::accept() { + LOG(logDEBUG1) << "In accept function, waiting for client to connect..."; lastClient = thisClient; // update from previous connection struct sockaddr_in clientAddr; socklen_t addr_size = sizeof clientAddr;