using ThreadObject, waiting for 1 sec

This commit is contained in:
maliakal_d 2022-02-03 10:59:52 +01:00
parent cace18e535
commit 7af5d991d9
9 changed files with 103 additions and 149 deletions

View File

@ -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

View File

@ -886,7 +886,7 @@ class Detector {
Result<bool> 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 = {});

View File

@ -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<int>,
"[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<int>,
"[0, 1]\n\tStarts a thread in slsReceiver to arping "
"the interface it is "
"listening to every minute. Useful in 10G mode.");
/* File */

View File

@ -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 <iostream>
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;
}
}
}

View File

@ -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<std::string> arpingCommands;
};

View File

@ -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();
}

View File

@ -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 <iostream>
#include <sys/syscall.h>
#include <unistd.h>
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<std::string> 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 << " ]";
}

View File

@ -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 <atomic>
#include <string>
#include <thread>
#include <utility> // pair, make_pair
class ThreadArping : private virtual slsDetectorDefs {
private:
std::atomic<bool> killThread{false};
std::atomic<bool> runningFlag{false};
pthread_t threadObject;
std::vector<std::pair<std::string, std::string>> arpInterfaceIp;
std::vector<std::string> 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();
};

View File

@ -21,8 +21,10 @@ class ThreadObject : private virtual slsDetectorDefs {
protected:
const int index{0};
private:
protected:
std::atomic<bool> killThread{false};
private:
std::atomic<bool> runningFlag{false};
std::thread threadObject;
sem_t semaphore;