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


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:47