From 14da1245b43967c534889e672519a819c1cbdd53 Mon Sep 17 00:00:00 2001 From: Alice Date: Fri, 20 Feb 2026 16:58:10 +0100 Subject: [PATCH] WIP --- .../matterhonServer/include/StopServer.h | 13 ++-- .../matterhonServer/src/MatterhornApp.cpp | 30 ++++----- .../src/MatterhornClientInterface.cpp | 24 ++++++-- .../matterhonServer/src/StopServer.cpp | 9 ++- .../include/ClientInterface.h | 8 +-- .../slsDetectorServer/src/ClientInterface.cpp | 61 +++++++++++++------ slsDetectorSoftware/src/Detector.cpp | 4 ++ slsDetectorSoftware/src/DetectorImpl.cpp | 4 +- slsDetectorSoftware/src/Module.cpp | 26 ++++++++ slsReceiverSoftware/src/ClientInterface.cpp | 2 +- slsSupportLib/include/sls/DataSocket.h | 3 + slsSupportLib/include/sls/ServerInterface.h | 5 ++ slsSupportLib/src/ClientSocket.cpp | 5 ++ slsSupportLib/src/DataSocket.cpp | 7 ++- slsSupportLib/src/ServerSocket.cpp | 6 ++ 15 files changed, 156 insertions(+), 51 deletions(-) diff --git a/slsDetectorServers/matterhonServer/include/StopServer.h b/slsDetectorServers/matterhonServer/include/StopServer.h index 91f85b1f7..f7a195c32 100644 --- a/slsDetectorServers/matterhonServer/include/StopServer.h +++ b/slsDetectorServers/matterhonServer/include/StopServer.h @@ -1,9 +1,14 @@ +#include "MatterhornServer.h" #include -// TODO: should this inherit from MatterhornServer? -class StopServer { +namespace sls { + +// TODO: should this inherit from MatterhornServer or a base class +class StopServer : public MatterhornServer { public: StopServer(uint16_t port); - ~StopServer(); -}; \ No newline at end of file + ~StopServer() = default; +}; + +} // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp index 43381ff26..29ab873d7 100644 --- a/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhonServer/src/MatterhornApp.cpp @@ -31,12 +31,12 @@ void sigInterruptHandler(int signal) { if (child_pid > 0) { kill(child_pid, SIGTERM); // tell child to exit } - sem_post(&semaphore); + // sem_post(&semaphore); } void childSigTermHandler(int signal) { (void)signal; // suppress unused warning if needed - sem_post(&semaphore); + // sem_post(&semaphore); } // TODO: should be a generic ServerApp for all detectors @@ -54,28 +54,29 @@ int main(int argc, char *argv[]) { } // Register Ctrl+C handler - std::signal(SIGINT, sigInterruptHandler); + // std::signal(SIGINT, sigInterruptHandler); // LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]"; // handle locally on socket crash // sls::setupSignalHandler(SIGPIPE, SIG_IGN); / what is this? - sem_init(&semaphore, 1, 0); + // sem_init(&semaphore, 1, 0); child_pid = fork(); // fork process for control and stop server if (child_pid == 0) { // Stop server Process - std::signal(SIGTERM, childSigTermHandler); + signal(SIGPIPE, SIG_IGN); + + // std::signal(SIGTERM, childSigTermHandler); LOG(TLogLevel::logINFOBLUE) << "Stop Server [" << opts.port + 1 << "]"; try { - // StopServer stopServer(opts.port + 1); TODO: forget the stop - // server for now - sem_wait(&semaphore); // wait until parent signals to exit - sem_destroy(&semaphore); + MatterhornServer stopServer(opts.port + 1); + // sem_wait(&semaphore); // wait until parent signals to exit + // sem_destroy(&semaphore); } catch (...) { - sem_destroy(&semaphore); + // sem_destroy(&semaphore); LOG(TLogLevel::logINFOBLUE) << "Exiting Stop Server [ Tid: " << gettid() << " ]"; // TODO: maybe also terminate the control server !!!! @@ -87,6 +88,8 @@ int main(int argc, char *argv[]) { exit(EXIT_SUCCESS); } else if (child_pid > 0) { // Control Server Process + signal(SIGPIPE, SIG_IGN); + LOG(TLogLevel::logINFOBLUE) << "Control Server [" << opts.port << "]\n"; if (opts.updateFlag == 0) { @@ -96,11 +99,10 @@ int main(int argc, char *argv[]) { try { sls::MatterhornServer server(opts.port); LOG(sls::logINFO) << "[ Press \'Ctrl+c\' to exit ]"; - // exit upon ctr + c - sem_wait(&semaphore); - sem_destroy(&semaphore); + // sem_wait(&semaphore); + // sem_destroy(&semaphore); } catch (...) { - sem_destroy(&semaphore); + // 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 bd1551e5b..b47641fc0 100644 --- a/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp +++ b/slsDetectorServers/matterhonServer/src/MatterhornClientInterface.cpp @@ -18,21 +18,37 @@ MatterhornClientInterface::MatterhornClientInterface(const uint16_t portNumber) [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); + }); + + startTCPServer(); } ReturnCode MatterhornClientInterface::get_version(ServerInterface &socket) { auto version = getMatterhornServerVersion(); - version.resize(MAX_STR_LENGTH); + 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"; return static_cast(socket.sendResult( - version)); // TODO: check what would be possible return codes!!! + version_cstr)); // TODO: check what would be possible return codes!!! } ReturnCode MatterhornClientInterface::get_detector_type(ServerInterface &socket) { - return static_cast( - socket.sendResult(slsDetectorDefs::detectorType::MATTERHORN)); + int detectortype = slsDetectorDefs::detectorType::MATTERHORN; + return static_cast(socket.sendResult(detectortype)); } std::string MatterhornClientInterface::getMatterhornServerVersion() { diff --git a/slsDetectorServers/matterhonServer/src/StopServer.cpp b/slsDetectorServers/matterhonServer/src/StopServer.cpp index 5c1f9e965..82e7c5ec6 100644 --- a/slsDetectorServers/matterhonServer/src/StopServer.cpp +++ b/slsDetectorServers/matterhonServer/src/StopServer.cpp @@ -1,12 +1,17 @@ + #include "StopServer.h" #include "sls/network_utils.h" #include "sls/sls_detector_defs.h" -StopServer::StopServer(uint16_t port) { - validatePortNumber(port); +namespace sls { +StopServer::StopServer(uint16_t port) : MatterhornServer(port) { + /* // open shared memory segment of control server map to virtual memory space if (sharedMemory_open(port - 1) == slsDetectorDefs::FAIL) { throw sls::RuntimeError("Failed to open shared memory"); } + */ } + +} // namespace sls diff --git a/slsDetectorServers/slsDetectorServer/include/ClientInterface.h b/slsDetectorServers/slsDetectorServer/include/ClientInterface.h index f39d5555b..09080d351 100644 --- a/slsDetectorServers/slsDetectorServer/include/ClientInterface.h +++ b/slsDetectorServers/slsDetectorServer/include/ClientInterface.h @@ -21,6 +21,9 @@ class ClientInterface { std::unordered_map> functionTable{}; // set in constructor of child process + /// @brief starts the TCP/IP server to listen for client commands + void startTCPServer(); + private: /// @brief listener thread for TCP/IP communication with the client std::unique_ptr tcpThread; @@ -45,10 +48,7 @@ class ClientInterface { // std::string getReceiverVersion(); private: - /// @brief starts the TCP/IP server to listen for client commands - void startTCPServer(); - - /** + /** * @brief decodes the received command and calls the corresponding function * @param function_id The ID of the function recived by the server and to * be executed diff --git a/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp b/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp index 7a04b9a7c..b161a9897 100644 --- a/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp +++ b/slsDetectorServers/slsDetectorServer/src/ClientInterface.cpp @@ -11,13 +11,11 @@ ClientInterface::ClientInterface(const uint16_t portNumber) : portNumber(portNumber), server(portNumber) { validatePortNumber(portNumber); // parentThreadId = gettid(); - tcpThread = - std::make_unique(&ClientInterface::startTCPServer, this); } ClientInterface::~ClientInterface() { killTcpThread = true; - LOG(logINFO) << "Shutting down TCP Socket on port " << portNumber; + LOG(logINFORED) << "Shutting down TCP Socket on port " << portNumber; server.shutdown(); LOG(logDEBUG) << "TCP Socket closed on port " << portNumber; @@ -26,57 +24,80 @@ ClientInterface::~ClientInterface() { receiver->shutDownUDPSockets(); } */ - - tcpThread->join(); } void ClientInterface::startTCPServer() { - const pid_t tcpThreadId = gettid(); - LOG(logINFOBLUE) << "Created [ TCP server Tid: " << tcpThreadId << "]"; - LOG(logINFO) << "SLS Receiver starting TCP Server on port " << portNumber + + LOG(logINFO) << "SLS Server starting TCP Server on port " << portNumber << '\n'; int function_id{}; // TODO should it be an enum type - while (!killTcpThread) { + while (true) { LOG(logDEBUG1) << "Start accept loop"; 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); - processReceivedData(static_cast(function_id), socket); + if (function_id < 0 || function_id >= NUM_DET_FUNCTIONS) { + throw RuntimeError(UNRECOGNIZED_FNUM_ENUM + + std::to_string(function_id)); + } + auto returncode = processReceivedData( + static_cast(function_id), socket); + + if (returncode == FAIL) { + LOG(logERROR) << "Error processing command with fnum: " + << function_id; + } } catch (const RuntimeError &e) { // We had an error needs to be sent to client char mess[MAX_STR_LENGTH]{}; + LOG(logERROR) << "Error processing command: " << e.what(); strcpy_safe(mess, e.what()); - socket.Send(FAIL); + socket.Send(slsDetectorDefs::FAIL); socket.Send(mess); } // TODO handle exiting server if tcp command was to exit server } catch (const RuntimeError &e) { - LOG(logERROR) << "Accept failed"; + LOG(logERROR) << "Accept failed: " << e.what(); } } - LOG(logINFOBLUE) << "Exiting [ TCP server Tid: " << tcpThreadId << "]"; + LOG(logINFOBLUE) << "Exiting TCP Server"; } ReturnCode ClientInterface::processReceivedData(const detFuncs function_id, ServerInterface &socket) { // TODO: is NUM_DET_FUNCTIONS correct? - if (function_id < 0 || function_id >= NUM_DET_FUNCTIONS) { - throw RuntimeError(UNRECOGNIZED_FNUM_ENUM + + + 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); + }); + + auto function = functionTable.find(function_id); + if (function == functionTable.end()) { + throw RuntimeError("unrecognized Function id: " + std::to_string(function_id)); } - LOG(logDEBUG1) << "calling function fnum: " << function_id << " (" - << getFunctionNameFromEnum((enum detFuncs)function_id) - << ")"; - ReturnCode returncode = (functionTable[function_id])( - socket); // how does it pass input arguments? + + ReturnCode returncode = + function->second(socket); // how does it pass input arguments? LOG(logDEBUG1) << "Function " << getFunctionNameFromEnum((enum detFuncs)function_id) << " finished"; diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 39706ec56..d0d721ac2 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -174,16 +174,20 @@ Result Detector::getHostname(Positions pos) const { } void Detector::setHostname(const std::vector &hostname) { + LOG(logINFORED) << "test"; + LOG(logDEBUG) << "Checking if hostname is already in shared memory"; if (pimpl->hasModulesInSharedMemory()) { LOG(logWARNING) << "There are already module(s) in shared memory." "Freeing Shared memory now."; auto numChannels = getDetectorSize(); + LOG(logDEBUG) << "Current detector size: " << numChannels; auto initialChecks = getInitialChecks(); freeSharedMemory(getShmId()); pimpl = make_unique(getShmId()); setDetectorSize(numChannels); setInitialChecks(initialChecks); } + LOG(logDEBUG) << "not in shared memory yet, setting hostname"; pimpl->setHostname(hostname); } diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index 0f48a6b6b..8451874b1 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -193,10 +193,12 @@ void DetectorImpl::setHostname(const std::vector &name) { } ctb_shm.createSharedMemory(); } + + LOG(logDEBUG) << "created shared memory"; } void DetectorImpl::addModule(const std::string &name) { - LOG(logINFO) << "Adding module " << name; + LOG(TLogLevel::logDEBUG) << "Adding module " << name; auto host = verifyUniqueDetHost(name); std::string hostname = host.first; uint16_t port = host.second; diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 8a97afb89..65ed68af4 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -50,6 +50,7 @@ std::string Module::getHostname() const { return shm()->hostname; } void Module::setHostname(const std::string &hostname, const bool initialChecks) { strcpy_safe(shm()->hostname, hostname.c_str()); + LOG(logDEBUG1) << "control port: " << shm()->controlPort; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.close(); try { @@ -87,14 +88,23 @@ std::string Module::getControlServerLongVersion() const { // throw with old server version (sends 8 bytes) catch (RuntimeError &e) { std::string emsg = std::string(e.what()); + LOG(logDEBUG) << "Error message: " << emsg; + + /* if (emsg.find(F_GET_SERVER_VERSION) && emsg.find("8 bytes")) { throwDeprecatedServerVersion(); } + */ + + LOG(logINFORED) << "Before throw"; throw; + LOG(logINFORED) << "Well it should have thrown by now"; } + LOG(logINFORED) << "After catch, should not be here"; } void Module::throwDeprecatedServerVersion() const { + LOG(logDEBUG1) << "throw deprecated version error"; uint64_t res = sendToDetectorStop(F_GET_SERVER_VERSION); std::cout << std::endl; std::ostringstream os; @@ -104,6 +114,7 @@ void Module::throwDeprecatedServerVersion() const { } std::string Module::getStopServerLongVersion() const { + LOG(logDEBUG1) << "Getting Stop Server Version"; char retval[MAX_STR_LENGTH]{}; sendToDetectorStop(F_GET_SERVER_VERSION, nullptr, retval); return retval; @@ -147,6 +158,8 @@ std::string Module::getReceiverSoftwareVersion() const { slsDetectorDefs::detectorType Module::getTypeFromDetector(const std::string &hostname, uint16_t cport) { LOG(logDEBUG1) << "Getting Module type "; + LOG(logDEBUG1) << "ClientSocket: Hostname: " << hostname + << " Port: " << cport; ClientSocket socket("Detector", hostname, cport); socket.Send(F_GET_DETECTOR_TYPE); socket.setFnum(F_GET_DETECTOR_TYPE); @@ -3051,7 +3064,10 @@ void Module::sendToDetector(int fnum, const void *args, size_t args_size, // the other versions use templates to deduce sizes and create // the return type checkArgs(args, args_size, retval, retval_size); + LOG(logDEBUG1) << "Creating DetectorSocket on: " << shm()->hostname << ":" + << shm()->controlPort; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); + LOG(logDEBUG1) << "sending command then read to DetectorSocket"; client.sendCommandThenRead(fnum, args, args_size, retval, retval_size); client.close(); } @@ -3064,6 +3080,7 @@ 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() << ", " @@ -3096,6 +3113,7 @@ 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() << ", " @@ -3122,6 +3140,7 @@ 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() << ", " @@ -3163,8 +3182,10 @@ void Module::sendToDetectorStop(int fnum, const void *args, size_t args_size, // This is the only function that actually sends data to the detector stop // the other versions use templates to deduce sizes and create // the return type + LOG(logINFORED) << "Sending command to Detector Stop Socket"; checkArgs(args, args_size, retval, retval_size); auto stop = DetectorSocket(shm()->hostname, shm()->stopPort); + LOG(logDEBUG1) << "sending command then read to Detector Stop Socket"; stop.sendCommandThenRead(fnum, args, args_size, retval, retval_size); stop.close(); } @@ -3287,6 +3308,7 @@ void Module::sendToReceiver(int fnum, const void *args, size_t args_size, } checkArgs(args, args_size, retval, retval_size); auto receiver = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort); + LOG(logDEBUG1) << "sending command then read to ReceiverSocket"; receiver.sendCommandThenRead(fnum, args, args_size, retval, retval_size); receiver.close(); } @@ -3465,8 +3487,12 @@ void Module::initialDetectorServerChecks() { } void Module::checkDetectorVersionCompatibility() { + LOG(logDEBUG) << "Checking detector version compatibility with client..."; std::string detServers[2] = {getControlServerLongVersion(), getStopServerLongVersion()}; + LOG(logINFO) + << "Checking detector version compatibility with client version " + << detServers[0] << " and " << detServers[1]; for (int i = 0; i != 2; ++i) { // det and client (sem. versioning) Version det(detServers[i]); diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index 7acf59e2e..133a12cc9 100644 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -96,7 +96,7 @@ void ClientInterface::startTCPServer() { // We had an error needs to be sent to client char mess[MAX_STR_LENGTH]{}; strcpy_safe(mess, e.what()); - socket.Send(FAIL); + socket.Send(slsDetectorDefs::FAIL); socket.Send(mess); } // if tcp command was to exit server diff --git a/slsSupportLib/include/sls/DataSocket.h b/slsSupportLib/include/sls/DataSocket.h index 01215c20e..6f14945d3 100644 --- a/slsSupportLib/include/sls/DataSocket.h +++ b/slsSupportLib/include/sls/DataSocket.h @@ -3,6 +3,7 @@ #pragma once #include "sls/TypeTraits.h" +#include "sls/logger.h" #include #include #include @@ -43,6 +44,8 @@ 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 475201983..c36caedf2 100644 --- a/slsSupportLib/include/sls/ServerInterface.h +++ b/slsSupportLib/include/sls/ServerInterface.h @@ -3,6 +3,8 @@ #pragma once #include "sls/DataSocket.h" +#include "sls/logger.h" + namespace sls { class ServerInterface; } @@ -25,6 +27,9 @@ 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/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index 117e7621c..d5e908e1e 100644 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -67,7 +67,9 @@ int ClientSocket::sendCommandThenRead(int fnum, const void *args, int ret = slsDetectorDefs::FAIL; Send(&fnum, sizeof(fnum)); setFnum(fnum); + LOG(logDEBUG1) << "Sent command fnum: " << fnum << " to " << socketType; Send(args, args_size); + LOG(logDEBUG1) << "About to read reply"; readReply(ret, retval, retval_size); return ret; } @@ -76,6 +78,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { try { Receive(&ret, sizeof(ret)); + // Receive() if (ret == slsDetectorDefs::FAIL) { std::string mess = readErrorMessage(); // Do we need to know hostname here? @@ -90,6 +93,8 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { } // get retval Receive(retval, retval_size); + LOG(logDEBUG1) << "Received size " << retval_size << " bytes from " + << socketType << " with return code: " << ret; } // debugging catch (SocketError &e) { diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index 25d016d0b..6fbdc1303 100644 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -50,6 +50,7 @@ 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 "; while (bytes_read < bytes_expected) { auto this_read = ::read(getSocketId(), reinterpret_cast(buffer) + bytes_read, @@ -80,6 +81,7 @@ std::string DataSocket::Receive(size_t length) { int DataSocket::Send(const void *buffer, size_t size) { int bytes_sent = 0; int data_size = static_cast(size); // signed size + LOG(logDEBUG1) << "Sending " << data_size << " bytes on socket "; while (bytes_sent < (data_size)) { auto this_send = ::write(getSocketId(), buffer, size); if (this_send <= 0) @@ -96,7 +98,10 @@ int DataSocket::Send(const void *buffer, size_t size) { return bytes_sent; } -int DataSocket::Send(const std::string &s) { return Send(&s[0], s.size()); } +int DataSocket::Send(const std::string &s) { + LOG(logDEBUG1) << "Sending string of size: " << sizeof(s) << " bytes"; + return Send(&s[0], s.size()); +} int DataSocket::write(void *buffer, size_t size) { return ::write(getSocketId(), buffer, size); diff --git a/slsSupportLib/src/ServerSocket.cpp b/slsSupportLib/src/ServerSocket.cpp index e61c5d132..c69387dfb 100644 --- a/slsSupportLib/src/ServerSocket.cpp +++ b/slsSupportLib/src/ServerSocket.cpp @@ -52,9 +52,15 @@ ServerInterface ServerSocket::accept() { if (newSocket == -1) { throw SocketError("Server ERROR: socket accept failed\n"); } + char tc[INET_ADDRSTRLEN]{}; inet_ntop(AF_INET, &(clientAddr.sin_addr), tc, INET_ADDRSTRLEN); thisClient = IpAddr{tc}; + LOG(logDEBUG1) << "Accepted connection from: " << tc << ":" + << ntohs(clientAddr.sin_port); + LOG(logDEBUG1) << "Last client was: " << lastClient.str(); + LOG(logDEBUG1) << "new client is: " << thisClient.str(); + // Set socket buffer size return ServerInterface(newSocket); }