statepublisher_zmq.cpp
Go to the documentation of this file.
1 #include "statepublisher_zmq.h"
2 #include <QTextStream>
3 #include <QFile>
4 #include <QMessageBox>
5 #include <zmq.hpp>
6 #include <thread>
7 
9  : _prev_dataplot(0)
10  , _thread(std::bind(&StatePublisherZMQ::run_thread, this))
11  , _prev_time(0)
12 {
13 }
14 
16 {
17 }
18 
20 {
21  zmq::context_t context(1);
22  zmq::socket_t socket(context, ZMQ_REP);
23  socket.bind("tcp://*:6665");
24 
25  while (true)
26  {
27  zmq::message_t request;
28 
29  // Wait for next request from client
30  socket.recv(&request);
31  const char* request_data = (const char*)request.data();
32 
33  if (strncmp(request_data, "[get_data_names]", 16) == 0)
34  {
35  QString string_reply;
36 
37  _mutex.lock();
38 
39  std::map<QString, double>::iterator it;
40  for (it = _current_data.begin(); it != _current_data.end(); it++)
41  {
42  string_reply.append(it->first + QString(" "));
43  }
44  _mutex.unlock();
45 
46  zmq::message_t reply(string_reply.size());
47  socket.send(reply);
48  }
49  else if (strncmp(request_data, "[get_data]", 10) == 0)
50  {
51  bool abort = false;
52  QString string_request = QString::fromUtf8(&request_data[10], request.size() - 10);
53  QStringList names = string_request.split(';');
54 
55  _mutex.lock();
56  QString string_reply;
57 
58  for (int i = 0; i < names.count(); i++)
59  {
60  std::map<QString, double>::iterator it = _current_data.find(names.at(i));
61  if (it == _current_data.end())
62  {
63  abort = true;
64  break;
65  }
66  else
67  {
68  double value = it->second;
69  string_reply.append(QString::number(value) + QString(" "));
70  }
71  }
72  _mutex.unlock();
73 
74  if (abort)
75  {
76  zmq::message_t reply(5);
77  memcpy(reply.data(), "Error", 5);
78  socket.send(reply);
79  }
80  else
81  {
82  zmq::message_t reply(string_reply.size());
83  socket.send(reply);
84  }
85  }
86  else
87  {
88  zmq::message_t reply(5);
89  memcpy(reply.data(), "Error", 5);
90  socket.send(reply);
91  }
92  }
93 }
94 
95 void StatePublisherZMQ::updateState(PlotDataMap* datamap, double current_time)
96 {
97  if (datamap == 0)
98  {
99  _prev_dataplot = datamap;
100  _prev_time = current_time;
101  _mutex.lock();
102  _current_data.clear();
103  _mutex.unlock();
104  return;
105  }
106 
107  PlotDataMap::iterator it;
108 
109  _mutex.lock();
110  if (datamap != _prev_dataplot || current_time != _prev_time)
111  {
112  for (it = datamap->begin(); it != datamap->end(); it++)
113  {
114  const QString& name = it->first;
115  PlotDataPtr Timeseries = it->second;
116  _current_data[name] = plotdata->getY(current_time);
117  }
118  }
119  _mutex.unlock();
120 
121  _prev_dataplot = datamap;
122  _prev_time = current_time;
123 }
StatePublisherZMQ::_prev_time
double _prev_time
Definition: statepublisher_zmq.h:33
StatePublisherZMQ::updateState
virtual void updateState(PlotDataMap *datamap, double current_time)
Definition: statepublisher_zmq.cpp:95
zmq::message_t
Definition: zmq.hpp:389
zmq::socket_t
Definition: zmq.hpp:2262
zmq::message_t::data
void * data() ZMQ_NOTHROW
Definition: zmq.hpp:572
StatePublisherZMQ::run_thread
void run_thread()
Definition: statepublisher_zmq.cpp:19
zmq::detail::socket_base::send
size_t send(const void *buf_, size_t len_, int flags_=0)
Definition: zmq.hpp:1959
statepublisher_zmq.h
zmq.hpp
zmq::context_t
Definition: zmq.hpp:804
StatePublisherZMQ::_mutex
std::mutex _mutex
Definition: statepublisher_zmq.h:30
StatePublisherZMQ::~StatePublisherZMQ
virtual ~StatePublisherZMQ()
Definition: statepublisher_zmq.cpp:15
StatePublisherZMQ::StatePublisherZMQ
StatePublisherZMQ()
Definition: statepublisher_zmq.cpp:8
std
StatePublisherZMQ::_prev_dataplot
PlotDataMap * _prev_dataplot
Definition: statepublisher_zmq.h:25
zmq::detail::socket_base::bind
void bind(std::string const &addr)
Definition: zmq.hpp:1905
StatePublisherZMQ::_current_data
std::map< QString, double > _current_data
Definition: statepublisher_zmq.h:32
StatePublisherZMQ
Definition: statepublisher_zmq.h:11
zmq::message_t::size
size_t size() const ZMQ_NOTHROW
Definition: zmq.hpp:582


plotjuggler
Author(s): Davide Faconti
autogenerated on Tue Nov 26 2024 03:24:11