Commit cfda126f authored by Locatelli's avatar Locatelli
Browse files

Publish spies picture for remote client

parent c3a10d76
......@@ -114,8 +114,20 @@ int main(int32 argc, char* argv[]) {
return EXIT_FAILURE;
}
std::shared_ptr<cameo::application::Publisher> spypublisher;
if (remote == false) {
// Create the publisher.
spypublisher = application::Publisher::create("spy_publisher");
DBGMSG("Created publisher " << *spypublisher);
if (spypublisher.get() == 0) {
// TODO Error
cout << "spypublisher error" << endl;
return EXIT_FAILURE;
}
}
// Manage application signal
signal(SIGABRT, crash_handler);
// signal(SIGABRT, crash_handler);
signal(SIGHUP, crash_handler);
// signal(SIGSEGV, crash_handler);
signal(SIGPIPE, crash_handler);
......@@ -123,7 +135,7 @@ int main(int32 argc, char* argv[]) {
// Init the nomad server requester manager
manager::ServerRequesterManager::getInstance()->init(requesterdb);
OffScreenPlotManager::getInstance()->init(mpl, remote);
OffScreenPlotManager::getInstance()->init(mpl, spypublisher, remote);
// Start thread waiting and managing log event
thread serverLogSubscriberThread(bind(&OffScreenPlotManager::loop, OffScreenPlotManager::getInstance(), logsubscriber));
......
......@@ -23,23 +23,15 @@
#include <cstdlib>
#include <memory>
#include <signal.h>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "Common.h"
#include "common/protobuf/generated/NotificationMessages.pb.h"
#include "protobuf/generated/AcquisitionSerializer.pb.h"
#include "view/mpl/Mpl.h"
#include "view/mpl/MplFigure.h"
#include "Trace.h"
#include "Error.h"
#include "PlotId.h"
#include "plot/offscreen/OffScreenEmptyPlot.h"
#include "plot/offscreen/OffScreenPlot1D.h"
#include "plot/offscreen/OffScreenPlot2D.h"
#include "json/LogSender.h"
#include "manager/OffScreenPlotManager.h"
using namespace std;
using namespace manager;
using namespace cameo;
/*!
......@@ -55,8 +47,11 @@ void crash_handler(int32 sig) {
*/
int32 main(int32 argc, char* argv[]) {
for(int32 i = 0; i < argc; ++i) {
cout << argv[i] << endl;
}
if (argc < 3) {
Error("mainoffscreenplot", "Missing arguments, need 2 : got ", argc);
cerr << "mainoffscreenplot Missing arguments, need 2 : got " << argc;
return EXIT_FAILURE;
}
......@@ -65,92 +60,19 @@ int32 main(int32 argc, char* argv[]) {
// Create and init the Matplolib module
unique_ptr<view::mpl::Mpl> mpl(new view::mpl::Mpl());
try {
unique_ptr<view::mpl::MplFigure> figure(new view::mpl::MplFigure(*mpl.get(), false));
boost::filesystem::path pbfile = path;
pbfile /= pbfilename;
pbfile.replace_extension("pb");
uint32 size = (uint32) boost::filesystem::file_size(pbfile);
ifstream inputMessage(pbfile.string().c_str(), fstream::binary);
if (inputMessage.is_open() == false) {
Error("OffScreenPlotManager", "savePlot",
"Failed to open temporary file which contains ploty::PlotPropertyDataMessage", pbfilename);
} else {
char buffer[size];
inputMessage.read(buffer, size);
buffer::Data data;
data.ParseFromArray(buffer, size);
// DBGMSG("buffer = " << size);
// DBGMSG("numor = " << data.numor());
// DBGMSG("dataxArray = " << data.xdata_size());
// DBGMSG("datayArray = " << data.ydata_size());
// DBGMSG("datazArray = " << data.zdata_size());
inputMessage.close();
ostringstream plotkey;
plotkey << pbfilename;
uint32 dpi = 100;
float64 pad = 1;
if (data.type() == buffer::Data::Spy) {
dpi = 30;
pad = 0;
} else if (data.type() == buffer::Data::Multiplot) {
dpi = 30;
}
if ((data.xdata_size() > 0) && (data.ydata_size() > 0) && (data.zdata_size() > 0)) {
// 2D
for (int32 i = 0; i < data.xdata_size(); ++i) {
try {
plot::offscreen::OffScreenPlot2D plot(PlotId::id, *figure, plotkey.str(), *mpl.get(), data);
plot.display();
plot.save(pbfile.parent_path().parent_path().string(), dpi, pad);
} catch (Error& e) {
Error("OffScreenPlotManager", "savePlot", "Failed create 2d plot", plotkey.str());
}
}
} else if ((data.xdata_size() > 0) && (data.ydata_size() > 0)) {
// 1D
for (int32 i = 0; i < data.xdata_size(); ++i) {
try {
plot::offscreen::OffScreenPlot1D plot(PlotId::id, *figure, plotkey.str(), *mpl.get(), data);
plot.display();
plot.save(pbfile.parent_path().parent_path().string(), dpi, pad);
} catch (Error& e) {
Error("OffScreenPlotManager", "savePlot", "Failed create 1d plot", plotkey.str());
}
}
} else {
try {
plot::offscreen::OffScreenEmptyPlot plot(PlotId::id ,*figure, plotkey.str(), *mpl.get(), data);
plot.display();
plot.save(pbfile.parent_path().parent_path().string(), dpi, pad);
} catch (Error& e) {
Error("OffScreenPlotManager", "savePlot", "Failed create empty plot", plotkey.str());
}
}
if (data.type() == buffer::Data::Log) {
// delete file
boost::filesystem::remove(pbfile);
// Send image to log database system
json::LogSender sender(&data);
sender.postMessage();
boost::filesystem::path pngfile = path;
pngfile = pngfile.parent_path().parent_path();
pngfile /= pbfilename;
// delete image file
boost::filesystem::remove(pngfile);
}
}
} catch (boost::filesystem::filesystem_error& e) {
Error("OffScreenPlotManager", "savePlot",
"Failed to open temporary file which contains ploty::PlotPropertyDataMessage", pbfilename);
shared_ptr<view::mpl::Mpl> mpl = make_shared<view::mpl::Mpl>();
// Create the publisher.
std::shared_ptr<cameo::application::Publisher> spypublisher = application::Publisher::create("spy_publisher");
cout << "Created publisher " << *spypublisher;
if (spypublisher.get() == 0) {
// TODO Error
cout << "spypublisher error" << endl;
return EXIT_FAILURE;
}
OffScreenPlotManager::getInstance()->init(mpl, spypublisher, false);
OffScreenPlotManager::getInstance()->run(pbfilename, path);
return EXIT_SUCCESS;
}
......@@ -30,6 +30,7 @@
#include "plot/offscreen/OffScreenPlot2D.h"
#include "common/protobuf/generated/NotificationMessages.pb.h"
#include "protobuf/generated/AcquisitionSerializer.pb.h"
#include "protobuf/generated/DataPlotMessages.pb.h"
#include "json/LogSender.h"
#include "Trace.h"
#include "view/mpl/MplLock.h"
......@@ -71,8 +72,10 @@ void OffScreenPlotManager::resetInstance() {
void OffScreenPlotManager::reset() {
}
void OffScreenPlotManager::init(std::shared_ptr<view::mpl::Mpl> mpl, bool remote) {
void OffScreenPlotManager::init(std::shared_ptr<view::mpl::Mpl> mpl, std::shared_ptr<cameo::application::Publisher> spypublisher, bool remote) {
m_Mpl = mpl;
m_SpyPublisher = spypublisher;
m_Remote = remote;
}
/*
......@@ -102,9 +105,13 @@ void OffScreenPlotManager::loop(std::shared_ptr<cameo::application::Subscriber>
notification::ImageDataReady messageImageReady;
try {
messageImageReady.ParseFromString(data2);
// run(messageImageReady.imagename(), messageImageReady.imagepath());
ThreadPoolManager::getInstance()->addJob(
bind(&OffScreenPlotManager::run, m_Instance, messageImageReady.imagename(),
messageImageReady.imagepath()));
// ostringstream com;
// com << "./offscreenplot " << messageImageReady.imagename() << " " << messageImageReady.imagepath();
// system(com.str().c_str());
} catch (std::bad_alloc &e) {
Error("OffScreenPlotManager", "loop", "Bad parsing of ImageDataReady");
}
......@@ -143,9 +150,6 @@ void OffScreenPlotManager::loop(std::shared_ptr<cameo::application::Subscriber>
* OffScreenPlotManager
*/
void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
// ostringstream com;
// com << "./offscreenplot " << pbfilename << " " << path;
// system(com.str().c_str());
try {
unique_ptr<view::mpl::MplFigure> figure(new view::mpl::MplFigure(*m_Mpl, false));
......@@ -214,6 +218,10 @@ void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
}
}
boost::filesystem::path pngfile = path;
pngfile = pngfile.parent_path().parent_path();
pngfile /= pbfilename;
if (data.type() == buffer::Data::Log) {
// delete file
boost::filesystem::remove(pbfile);
......@@ -221,12 +229,44 @@ void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
// Send image to log database system
json::LogSender sender(&data);
sender.postMessage();
boost::filesystem::path pngfile = path;
pngfile = pngfile.parent_path().parent_path();
pngfile /= pbfilename;
// delete image file
boost::filesystem::remove(pngfile);
// boost::filesystem::remove(pngfile);
}
else {
if (m_Remote == false) {
ploty::SpyMultiPlotMessage spymessage;
if (data.type() == buffer::Data::Spy) {
unique_lock<mutex> lock(m_SpyMutex);
spymessage.set_type(ploty::SpyMultiPlotMessage::Spy);
spymessage.set_name(plotkey.str());
ifstream file(pngfile.string().c_str(), fstream::binary);
if (file.is_open()) {
file.seekg (0, file.end);
size_t length = file.tellg();
file.seekg (0, file.beg);
string buf;
buf.resize(length);
file.read(&buf[0],length);
m_SpyPublisher->sendTwoBinaryParts(spymessage.SerializeAsString(), buf);
}
} else if (data.type() == buffer::Data::Multiplot) {
unique_lock<mutex> lock(m_MultiplotMutex);
spymessage.set_type(ploty::SpyMultiPlotMessage::MultiPlot);
spymessage.set_name(plotkey.str());
ifstream file(pngfile.string().c_str(), fstream::binary);
if (file.is_open()) {
file.seekg (0, file.end);
size_t length = file.tellg();
file.seekg (0, file.beg);
string buf;
buf.resize(length);
file.read(&buf[0],length);
m_SpyPublisher->sendTwoBinaryParts(spymessage.SerializeAsString(), buf);
}
}
}
}
DBGMSG("OK pbfilename = " << path << "/" << pbfilename);
}
} catch (boost::filesystem::filesystem_error& e) {
Error("OffScreenPlotManager", "savePlot",
......
......@@ -63,11 +63,18 @@ public:
/*!
* \brief init the OffScreenPlotManager
* \param[in] mpl The mpl object
* \param[in] spypublisher Spy publisher
* \param[in] remote Remote ploty2
*/
void init(std::shared_ptr<view::mpl::Mpl> mpl, bool remote = false);
void init(std::shared_ptr<view::mpl::Mpl> mpl, std::shared_ptr<cameo::application::Publisher> spypublisher, bool remote = false);
// void delThreadMapElement(uint32 id);
/*!
* \brief do save action
*/
void run(std::string pbfilename, std::string path);
private:
/*!
......@@ -87,15 +94,13 @@ private:
// void addThreadMapElement(view::mpl::Mpl* mpl, uint32 id, std::string pbfilename, std::string path);
/*!
* \brief do save action
*/
void run(std::string pbfilename, std::string path);
static OffScreenPlotManager* m_Instance; //! Pointer of singleton instance
std::shared_ptr<view::mpl::Mpl> m_Mpl; //! Mpl object (main class which managed the matplotib api)
std::shared_ptr<cameo::application::Publisher> m_SpyPublisher; //! Spu publisher for remote ploty2
bool m_Remote; //! Flag tells ploty2 is running on a remote machine
std::mutex m_SpyMutex;
std::mutex m_MultiplotMutex;
// typedef std::unordered_map<uint32, std::thread> ThreadMap;
// ThreadMap m_ThreadMap;
......
......@@ -192,12 +192,14 @@ void OffScreenPlot1D::plot(uint32 numplot, const std::vector<float64> &x, const
* optimizationPlot
*/
void OffScreenPlot1D::optimizationPlot(uint32 numplot) throw (Error) {
if (numplot < m_Data.optimizationfitdata_size()) {
if (m_Data.optimizationfitdata(numplot).datadouble_size() == m_PlotXArrays[numplot].size()) {
vector<float64> yvalues;
copy(m_Data.optimizationfitdata(numplot).datadouble().begin(),
m_Data.optimizationfitdata(numplot).datadouble().end(), back_inserter(yvalues));
m_MplPlot1D.optimizationPlot(numplot, m_PlotXArrays[numplot], yvalues, "Fit", "#FFA800", 0.5);
if (m_Data.type() != buffer::Data::Spy) {
if (numplot < m_Data.optimizationfitdata_size()) {
if (m_Data.optimizationfitdata(numplot).datadouble_size() == m_PlotXArrays[numplot].size()) {
vector<float64> yvalues;
copy(m_Data.optimizationfitdata(numplot).datadouble().begin(),
m_Data.optimizationfitdata(numplot).datadouble().end(), back_inserter(yvalues));
m_MplPlot1D.optimizationPlot(numplot, m_PlotXArrays[numplot], yvalues, "Fit", "#FFA800", 0.5);
}
}
}
}
......@@ -206,10 +208,12 @@ void OffScreenPlot1D::optimizationPlot(uint32 numplot) throw (Error) {
* optimizationResult
*/
void OffScreenPlot1D::optimizationResult(uint32 numplot) throw (Error) {
if (numplot < m_Data.optimizationresultfounds_size()) {
if (m_Data.optimizationresultfounds(numplot) == true) {
float64 xvalue = m_Data.optimizationresultvalues(numplot);
m_MplPlot1D.optimizationResult(numplot, xvalue, "dashed", "#FFA800", 1);
if (m_Data.type() != buffer::Data::Spy) {
if (numplot < m_Data.optimizationresultfounds_size()) {
if (m_Data.optimizationresultfounds(numplot) == true) {
float64 xvalue = m_Data.optimizationresultvalues(numplot);
m_MplPlot1D.optimizationResult(numplot, xvalue, "dashed", "#FFA800", 1);
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment