mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
dumper: add function for naming the socket
Signed-off-by: Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
This commit is contained in:
parent
536b39e96d
commit
70d00ba774
2 changed files with 34 additions and 16 deletions
|
@ -18,19 +18,23 @@ namespace node {
|
|||
class Dumper {
|
||||
|
||||
protected:
|
||||
bool active;
|
||||
int socketFd;
|
||||
std::string socketName;
|
||||
std::string socketPath;
|
||||
bool supressRepeatedWarning;
|
||||
uint64_t warningCounter;
|
||||
Logger logger;
|
||||
|
||||
public:
|
||||
Dumper(const std::string &socketNameIn);
|
||||
Dumper();
|
||||
~Dumper();
|
||||
|
||||
int openSocket();
|
||||
int closeSocket();
|
||||
bool isActive();
|
||||
int setActive();
|
||||
|
||||
int setPath(std::string socketPathIn);
|
||||
void writeDataCSV(unsigned len, double *yData, double *xData = nullptr);
|
||||
void writeDataBinary(unsigned len, double *yData, double *xData = nullptr);
|
||||
};
|
||||
|
|
|
@ -18,30 +18,38 @@
|
|||
using namespace villas;
|
||||
using namespace villas::node;
|
||||
|
||||
Dumper::Dumper(const std::string &socketNameIn) :
|
||||
socketName(socketNameIn),
|
||||
Dumper::Dumper() :
|
||||
active(false),
|
||||
socketPath(""),
|
||||
supressRepeatedWarning(true),
|
||||
warningCounter(0),
|
||||
logger(logging.get("dumper"))
|
||||
{
|
||||
openSocket();
|
||||
}
|
||||
{}
|
||||
|
||||
Dumper::~Dumper() {
|
||||
closeSocket();
|
||||
}
|
||||
|
||||
bool Dumper::isActive(){
|
||||
return active;
|
||||
}
|
||||
|
||||
int Dumper::setActive() {
|
||||
active = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Dumper::openSocket()
|
||||
{
|
||||
socketFd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
||||
if (socketFd < 0) {
|
||||
logger->info("Error creating socket {}", socketName);
|
||||
logger->info("Error creating socket {}", socketPath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sockaddr_un socketaddrUn;
|
||||
socketaddrUn.sun_family = AF_UNIX;
|
||||
strcpy(socketaddrUn.sun_path, socketName.c_str());
|
||||
strcpy(socketaddrUn.sun_path, socketPath.c_str());
|
||||
|
||||
int ret = connect(socketFd, (struct sockaddr *) &socketaddrUn, sizeof(socketaddrUn));
|
||||
if (!ret)
|
||||
|
@ -59,6 +67,11 @@ int Dumper::closeSocket()
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Dumper::setPath(std::string socketPathIn) {
|
||||
socketPath = socketPathIn;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Dumper::writeDataBinary(unsigned len, double *yData, double *xData){
|
||||
|
||||
if (warningCounter > 10)
|
||||
|
@ -67,22 +80,23 @@ void Dumper::writeDataBinary(unsigned len, double *yData, double *xData){
|
|||
if (yData == nullptr)
|
||||
return;
|
||||
|
||||
unsigned dataLen = len * sizeof(yData[0]);
|
||||
unsigned dataLen = len * sizeof(double);
|
||||
ssize_t bytesWritten = write(socketFd, &dataLen, sizeof(dataLen));
|
||||
if ((size_t) bytesWritten != sizeof(len)) {
|
||||
logger->warn("Could not send all content (Len) to socket {}", socketName);
|
||||
logger->warn("Could not send all content (Len) to socket {}", socketPath);
|
||||
warningCounter++;
|
||||
}
|
||||
|
||||
bytesWritten = write(socketFd, "d000", 4);
|
||||
if (bytesWritten != 4) {
|
||||
logger->warn("Could not send all content (Type) to socket {}", socketName);
|
||||
static const char buf[] = "d000";
|
||||
bytesWritten = write(socketFd, buf, sizeof(buf));
|
||||
if (bytesWritten != sizeof(buf)) {
|
||||
logger->warn("Could not send all content (Type) to socket {}", socketPath);
|
||||
warningCounter++;
|
||||
}
|
||||
|
||||
bytesWritten = write(socketFd, yData, dataLen );
|
||||
if (bytesWritten != (ssize_t) dataLen && (!supressRepeatedWarning || warningCounter <1 )) {
|
||||
logger->warn("Could not send all content (Data) to socket {}", socketName);
|
||||
logger->warn("Could not send all content (Data) to socket {}", socketPath);
|
||||
warningCounter++;
|
||||
}
|
||||
}
|
||||
|
@ -102,7 +116,7 @@ void Dumper::writeDataCSV(unsigned len, double *yData, double *xData)
|
|||
auto str = ss.str();
|
||||
auto bytesWritten = write(socketFd, str.c_str(), str.length());
|
||||
if ((size_t) bytesWritten != str.length() && (!supressRepeatedWarning || warningCounter < 1)) {
|
||||
logger->warn("Could not send all content to socket {}", socketName);
|
||||
logger->warn("Could not send all content to socket {}", socketPath);
|
||||
warningCounter++;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue