Commit 1d253d20 authored by Locatelli's avatar Locatelli
Browse files

Offscreen message between main and remote ploty2s

parent 12dbde3a
......@@ -37,7 +37,6 @@ using namespace std;
using namespace cameo;
using namespace manager;
unique_ptr<application::Instance> nomadserver; //! Instance of nomad server
shared_ptr<application::Subscriber> logsubscriber; //! Subcriber on log publisher of the server
shared_ptr<application::Responder> requesterplot; //! Responder for nomad GUI request
......@@ -60,12 +59,13 @@ int main(int32 argc, char* argv[]) {
// Need to create Mpl in main thread
shared_ptr<view::mpl::Mpl> mpl = make_shared<view::mpl::Mpl>();
unique_ptr<application::Instance> nomadserver; //! Instance of nomad server
unique_ptr<application::Instance> ploty2; //! Instance of main ploty2
// Init cameo application
int32 err = EXIT_SUCCESS;
application::This::init(1, &argv[2]);
{
bool remote = false;
string clientype = getNomadProperty("clientType");
if (clientype == "remote") {
DBGMSG("Start ploty in remote mode");
......@@ -88,8 +88,7 @@ int main(int32 argc, char* argv[]) {
// create a subscriber connected on data change subscriber on nomad server data change publisher
logsubscriber = application::Subscriber::create(*nomadserver, "log_publisher");
DBGMSG("Created requester " << *logsubscriber);
DBGMSG("Created subscriber " << *logsubscriber);
if (logsubscriber.get() == 0) {
Error("mainplot", "Connection problem on log_publisher");
err = EXIT_FAILURE;
......@@ -114,16 +113,34 @@ 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;
std::shared_ptr<cameo::application::Subscriber> spysubscriber;
// Create the publisher.
std::shared_ptr<cameo::application::Publisher> spypublisher = application::Publisher::create("spy_publisher");
DBGMSG("Created publisher " << *spypublisher);
if (spypublisher.get() == 0) {
// TODO Error
cout << "spypublisher error" << endl;
return EXIT_FAILURE;
}
if (clientype == "remote") {
// Get nomad server instance
ploty2 = getPloty2Instance(server);
if (ploty2.get() == 0) {
Error("mainplot", "No ploty2 instance");
err = EXIT_FAILURE;
goto exit;
}
DBGMSG("Connected to " << *ploty2);
spysubscriber = application::Subscriber::create(*nomadserver, "spy_publisher");
DBGMSG("Created subscriber " << *spysubscriber);
if (spysubscriber.get() == 0) {
Error("mainplot", "Connection problem on spy_publisher");
err = EXIT_FAILURE;
goto exit;
}
}
std::shared_ptr<cameo::application::Publisher> frontpublisher;
......@@ -147,7 +164,7 @@ int main(int32 argc, char* argv[]) {
manager::ServerRequesterManager::getInstance()->init(requesterdb);
if (clientype == "main") {
OffScreenPlotManager::getInstance()->init(mpl, spypublisher, remote);
OffScreenPlotManager::getInstance()->init(mpl, spypublisher);
// Start thread waiting and managing log event
thread serverLogSubscriberThread(bind(&OffScreenPlotManager::loop, OffScreenPlotManager::getInstance(), logsubscriber));
......@@ -166,6 +183,10 @@ int main(int32 argc, char* argv[]) {
manager::RequestDealerManager::resetInstance();
}
else {
OffScreenPlotManager::getInstance()->init(mpl, spypublisher);
// Start thread waiting and managing log event
thread ploty2SubscriberThread(bind(&OffScreenPlotManager::spyloop, OffScreenPlotManager::getInstance(), spysubscriber));
// Start thread waiting and managing nomad gui request
thread plotRequestThread(bind(&manager::RequestDealerManager::loop, manager::RequestDealerManager::getInstance(), requesterplot, frontpublisher));
......
......@@ -75,7 +75,7 @@ int32 main(int32 argc, char* argv[]) {
// }
// out << "init off manager" << endl;
OffScreenPlotManager::getInstance()->init(mpl, nullptr, false);
OffScreenPlotManager::getInstance()->init(mpl, nullptr);
// out << "run" << endl;
OffScreenPlotManager::getInstance()->run(pbfilename, path);
// out << "finished" << endl;
......
......@@ -73,10 +73,10 @@ void OffScreenPlotManager::resetInstance() {
void OffScreenPlotManager::reset() {
}
void OffScreenPlotManager::init(std::shared_ptr<view::mpl::Mpl> mpl, std::shared_ptr<cameo::application::Publisher> spypublisher, bool remote) {
void OffScreenPlotManager::init(std::shared_ptr<view::mpl::Mpl> mpl,
std::shared_ptr<cameo::application::Publisher> spypublisher) {
m_Mpl = mpl;
m_SpyPublisher = spypublisher;
m_Remote = remote;
}
/*
......@@ -123,6 +123,40 @@ void OffScreenPlotManager::loop(std::shared_ptr<cameo::application::Subscriber>
}
}
/*
* loop
*/
void OffScreenPlotManager::spyloop(std::shared_ptr<cameo::application::Subscriber> subscriber) {
// Enable thread using GIL
view::mpl::MplEnableThreads mplenablethreads;
string data1, data2;
while (subscriber->receiveTwoBinaryParts(data1, data2)) {
if (subscriber->hasEnded() == false) {
ploty::SpyMultiPlotMessage message;
message.ParseFromString(data1);
if (message.type() == ploty::SpyMultiPlotMessage::SpyImage) {
boost::filesystem::path pngfile = "/users/ics/offscreenImages/spy";
pngfile.append(message.name());
pngfile.replace_extension("png");
DBGMSG(pngfile.string());
ofstream file(pngfile.string().c_str(), fstream::binary);
if (file.is_open()) {
file.write(data2.c_str(),data2.size());
}
ploty::SpyMultiPlotMessage spymessage;
spymessage.set_type(ploty::SpyMultiPlotMessage::SpyChange);
spymessage.set_name(message.name());
string buf;
m_SpyPublisher->sendTwoBinaryParts(spymessage.SerializeAsString(), buf);
}
} else {
break;
}
}
}
///*
// * addThreadMapElement
// */
......@@ -151,7 +185,6 @@ void OffScreenPlotManager::loop(std::shared_ptr<cameo::application::Subscriber>
* OffScreenPlotManager
*/
void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
// size_t p1 = pbfilename.find("0.0#");
// size_t p2 = pbfilename.find(".png");
// uint32 num = 0;
......@@ -165,6 +198,7 @@ void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
// }
try {
unique_ptr<view::mpl::MplFigure> figure(new view::mpl::MplFigure(*m_Mpl, false));
// return;
boost::filesystem::path pbfile = path;
pbfile /= pbfilename;
pbfile.replace_extension("pb");
......@@ -179,7 +213,7 @@ void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
buffer::Data data;
data.ParseFromArray(buffer, size);
// DBGMSG("pbfilename = " << path << "/" << pbfilename);
DBGMSG("pbfilename = " << path << "/" << pbfilename);
// DBGMSG("buffer = " << size);
// DBGMSG("numor = " << data.numor());
// DBGMSG("dataxArray = " << data.xdata_size());
......@@ -241,37 +275,41 @@ void OffScreenPlotManager::run(std::string pbfilename, std::string path) {
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);
}
ploty::SpyMultiPlotMessage spymessage;
// In case of Main , send spies and multiplots image to remote ploty2
if (data.type() == buffer::Data::Spy) {
spymessage.set_type(ploty::SpyMultiPlotMessage::SpyImage);
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) {
spymessage.set_type(ploty::SpyMultiPlotMessage::MultiPlotImage);
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);
}
}
// For all send event to gui for updating spies...
if (data.type() == buffer::Data::Spy) {
spymessage.set_type(ploty::SpyMultiPlotMessage::SpyChange);
spymessage.set_name(plotkey.str());
string buf;
m_SpyPublisher->sendTwoBinaryParts(spymessage.SerializeAsString(), buf);
}
}
}
......
......@@ -21,8 +21,8 @@
#include <cameo/cameo.h>
#include <unordered_map>
#include <thread>
#include <mutex>
//#include <thread>
//#include <mutex>
#include "view/mpl/Mpl.h"
#include "view/mpl/MplFigure.h"
......@@ -53,6 +53,12 @@ public:
*/
void loop(std::shared_ptr<cameo::application::Subscriber> subscriber);
/*!
* \brief subscriber loop for nomad server data change publisher
* \param[in] subscriber instance of the cameo subcriber
*/
void spyloop(std::shared_ptr<cameo::application::Subscriber> subscriber);
/*!
* \brief Optimization plot result creation
* \param[in] pbfilename the plot binary file name from server
......@@ -66,7 +72,7 @@ public:
* \param[in] spypublisher Spy publisher
* \param[in] remote Remote ploty2
*/
void init(std::shared_ptr<view::mpl::Mpl> mpl, std::shared_ptr<cameo::application::Publisher> spypublisher, bool remote = false);
void init(std::shared_ptr<view::mpl::Mpl> mpl, std::shared_ptr<cameo::application::Publisher> spypublisher);
// void delThreadMapElement(uint32 id);
......@@ -74,6 +80,10 @@ public:
* \brief do save action
*/
void run(std::string pbfilename, std::string path);
/*!
* \brief do save action
*/
void runSpy(std::string pbfilename, std::string path);
private:
......@@ -97,10 +107,9 @@ private:
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;
std::shared_ptr<cameo::application::Publisher> m_SpyPublisher; //! Spy publisher for gui
// std::mutex m_SpyMutex;
// std::mutex m_MultiplotMutex;
// std::chrono::time_point<std::chrono::steady_clock> start;
// std::chrono::time_point<std::chrono::steady_clock> end;
......
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