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 }
15 
17 {
18 
19 }
20 
22 {
23  zmq::context_t context(1);
24  zmq::socket_t socket (context, ZMQ_REP);
25  socket.bind ("tcp://*:6665");
26 
27  while (true)
28  {
29  zmq::message_t request;
30 
31  // Wait for next request from client
32  socket.recv (&request);
33  const char* request_data = (const char*)request.data();
34 
35  if( strncmp( request_data, "[get_data_names]", 16 ) == 0 )
36  {
37  QString string_reply;
38 
39  _mutex.lock();
40 
41  std::map<QString, double>::iterator it;
42  for( it = _current_data.begin(); it != _current_data.end(); it++ )
43  {
44  string_reply.append( it->first + QString(" "));
45  }
46  _mutex.unlock();
47 
48  zmq::message_t reply ( string_reply.size() );
49  socket.send (reply);
50  }
51  else if( strncmp( request_data, "[get_data]", 10 ) == 0 )
52  {
53  bool abort = false;
54  QString string_request = QString::fromUtf8( &request_data [10], request.size() - 10);
55  QStringList names = string_request.split(';');
56 
57  _mutex.lock();
58  QString string_reply;
59 
60  for(int i = 0; i< names.count(); i++ )
61  {
62  std::map<QString, double>::iterator it = _current_data.find( names.at(i) );
63  if( it == _current_data.end())
64  {
65  abort = true;
66  break;
67  }
68  else{
69  double value = it->second;
70  string_reply.append( QString::number(value) + QString(" "));
71  }
72  }
73  _mutex.unlock();
74 
75  if( abort )
76  {
77  zmq::message_t reply (5);
78  memcpy (reply.data (), "Error", 5);
79  socket.send (reply);
80  }
81  else{
82  zmq::message_t reply ( string_reply.size() );
83  socket.send (reply);
84  }
85  }
86  else{
87  zmq::message_t reply (5);
88  memcpy (reply.data (), "Error", 5);
89  socket.send (reply);
90  }
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 plotdata = 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 }
virtual const char * name() const =0
virtual void updateState(PlotDataMap *datamap, double current_time)
T value
std::map< QString, double > _current_data
PlotDataMap * _prev_dataplot
int i


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18