datastreamserver.cpp
Go to the documentation of this file.
00001 /*DataStreamServer PlotJuggler  Plugin license(Faircode)
00002 
00003 Copyright(C) 2018 Philippe Gauthier - ISIR - UPMC
00004 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.
00005 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00006 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.
00007 */
00008 #include "datastreamserver.h"
00009 #include <QTextStream>
00010 #include <QFile>
00011 #include <QMessageBox>
00012 #include <QDebug>
00013 #include <thread>
00014 #include <mutex>
00015 #include <thread>
00016 #include <math.h>
00017 #include <QWebSocket>
00018 #include <QInputDialog>
00019 
00020 DataStreamServer::DataStreamServer() :
00021         _running(false),
00022         _server("plotJuggler", QWebSocketServer::NonSecureMode) 
00023 {
00024 
00025         
00026 }
00027 
00028 DataStreamServer::~DataStreamServer()
00029 {
00030         shutdown();
00031 }
00032 
00033 bool DataStreamServer::start(QStringList*)
00034 {               
00035         if (!_running) {
00036                 bool ok;
00037                 _port = QInputDialog::getInt(nullptr, tr(""),
00038                         tr("On whish port should the server listen to:"), 6666, 1111, 65535, 1, &ok);
00039                 if (ok && _server.listen(QHostAddress::Any, _port))
00040                 {
00041                         qDebug() << "Websocket listening on port" << _port;
00042                         connect(&_server, &QWebSocketServer::newConnection,
00043                                 this, &DataStreamServer::onNewConnection);
00044                         _running = true;
00045                 }
00046                 else {
00047                         qDebug() << "Couldn't open websocket on port " << _port;
00048                         _running = false;
00049                 }
00050         }
00051         else {
00052                 qDebug() << "Server already running on port " << _port;
00053                 QMessageBox::information(nullptr,"Info",QString("Server already running on port: %1").arg(_port));              
00054         }
00055         return _running;
00056 }
00057 
00058 void DataStreamServer::shutdown()
00059 {
00060         if (_running) {
00061                 socketDisconnected();
00062                 _server.close();
00063                 _running = false;
00064         }
00065 }
00066 
00067 void DataStreamServer::onNewConnection()
00068 {
00069         qDebug() << "DataStreamServer: onNewConnection";
00070         QWebSocket *pSocket = _server.nextPendingConnection();
00071         connect(pSocket, &QWebSocket::textMessageReceived, this, &DataStreamServer::processMessage);    
00072         connect(pSocket, &QWebSocket::disconnected, this, &DataStreamServer::socketDisconnected);
00073 
00074         _clients << pSocket;
00075 }
00076 
00077 
00078 void DataStreamServer::processMessage(QString message)
00079 {
00080     std::lock_guard<std::mutex> lock( mutex() );
00081 
00082         //qDebug() << "DataStreamServer: processMessage: "<< message;
00083         QStringList lst = message.split(':');
00084         if (lst.size() == 3) {
00085                 QString key = lst.at(0);
00086                 double time = lst.at(1).toDouble();
00087                 double value = lst.at(2).toDouble();
00088 
00089         auto& numeric_plots = dataMap().numeric;
00090 
00091                 const std::string name_str = key.toStdString();
00092         auto plotIt = numeric_plots.find(name_str);
00093                 
00094         if (plotIt == numeric_plots.end())
00095         {
00096             dataMap().addNumeric(name_str);
00097                 }
00098         else{
00099             plotIt->second.pushBack( {time, value} );
00100         }
00101         }       
00102 }
00103 
00104 void DataStreamServer::socketDisconnected()
00105 {
00106         qDebug() << "DataStreamServer: socketDisconnected";
00107         QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
00108         if (pClient){
00109                 disconnect(pClient, &QWebSocket::textMessageReceived, this, &DataStreamServer::processMessage);
00110                 disconnect(pClient, &QWebSocket::disconnected, this, &DataStreamServer::socketDisconnected);
00111 
00112                 _clients.removeAll(pClient);
00113                 pClient->deleteLater();
00114         }
00115 }
00116 
00117 


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04