2020-10-12 15:26:03 +02:00
|
|
|
/** Dump fields and values in a socket to plot them with villasDump.py.
|
|
|
|
*
|
|
|
|
* @author Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-02-16 14:41:55 +01:00
|
|
|
#include <sstream>
|
2021-02-16 15:49:10 +01:00
|
|
|
#include <cstring>
|
2021-02-16 14:41:55 +01:00
|
|
|
|
|
|
|
#include <villas/dumper.hpp>
|
|
|
|
|
2020-10-12 15:26:03 +02:00
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
2021-02-16 13:25:37 +01:00
|
|
|
Dumper::Dumper(const std::string &socketNameIn) :
|
|
|
|
socketName(socketNameIn),
|
2021-02-10 13:06:19 +01:00
|
|
|
supressRepeatedWarning(true),
|
2021-02-16 14:15:14 +01:00
|
|
|
warningCounter(0),
|
|
|
|
logger(logging.get("dumper"))
|
2020-10-12 15:26:03 +02:00
|
|
|
{
|
2021-02-16 13:25:37 +01:00
|
|
|
openSocket();
|
2020-10-12 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Dumper::~Dumper() {
|
2021-02-10 13:06:19 +01:00
|
|
|
closeSocket();
|
2020-10-12 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 13:25:37 +01:00
|
|
|
int Dumper::openSocket()
|
2021-02-10 13:06:19 +01:00
|
|
|
{
|
2021-02-16 13:25:37 +01:00
|
|
|
socketFd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
2021-02-10 13:06:19 +01:00
|
|
|
if (socketFd < 0) {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->info("Error creating socket {}", socketName);
|
2021-02-10 13:06:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-10-12 15:26:03 +02:00
|
|
|
|
2021-02-16 11:43:53 +01:00
|
|
|
sockaddr_un socketaddrUn;
|
|
|
|
socketaddrUn.sun_family = AF_UNIX;
|
|
|
|
strcpy(socketaddrUn.sun_path, socketName.c_str());
|
2020-10-12 15:26:03 +02:00
|
|
|
|
2021-02-16 14:41:55 +01:00
|
|
|
int ret = connect(socketFd, (struct sockaddr *) &socketaddrUn, sizeof(socketaddrUn));
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
2020-10-12 15:26:03 +02:00
|
|
|
|
2021-02-16 14:41:55 +01:00
|
|
|
return 0;
|
2020-10-12 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:41:55 +01:00
|
|
|
int Dumper::closeSocket()
|
2021-02-10 13:06:19 +01:00
|
|
|
{
|
2021-02-16 14:41:55 +01:00
|
|
|
int ret = close(socketFd);
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2020-10-12 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 13:25:37 +01:00
|
|
|
void Dumper::writeData(unsigned len, double *yData, double *xData)
|
2021-02-10 13:06:19 +01:00
|
|
|
{
|
2021-02-16 13:25:37 +01:00
|
|
|
for (unsigned i = 0; i<len; i++) {
|
2021-02-16 14:41:55 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << yData[i];
|
|
|
|
|
2021-02-16 09:36:14 +01:00
|
|
|
if(xData != nullptr)
|
2021-02-16 14:41:55 +01:00
|
|
|
ss << ";" << xData[i];
|
|
|
|
|
|
|
|
ss << std::endl;
|
2020-10-12 15:26:03 +02:00
|
|
|
|
2021-02-16 14:41:55 +01:00
|
|
|
auto str = ss.str();
|
|
|
|
auto bytesWritten = write(socketFd, str.c_str(), str.length());
|
|
|
|
if ((long unsigned int) bytesWritten != str.length() && (!supressRepeatedWarning || warningCounter <1 )) {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Could not send all content to socket {}", socketName);
|
2021-02-10 13:06:19 +01:00
|
|
|
warningCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|