diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index 91e81e90c..1efa837e5 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -271,7 +271,7 @@ class Detector(CppDetectorApi): @property @element def rx_arping(self): - """Starts a thread in slsReceiver to ping the interface it is listening every minute. Useful in 10G mode. """ + """Starts a thread in slsReceiver to arping the interface it is listening every minute. Useful in 10G mode. """ return self.getRxArping() @rx_arping.setter diff --git a/slsDetectorSoftware/include/sls/Detector.h b/slsDetectorSoftware/include/sls/Detector.h index a842dd92f..1f16b7269 100644 --- a/slsDetectorSoftware/include/sls/Detector.h +++ b/slsDetectorSoftware/include/sls/Detector.h @@ -886,7 +886,7 @@ class Detector { Result getRxArping(Positions pos = {}) const; - /** Starts a thread in slsReceiver to ping the interface it is listening + /** Starts a thread in slsReceiver to arping the interface it is listening * every minute. Useful in 10G mode. */ void setRxArping(bool value, Positions pos = {}); diff --git a/slsDetectorSoftware/src/CmdProxy.h b/slsDetectorSoftware/src/CmdProxy.h index 7674eb53d..ac1afb4ad 100644 --- a/slsDetectorSoftware/src/CmdProxy.h +++ b/slsDetectorSoftware/src/CmdProxy.h @@ -1745,10 +1745,10 @@ class CmdProxy { "processor 1, streamer 1, arping]. If no streamer yet or there " "is no second interface, it gives 0 in its place."); - INTEGER_COMMAND_VEC_ID( - rx_arping, getRxArping, setRxArping, StringTo, - "[0, 1]\n\tStarts a thread in slsReceiver to ping the interface it is " - "listening to every minute. Useful in 10G mode."); + INTEGER_COMMAND_VEC_ID(rx_arping, getRxArping, setRxArping, StringTo, + "[0, 1]\n\tStarts a thread in slsReceiver to arping " + "the interface it is " + "listening to every minute. Useful in 10G mode."); /* File */ diff --git a/slsReceiverSoftware/src/Arping.cpp b/slsReceiverSoftware/src/Arping.cpp new file mode 100644 index 000000000..6b7bfd3eb --- /dev/null +++ b/slsReceiverSoftware/src/Arping.cpp @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: LGPL-3.0-or-other +// Copyright (C) 2021 Contributors to the SLS Detector Package + +#include "Arping.h" + +#include + +const std::string Arping::ThreadType = "Arping"; + +Arping::Arping(nt ind) : ThreadObject(ind, ThreadType) {} + +Arping::~Arping() = default; + +void Arping::ClearIpsAndInterfaces() { + arpInterfaceIp.clear(); + commands.clear(); +} + +void Arping::AddInterfacesAndIps(std::string interface, std::string ip) { + // create commands to arping + std::ostringstream os; + os << "arping -c 1 -U -I " << interface << " " << ip; + // to read error messages + os << " 2>&1"; + std::string cmd = os.str(); + arpingCommands.push_back(cmd); +} + +void Arping::ThreadExecution() { + // arping + + // wait for 60s + usleep(60 * 1000 * 1000); +} + +LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << " ]"; +} + +void Arping::ExecuteCommands() { + for (auto cmd : commands) { + LOG(logDEBUG) << "Executing Arping Command: " << cmd; + + // execute command + FILE *sysFile = popen(cmd.c_str(), "r"); + if (sysFile == NULL) { + LOG(logERROR) << "Executing cmd [" cmd << " ] Fail:" + << "\n\t Popen fail"; + continue; + } + + // check for errors + char output[MAX_STR_LENGTH] = {0}; + fgets(output, sizeof(output), sysFile); + output[sizeof(output) - 1] = '\0'; + + if (pclose(sysFile)) { + LOG(logERROR) << "Executing cmd[" << cmd + << "]\n\tError Message : " << output; + } else { + LOG(logDEBUG) << output; + } + } +} \ No newline at end of file diff --git a/slsReceiverSoftware/src/Arping.h b/slsReceiverSoftware/src/Arping.h new file mode 100644 index 000000000..b32af3240 --- /dev/null +++ b/slsReceiverSoftware/src/Arping.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: LGPL-3.0-or-other +// Copyright (C) 2021 Contributors to the SLS Detector Package +#pragma once +/** + *@short creates/destroys an ARPing thread to arping the interfaces slsReceiver +is listening to. + */ + +#include "ThreadObject.h" + +class Arping : private virtual slsDetectorDefs, public ThreadObject { + + public: + Arping(int ind); + ~Arping(); + void ClearIpsAndInterfaces(); + void AddInterfacesAndIps(std::string interface, std::string ip); + + private: + /** + * Thread Execution for Arping Class + * Arping interfaces and wait 60 seconds + */ + void ThreadExecution() override; + void ExecuteCommands(); + + static const std::string ThreadType; + std::vector arpingCommands; +}; diff --git a/slsReceiverSoftware/src/Implementation.cpp b/slsReceiverSoftware/src/Implementation.cpp index 60dbd54d9..70730a5fa 100644 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -344,9 +344,9 @@ void Implementation::setArping(const bool i, threadArping->StopRunning(); } else { threadArping->ClearIpsAndInterfaces(); - threadArping->AddIpsAndInterfaces(eth[0], ips[0]); + threadArping->AddInterfacesAndIps(eth[0], ips[0]); if (numUDPInterfaces == 2 && detType != EIGER) { - threadArping->AddIpsAndInterfaces(eth[1], ips[1]); + threadArping->AddInterfacesAndIps(eth[1], ips[1]); } threadArping->StartRunning(); } diff --git a/slsReceiverSoftware/src/ThreadArping.cpp b/slsReceiverSoftware/src/ThreadArping.cpp deleted file mode 100644 index 6af8e86ac..000000000 --- a/slsReceiverSoftware/src/ThreadArping.cpp +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-other -// Copyright (C) 2021 Contributors to the SLS Detector Package - -#include "ThreadArping.h" -#include "sls/container_utils.h" -#include -#include -#include - -ThreadArping::ThreadArping() {} - -ThreadArping::~ThreadArping() { StopRunning(); } - -pid_t ThreadArping::GetThreadId() const { return threadId; } - -bool ThreadArping::IsRunning() const { return runningFlag; } - -void ThreadArping::StartRunning() { - if (!runningFlag) { - if (arpInterfaceIp.size() == 0) { - throw sls::RuntimeError("No Interface added to Arping"); - } - runningFlag = true; - - // create thread - try { - std::thread temp = std::thread(&ThreadArping::RunningThread, this); - threadObject = temp.native_handle(); - temp.detach(); - } catch (...) { - throw sls::RuntimeError("Could not create arping thread"); - } - } -} - -void ThreadArping::StopRunning() { - pthread_cancel(threadObject); - LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << " ]"; - runningFlag = false; -} - -void ThreadArping::ClearIpsAndInterfaces() { arpInterfaceIp.clear(); } - -void ThreadArping::AddIpsAndInterfaces(std::string interface, std::string ip) { - arpInterfaceIp.push_back(std::make_pair(interface, ip)); -} - -void ThreadArping::RunningThread() { - - threadId = syscall(SYS_gettid); - { - std::ostringstream os; - os << "Created [ Arping Thread, Tid: " << threadId << " ] for "; - for (auto ethip : arpInterfaceIp) { - os << "\n\t[ " << ethip.first << ", " << ethip.second << " ]"; - } - LOG(logINFOBLUE) << os.str(); - } - - // create the commands to ping necessary interfaces - std::vector commands; - for (auto ethip : arpInterfaceIp) { - std::ostringstream os; - os << "arping -c 1 -U -I " << ethip.first << " " << ethip.second; - // to read error messages - os << " 2>&1"; - std::string cmd = os.str(); - commands.push_back(cmd); - } - - while (IsRunning()) { - - // arping - for (auto cmd : commands) { - LOG(logDEBUG) << "Executing Arping Command: " << cmd; - - // execute command and check for errors - FILE *sysFile = popen(cmd.c_str(), "r"); - char output[MAX_STR_LENGTH] = {0}; - fgets(output, sizeof(output), sysFile); - output[sizeof(output) - 1] = '\0'; - if (pclose(sysFile)) { - LOG(logERROR) << "Executing cmd[" << cmd - << "]\n\tError Message : " << output; - } else { - LOG(logDEBUG) << output; - } - } - - // wait for 60s - usleep(60 * 1000 * 1000); - } - - LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << " ]"; -} \ No newline at end of file diff --git a/slsReceiverSoftware/src/ThreadArping.h b/slsReceiverSoftware/src/ThreadArping.h deleted file mode 100644 index 9f41041f9..000000000 --- a/slsReceiverSoftware/src/ThreadArping.h +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-other -// Copyright (C) 2021 Contributors to the SLS Detector Package -#pragma once -/** - *@short creates/destroys an ARPing thread to ping the interfaces slsReceiver is -listening to. - */ - -#include "sls/logger.h" -#include "sls/sls_detector_defs.h" - -#include -#include -#include -#include // pair, make_pair - -class ThreadArping : private virtual slsDetectorDefs { - - private: - std::atomic killThread{false}; - std::atomic runningFlag{false}; - - pthread_t threadObject; - std::vector> arpInterfaceIp; - std::vector commands; - pid_t threadId; - - public: - ThreadArping(); - virtual ~ThreadArping(); - pid_t GetThreadId() const; - bool IsRunning() const; - void StartRunning(); - void StopRunning(); - void ClearIpsAndInterfaces(); - void AddIpsAndInterfaces(std::string interface, std::string ip); - - private: - /** - * Thread called: An infinite while loop that runs arping as long as - * RunningMask is satisfied Then it exits the thread on its own if - * killThread is true - */ - void RunningThread(); -}; diff --git a/slsReceiverSoftware/src/ThreadObject.h b/slsReceiverSoftware/src/ThreadObject.h index d57857548..61bdd92ab 100644 --- a/slsReceiverSoftware/src/ThreadObject.h +++ b/slsReceiverSoftware/src/ThreadObject.h @@ -21,8 +21,10 @@ class ThreadObject : private virtual slsDetectorDefs { protected: const int index{0}; - private: + protected: std::atomic killThread{false}; + + private: std::atomic runningFlag{false}; std::thread threadObject; sem_t semaphore;