From bab24af091240c6f0b3ab582389e60ccfa66a5e2 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 6 Apr 2014 14:31:42 +0000 Subject: [PATCH] added global router gui to NS git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7250 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../gui/GlobalRouterStatistics.cpp | 222 ++++++++++++++++++ .../gui/GlobalRouterStatistics.h | 72 ++++++ .../gui/GlobalRouterStatistics.ui | 55 +++++ .../gui/NetworkSimulatorGUI.cpp | 13 +- .../gui/NetworkSimulatorGUI.h | 2 + .../network_simulator/gui/NetworkViewer.cpp | 64 ++++- .../network_simulator/gui/NetworkViewer.h | 8 +- .../src/tests/network_simulator/gui/gui.pro | 10 +- .../nscore/MonitoredTurtleClient.h | 1 + .../network_simulator/nscore/PeerNode.cpp | 9 +- .../tests/network_simulator/nscore/PeerNode.h | 8 + 11 files changed, 445 insertions(+), 19 deletions(-) create mode 100644 libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.cpp create mode 100644 libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.h create mode 100644 libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.ui diff --git a/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.cpp b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.cpp new file mode 100644 index 000000000..d134a2456 --- /dev/null +++ b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.cpp @@ -0,0 +1,222 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 20011, RetroShare Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include "GlobalRouterStatistics.h" + +static const int MAX_TUNNEL_REQUESTS_DISPLAY = 10 ; + +static QColor colorScale(float f) +{ + if(f == 0) + return QColor::fromHsv(0,0,192) ; + else + return QColor::fromHsv((int)((1.0-f)*280),200,255) ; +} + +GlobalRouterStatistics::GlobalRouterStatistics(QWidget *parent) + : RsAutoUpdatePage(2000,parent) +{ + setupUi(this) ; + + _router_F->setWidget( _tst_CW = new GlobalRouterStatisticsWidget() ) ; +} + +GlobalRouterStatistics::~GlobalRouterStatistics() +{ +} + +void GlobalRouterStatistics::setGlobalRouter(const RsGRouter *grouter) +{ + _grouter = const_cast(grouter); + updateDisplay() ; +} +void GlobalRouterStatistics::updateDisplay() +{ + _tst_CW->updateContent(_grouter) ; +} + +QString GlobalRouterStatistics::getPeerName(const RsPeerId &peer_id) +{ + static std::map names ; + + std::map::const_iterator it = names.find(peer_id) ; + + if( it != names.end()) + return it->second ; + else + { + RsPeerDetails detail ; + if(!rsPeers->getPeerDetails(peer_id,detail)) + return tr("Unknown Peer"); + + return (names[peer_id] = QString::fromUtf8(detail.name.c_str())) ; + } +} + +GlobalRouterStatisticsWidget::GlobalRouterStatisticsWidget(QWidget *parent) + : QWidget(parent) +{ + maxWidth = 400 ; + maxHeight = 0 ; +} + +void GlobalRouterStatisticsWidget::updateContent(RsGRouter *grouter) +{ + std::vector cache_infos ; + RsGRouter::GRouterRoutingMatrixInfo matrix_info ; + + grouter->getRoutingCacheInfo(cache_infos) ; + grouter->getRoutingMatrixInfo(matrix_info) ; + + // What do we need to draw? + // + // Routing matrix + // Key [][][][][][][][][][] + // + // -> each [] shows a square (one per friend location) that is the routing probabilities for all connected friends + // computed using the "computeRoutingProbabilitites()" method. + // + // Own key ids + // key service id description + // + // Data items + // Msg id Local origin Destination Time Status + // + static const int cellx = 6 ; + static const int celly = 10+4 ; + + QPixmap tmppixmap(maxWidth, maxHeight); + tmppixmap.fill(this, 0, 0); + setFixedHeight(maxHeight); + + QPainter painter(&tmppixmap); + painter.initFrom(this); + painter.setPen(QColor::fromRgb(0,0,0)) ; + + maxHeight = 500 ; + + // std::cerr << "Drawing into pixmap of size " << maxWidth << "x" << maxHeight << std::endl; + // draw... + int ox=5,oy=5 ; + + painter.drawText(ox,oy+celly,tr("Pending packets")+":" + QString::number(cache_infos.size())) ; oy += celly*2 ; + + for(uint32_t i=0;i::const_iterator it(matrix_info.published_keys.begin());it!=matrix_info.published_keys.end();++it) + { + QString packet_string ; + packet_string += QString::fromStdString(it->first.toStdString()) ; + packet_string += tr(" : Service ID = ")+QString::number(it->second.service_id,16) ; + packet_string += " \""+QString::fromUtf8(it->second.description_string.c_str()) + "\"" ; + + painter.drawText(ox+2*cellx,oy+celly,packet_string ) ; oy += celly ; + } + oy += celly ; + + QString prob_string ; + + painter.drawText(ox+0*cellx,oy+celly,tr("Routing matrix (")) ; + + // draw scale + + for(int i=0;i<100;++i) + { + painter.setPen(colorScale(i/100.0)) ; + painter.drawLine(ox+120+i,oy+celly+2,ox+120+i,oy+2) ; + } + painter.setPen(QColor::fromRgb(0,0,0)) ; + + painter.drawText(ox+230,oy+celly,")") ; + + oy += celly ; + oy += celly ; + + static const int MaxKeySize = 20 ; + + for(std::map >::const_iterator it(matrix_info.per_friend_probabilities.begin());it!=matrix_info.per_friend_probabilities.end();++it) + { + painter.drawText(ox+2*cellx,oy+celly,QString::fromStdString(it->first.toStdString())+" : ") ; + + for(uint32_t i=0;isecond[i])) ; + + oy += celly ; + } + + oy += celly ; + oy += celly ; + + // update the pixmap + // + pixmap = tmppixmap; + maxHeight = oy ; +} + +QString GlobalRouterStatisticsWidget::speedString(float f) +{ + if(f < 1.0f) + return QString("0 B/s") ; + if(f < 1024.0f) + return QString::number((int)f)+" B/s" ; + + return QString::number(f/1024.0,'f',2) + " KB/s"; +} + +void GlobalRouterStatisticsWidget::paintEvent(QPaintEvent */*event*/) +{ + QStylePainter(this).drawPixmap(0, 0, pixmap); +} + +void GlobalRouterStatisticsWidget::resizeEvent(QResizeEvent *event) +{ + QRect TaskGraphRect = geometry(); + maxWidth = TaskGraphRect.width(); + maxHeight = TaskGraphRect.height() ; + + QWidget::resizeEvent(event); +// updateContent(); +} + diff --git a/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.h b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.h new file mode 100644 index 000000000..9c54aa4d6 --- /dev/null +++ b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.h @@ -0,0 +1,72 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 20011, RetroShare Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#pragma once + +#include +#include +#include + +#include "RsAutoUpdatePage.h" +#include "ui_GlobalRouterStatistics.h" + +class GlobalRouterStatisticsWidget ; +class p3GRouter ; + +class GlobalRouterStatistics: public RsAutoUpdatePage, public Ui::GlobalRouterStatistics +{ + Q_OBJECT + + public: + GlobalRouterStatistics(QWidget *parent = NULL) ; + ~GlobalRouterStatistics(); + + // Cache for peer names. + static QString getPeerName(const RsPeerId& peer_id) ; + + void setGlobalRouter(const RsGRouter *grouter) ; + + private: + + virtual void updateDisplay() ; + + GlobalRouterStatisticsWidget *_tst_CW ; + RsGRouter *_grouter ; +} ; + +class GlobalRouterStatisticsWidget: public QWidget +{ + Q_OBJECT + + public: + GlobalRouterStatisticsWidget(QWidget *parent = NULL) ; + + virtual void paintEvent(QPaintEvent *event) ; + virtual void resizeEvent(QResizeEvent *event); + + void updateContent(RsGRouter *grouter) ; + private: + static QString speedString(float f) ; + + QPixmap pixmap ; + int maxWidth,maxHeight ; +}; + diff --git a/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.ui b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.ui new file mode 100644 index 000000000..0e6e28f7b --- /dev/null +++ b/libretroshare/src/tests/network_simulator/gui/GlobalRouterStatistics.ui @@ -0,0 +1,55 @@ + + + GlobalRouterStatistics + + + + 0 + 0 + 611 + 408 + + + + Router Statistics + + + + :/images/rstray3.png:/images/rstray3.png + + + + + + Qt::Vertical + + + + QFrame::NoFrame + + + Qt::ScrollBarAlwaysOff + + + true + + + + + 0 + 0 + 593 + 390 + + + + + + + + + + + + + diff --git a/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.cpp b/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.cpp index aa3cd0072..5016ec57e 100644 --- a/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.cpp +++ b/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.cpp @@ -4,6 +4,7 @@ #include "NetworkSimulatorGUI.h" #include "NetworkViewer.h" #include "TurtleRouterStatistics.h" +#include "GlobalRouterStatistics.h" NetworkSimulatorGUI::NetworkSimulatorGUI(Network& net) { @@ -16,13 +17,19 @@ NetworkSimulatorGUI::NetworkSimulatorGUI(Network& net) QObject::connect(_viewer,SIGNAL(nodeSelected(int)),this,SLOT(updateSelectedNode(int))) ; QObject::connect(flow_CB,SIGNAL(toggled(bool)),this,SLOT(toggleNetworkTraffic(bool))) ; - QVBoxLayout *layout2 = new QVBoxLayout(inspectorFrame) ; - layout2->addWidget(_turtle_router_statistics = new TurtleRouterStatistics() ) ; + QVBoxLayout *layout2 = new QVBoxLayout(inspectorFrame) ; + QTabWidget *tabwidget = new QTabWidget() ; + + layout2->addWidget(tabwidget) ; + + tabwidget->addTab(_turtle_router_statistics = new TurtleRouterStatistics(),"Turtle router" ) ; + tabwidget->addTab(_global_router_statistics = new GlobalRouterStatistics(),"Global router" ) ; } void NetworkSimulatorGUI::updateSelectedNode(int node_id) { - _turtle_router_statistics->setTurtleRouter( _viewer->network().node(node_id).turtle_service() ) ; + _turtle_router_statistics->setTurtleRouter( _viewer->network().node(node_id).turtle_service() ) ; + _global_router_statistics->setGlobalRouter( _viewer->network().node(node_id).global_router_service() ) ; } void NetworkSimulatorGUI::toggleNetworkTraffic(bool b) diff --git a/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.h b/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.h index 09a453e7f..ddd2abef6 100644 --- a/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.h +++ b/libretroshare/src/tests/network_simulator/gui/NetworkSimulatorGUI.h @@ -1,6 +1,7 @@ #include "ui_NetworkSimulatorGUI.h" class TurtleRouterStatistics ; +class GlobalRouterStatistics ; class NetworkViewer ; class Network ; @@ -20,6 +21,7 @@ class NetworkSimulatorGUI: public QMainWindow, public Ui::NetworkSimulatorGUI private: NetworkViewer *_viewer ; TurtleRouterStatistics *_turtle_router_statistics ; + GlobalRouterStatistics *_global_router_statistics ; int tickTimerId ; }; diff --git a/libretroshare/src/tests/network_simulator/gui/NetworkViewer.cpp b/libretroshare/src/tests/network_simulator/gui/NetworkViewer.cpp index 951b2d0bd..64797c991 100644 --- a/libretroshare/src/tests/network_simulator/gui/NetworkViewer.cpp +++ b/libretroshare/src/tests/network_simulator/gui/NetworkViewer.cpp @@ -5,6 +5,7 @@ #include #include "nscore/Network.h" +#include "nscore/MonitoredGRouterClient.h" #include "NetworkViewer.h" NetworkViewer::NetworkViewer(QWidget *parent,Network&net) @@ -32,8 +33,11 @@ NetworkViewer::NetworkViewer(QWidget *parent,Network&net) connect(this,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(contextMenu(QPoint))); - action_ManageHash = new QAction(QString("Manage new random hash"),this) ; - QObject::connect(action_ManageHash,SIGNAL(triggered()),this,SLOT(actionManageHash())) ; + action_ClientForHash = new QAction(QString("Client for new random hash"),this) ; + QObject::connect(action_ClientForHash,SIGNAL(triggered()),this,SLOT(actionClientForHash())) ; + + action_ProvideGRKey = new QAction(QString("Provide new GRouter key"),this) ; + QObject::connect(action_ProvideGRKey,SIGNAL(triggered()),this,SLOT(actionProvideGRKey())) ; setMouseTracking(true) ; } @@ -514,7 +518,7 @@ void NetworkViewer::contextMenu(QPoint p) QMenu contextMnu ;//= ui.msgText->createStandardContextMenu(matrix.map(point)); - contextMnu.addAction(action_ManageHash); + contextMnu.addAction(action_ClientForHash); if(_current_acted_node == -1) return ; @@ -545,19 +549,27 @@ void NetworkViewer::contextMenu(QPoint p) } if(!provided_hashes.empty()) { - QMenu *Mnu2 = contextMnu.addMenu("Manage hash") ; + QMenu *Mnu2 = contextMnu.addMenu("Client for hash") ; for(std::set::const_iterator it(provided_hashes.begin());it!=provided_hashes.end();++it) { QAction* manage_hash_action = new QAction(QString::fromStdString((*it).toStdString()), Mnu2); - connect(manage_hash_action, SIGNAL(triggered()), this, SLOT(actionManageHash())); + connect(manage_hash_action, SIGNAL(triggered()), this, SLOT(actionClientForHash())); Mnu2->addAction(manage_hash_action); } - } - contextMnu.exec(mapToGlobal(p)); + } + contextMnu.addSeparator() ; + + // GRouter stuff + + contextMnu.addAction(action_ProvideGRKey); + + // Execute! + + contextMnu.exec(mapToGlobal(p)); } -void NetworkViewer::actionManageHash() +void NetworkViewer::actionClientForHash() { if(_current_acted_node < 0) return ; @@ -599,4 +611,40 @@ void NetworkViewer::actionProvideHash() updateGL() ; } +void NetworkViewer::actionSendToGRKey() +{ + if(_current_acted_node < 0) + return ; + + GRouterKeyId key_id ; + + if(qobject_cast(sender())->text().length() == 32) //data().toString().toStdString(); + { + key_id = GRouterKeyId(qobject_cast(sender())->text().toStdString()) ; + + std::cerr << "Sending to existing key " << key_id << std::endl; + } + + std::cerr << " current node = " << _current_acted_node << std::endl ; + std::cerr << " sending message = " << key_id << std::endl; + + _network.node(_current_acted_node).sendToGRKey(key_id) ; + + updateGL() ; +} + +void NetworkViewer::actionProvideGRKey() +{ + if(_current_acted_node < 0) + return ; + + GRouterKeyId key_id = GRouterKeyId::random(); + QString key = QString::fromStdString(key_id.toStdString()) ; + + std::cerr << "Providing new grouter key " << key_id << std::endl; + _network.node(_current_acted_node).provideGRKey(key_id) ; + + updateGL() ; +} + diff --git a/libretroshare/src/tests/network_simulator/gui/NetworkViewer.h b/libretroshare/src/tests/network_simulator/gui/NetworkViewer.h index 3f719b712..f06e493e2 100644 --- a/libretroshare/src/tests/network_simulator/gui/NetworkViewer.h +++ b/libretroshare/src/tests/network_simulator/gui/NetworkViewer.h @@ -43,8 +43,10 @@ class NetworkViewer: public QGLViewer public slots: void timerEvent(QTimerEvent *) ; void contextMenu(QPoint) ; - void actionManageHash() ; + void actionClientForHash() ; void actionProvideHash() ; + void actionSendToGRKey() ; + void actionProvideGRKey() ; private: void calculateForces(const Network::NodeId& node_id,const double *map,int W,int H,float x,float y,float /*speedf*/,float& new_x, float& new_y) ; @@ -73,7 +75,9 @@ class NetworkViewer: public QGLViewer bool _dragging ; bool _nodes_need_recomputing ; - QAction *action_ManageHash ; + QAction *action_SendToGRKey ; + QAction *action_ProvideGRKey ; + QAction *action_ClientForHash ; QAction *action_ProvideHash ; }; diff --git a/libretroshare/src/tests/network_simulator/gui/gui.pro b/libretroshare/src/tests/network_simulator/gui/gui.pro index 4f4bd987a..3c931691e 100644 --- a/libretroshare/src/tests/network_simulator/gui/gui.pro +++ b/libretroshare/src/tests/network_simulator/gui/gui.pro @@ -1,22 +1,22 @@ TEMPLATE = app -CONFIG *= qt qglviewer +CONFIG *= qt qglviewer uic QT *= xml opengl INCLUDEPATH *= ../../.. .. TARGET = NetworkSim -DESTDIR = bin +DESTDIR = ../bin PRE_TARGETDEPS = ../nscore/nscore.pro SOURCES = main.cpp NetworkViewer.cpp NetworkSimulatorGUI.cpp \ - TurtleRouterStatistics.cpp RsAutoUpdatePage.cpp + TurtleRouterStatistics.cpp RsAutoUpdatePage.cpp GlobalRouterStatistics.cpp HEADERS = NetworkViewer.h NetworkSimulatorGUI.h \ - TurtleRouterStatistics.h RsAutoUpdatePage.h + TurtleRouterStatistics.h RsAutoUpdatePage.h GlobalRouterStatistics.h -FORMS = NetworkSimulatorGUI.ui TurtleRouterStatistics.ui +FORMS = NetworkSimulatorGUI.ui TurtleRouterStatistics.ui GlobalRouterStatistics.ui LIBS *= ../../../lib/libretroshare.a \ ../../../../../libbitdht/src/lib/libbitdht.a \ diff --git a/libretroshare/src/tests/network_simulator/nscore/MonitoredTurtleClient.h b/libretroshare/src/tests/network_simulator/nscore/MonitoredTurtleClient.h index f927f11a6..bb03af705 100644 --- a/libretroshare/src/tests/network_simulator/nscore/MonitoredTurtleClient.h +++ b/libretroshare/src/tests/network_simulator/nscore/MonitoredTurtleClient.h @@ -9,6 +9,7 @@ public: bool handleTunnelRequest(const TurtleFileHash& hash,const RsPeerId& peer_id); void provideFileHash(const RsFileHash& hash); + void requestFileHash(const RsFileHash& hash) ; private: std::map _local_files ; diff --git a/libretroshare/src/tests/network_simulator/nscore/PeerNode.cpp b/libretroshare/src/tests/network_simulator/nscore/PeerNode.cpp index e5dfe0575..623345cde 100644 --- a/libretroshare/src/tests/network_simulator/nscore/PeerNode.cpp +++ b/libretroshare/src/tests/network_simulator/nscore/PeerNode.cpp @@ -67,7 +67,14 @@ void PeerNode::manageFileHash(const RsFileHash& hash) _managed_hashes.insert(hash) ; _turtle->monitorTunnels(hash,_turtle_client) ; } - +void PeerNode::sendToGRKey(const GRouterKeyId& key_id) +{ + _grouter_client->sendMessage(key_id) ; +} +void PeerNode::provideGRKey(const GRouterKeyId& key_id) +{ + _grouter_client->provideKey(key_id) ; +} void PeerNode::getTrafficInfo(NodeTrafficInfo& info) { std::vector > hashes_info ; diff --git a/libretroshare/src/tests/network_simulator/nscore/PeerNode.h b/libretroshare/src/tests/network_simulator/nscore/PeerNode.h index d49193941..1d9abc910 100644 --- a/libretroshare/src/tests/network_simulator/nscore/PeerNode.h +++ b/libretroshare/src/tests/network_simulator/nscore/PeerNode.h @@ -2,6 +2,7 @@ #include #include +#include class MonitoredTurtleClient ; class MonitoredGRouterClient ; @@ -34,6 +35,7 @@ class PeerNode // Turtle-related methods // const RsTurtle *turtle_service() const { return _turtle ; } + const RsGRouter *global_router_service() const { return _grouter ; } void manageFileHash(const RsFileHash& hash) ; void provideFileHash(const RsFileHash& hash) ; @@ -45,6 +47,11 @@ class PeerNode // GRouter-related methods // + void provideGRKey(const GRouterKeyId& key_id) ; + void sendToGRKey(const GRouterKeyId& key_id) ; + + const std::set& providedGRKeys() const { return _provided_keys; } + private: p3ServiceServer *_service_server ; pqiPublisher *_publisher ; @@ -62,5 +69,6 @@ class PeerNode std::set _provided_hashes ; std::set _managed_hashes ; + std::set _provided_keys ; };