datastreamserver.cpp
Go to the documentation of this file.
1 /*DataStreamServer PlotJuggler Plugin license(Faircode)
2 
3 Copyright(C) 2018 Philippe Gauthier - ISIR - UPMC
4 Permission is hereby granted to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies("Use") of the Software, and to permit persons to whom the Software is furnished to do so.
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 */
8 #include "datastreamserver.h"
9 #include <QTextStream>
10 #include <QFile>
11 #include <QMessageBox>
12 #include <QDebug>
13 #include <thread>
14 #include <mutex>
15 #include <thread>
16 #include <math.h>
17 #include <QWebSocket>
18 #include <QInputDialog>
19 
21  _running(false),
22  _server("plotJuggler", QWebSocketServer::NonSecureMode)
23 {
24 
25 
26 }
27 
29 {
30  shutdown();
31 }
32 
33 bool DataStreamServer::start(QStringList*)
34 {
35  if (!_running) {
36  bool ok;
37  _port = QInputDialog::getInt(nullptr, tr(""),
38  tr("On whish port should the server listen to:"), 6666, 1111, 65535, 1, &ok);
39  if (ok && _server.listen(QHostAddress::Any, _port))
40  {
41  qDebug() << "Websocket listening on port" << _port;
42  connect(&_server, &QWebSocketServer::newConnection,
44  _running = true;
45  }
46  else {
47  qDebug() << "Couldn't open websocket on port " << _port;
48  _running = false;
49  }
50  }
51  else {
52  qDebug() << "Server already running on port " << _port;
53  QMessageBox::information(nullptr,"Info",QString("Server already running on port: %1").arg(_port));
54  }
55  return _running;
56 }
57 
59 {
60  if (_running) {
62  _server.close();
63  _running = false;
64  }
65 }
66 
68 {
69  qDebug() << "DataStreamServer: onNewConnection";
70  QWebSocket *pSocket = _server.nextPendingConnection();
71  connect(pSocket, &QWebSocket::textMessageReceived, this, &DataStreamServer::processMessage);
72  connect(pSocket, &QWebSocket::disconnected, this, &DataStreamServer::socketDisconnected);
73 
74  _clients << pSocket;
75 }
76 
77 
78 void DataStreamServer::processMessage(QString message)
79 {
80  std::lock_guard<std::mutex> lock( mutex() );
81 
82  //qDebug() << "DataStreamServer: processMessage: "<< message;
83  QStringList lst = message.split(':');
84  if (lst.size() == 3) {
85  QString key = lst.at(0);
86  double time = lst.at(1).toDouble();
87  double value = lst.at(2).toDouble();
88 
89  auto& numeric_plots = dataMap().numeric;
90 
91  const std::string name_str = key.toStdString();
92  auto plotIt = numeric_plots.find(name_str);
93 
94  if (plotIt == numeric_plots.end())
95  {
96  dataMap().addNumeric(name_str);
97  }
98  else{
99  plotIt->second.pushBack( {time, value} );
100  }
101  }
102 }
103 
105 {
106  qDebug() << "DataStreamServer: socketDisconnected";
107  QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
108  if (pClient){
109  disconnect(pClient, &QWebSocket::textMessageReceived, this, &DataStreamServer::processMessage);
110  disconnect(pClient, &QWebSocket::disconnected, this, &DataStreamServer::socketDisconnected);
111 
112  _clients.removeAll(pClient);
113  pClient->deleteLater();
114  }
115 }
116 
117 
QWebSocketServer _server
std::unordered_map< std::string, PlotData > numeric
Definition: plotdata.h:144
PlotDataMapRef & dataMap()
virtual void shutdown() override
std::mutex & mutex()
void processMessage(QString message)
T value
virtual ~DataStreamServer()
QList< QWebSocket * > _clients
void * arg
std::unordered_map< std::string, PlotData >::iterator addNumeric(const std::string &name)
Definition: plotdata.h:147
virtual bool start(QStringList *) override


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