/**************************************************************** * 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 #include #include #include "GlobalRouterStatistics.h" #include "gui/settings/rsharesettings.h" #include "util/QtVersion.h" #include "util/misc.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) ; m_bProcessSettings = false; _router_F->setWidget( _tst_CW = new GlobalRouterStatisticsWidget() ) ; /* Set header resize modes and initial section sizes Uploads TreeView*/ QHeaderView_setSectionResizeMode(treeWidget->header(), QHeaderView::ResizeToContents); // load settings processSettings(true); } GlobalRouterStatistics::~GlobalRouterStatistics() { // save settings processSettings(false); } void GlobalRouterStatistics::processSettings(bool bLoad) { m_bProcessSettings = true; Settings->beginGroup(QString("GlobalRouterStatistics")); if (bLoad) { // load settings // state of splitter //splitter->restoreState(Settings->value("Splitter").toByteArray()); } else { // save settings // state of splitter //Settings->setValue("Splitter", splitter->saveState()); } Settings->endGroup(); m_bProcessSettings = false; } void GlobalRouterStatistics::updateDisplay() { _tst_CW->updateContent() ; updateContent(); } 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())) ; } } void GlobalRouterStatistics::updateContent() { std::vector cache_infos ; rsGRouter->getRoutingCacheInfo(cache_infos) ; treeWidget->clear(); static const QString data_status_string[6] = { "Unkown","Pending","Sent","Receipt OK","Ongoing","Done" } ; static const QString tunnel_status_string[4] = { "Unmanaged", "Pending", "Ready" } ; time_t now = time(NULL) ; groupBox->setTitle(tr("Pending packets")+": " + QString::number(cache_infos.size()) ); for(int i=0;iaddTopLevelItem(item); RsIdentityDetails details ; rsIdentity->getIdDetails(cache_infos[i].destination,details); QString nicknames = QString::fromUtf8(details.mNickname.c_str()); if(nicknames.isEmpty()) nicknames = tr("Unknown"); item -> setData(0, Qt::DisplayRole, QString::number(cache_infos[i].mid,16).rightJustified(16,'0')); item -> setData(1, Qt::DisplayRole, nicknames + "@" + QString::fromStdString(cache_infos[i].destination.toStdString())); item -> setData(2, Qt::DisplayRole, data_status_string[cache_infos[i].data_status % 6]); item -> setData(3, Qt::DisplayRole, tunnel_status_string[cache_infos[i].tunnel_status % 3]); item -> setData(4, Qt::DisplayRole, misc::friendlyUnit(cache_infos[i].data_size)); item -> setData(5, Qt::DisplayRole, QString::fromStdString(cache_infos[i].item_hash.toStdString())); item -> setData(6, Qt::DisplayRole, QString::number(now - cache_infos[i].routing_time)); item -> setData(7, Qt::DisplayRole, QString::number(now - cache_infos[i].last_sent_time)); } } GlobalRouterStatisticsWidget::GlobalRouterStatisticsWidget(QWidget *parent) : QWidget(parent) { float size = QFontMetricsF(font()).height() ; float fact = size/14.0 ; maxWidth = 400*fact ; maxHeight = 0 ; } void GlobalRouterStatisticsWidget::updateContent() { RsGRouter::GRouterRoutingMatrixInfo matrix_info ; rsGRouter->getRoutingMatrixInfo(matrix_info) ; float size = QFontMetricsF(font()).height() ; float fact = size/14.0 ; // What do we need to draw? // // Routing matrix // Key [][][][][][][][][][] // // -> each [] shows a square (one per friend node) 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 // QPixmap tmppixmap(maxWidth, maxHeight); tmppixmap.fill(Qt::transparent); setFixedHeight(maxHeight); QPainter painter(&tmppixmap); painter.initFrom(this); painter.setPen(QColor::fromRgb(0,0,0)) ; QFont times_f(font());//"Times") ; QFont monospace_f("Monospace") ; monospace_f.setStyleHint(QFont::TypeWriter) ; monospace_f.setPointSize(font().pointSize()) ; QFontMetricsF fm_monospace(monospace_f) ; QFontMetricsF fm_times(times_f) ; static const int cellx = fm_monospace.width(QString(" ")) ; static const int celly = fm_monospace.height() ; maxHeight = 500*fact ; // std::cerr << "Drawing into pixmap of size " << maxWidth << "x" << maxHeight << std::endl; // draw... int ox=5*fact,oy=5*fact ; painter.setFont(times_f) ; painter.drawText(ox,oy+celly,tr("Managed keys")+":" + QString::number(matrix_info.published_keys.size())) ; oy += celly*2 ; painter.setFont(monospace_f) ; for(std::map::const_iterator it(matrix_info.published_keys.begin());it!=matrix_info.published_keys.end();++it) { QString packet_string ; packet_string += QString::fromStdString(it->second.authentication_key.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 ; std::map > tos ; // Now draw the matrix QString prob_string ; painter.setFont(times_f) ; QString Q = tr("Routing matrix (") ; painter.drawText(ox+0*cellx,oy+fm_times.height(),Q) ; // draw scale for(int i=0;i<100*fact;++i) { painter.setPen(colorScale(i/100.0/fact)) ; painter.drawLine(ox+fm_times.width(Q)+i,oy+fm_times.height()*0.5,ox+fm_times.width(Q)+i,oy+fm_times.height()) ; } painter.setPen(QColor::fromRgb(0,0,0)) ; painter.drawText(ox+fm_times.width(Q) + 102*fact,oy+celly,")") ; oy += celly ; oy += celly ; //print friends in the same order their prob is shown QString FO = tr("Friend Order ("); RsPeerDetails peer_ssl_details; for(uint32_t i=0;igetPeerDetails(matrix_info.friend_ids[i], peer_ssl_details); QString fn = QString::fromUtf8(peer_ssl_details.name.c_str()); FO+=fn; FO+=" "; } FO+=")"; painter.drawText(ox+0*cellx,oy+fm_times.height(),FO) ; oy += celly ; oy += celly ; static const int MaxKeySize = 20*fact ; painter.setFont(monospace_f) ; for(std::map >::const_iterator it(matrix_info.per_friend_probabilities.begin());it!=matrix_info.per_friend_probabilities.end();++it) { bool is_null = true ; for(uint32_t i=0;isecond[i] > 0.0) is_null = false ; if(!is_null) { QString ids = QString::fromStdString(it->first.toStdString())+" : " ; painter.drawText(ox+2*cellx,oy+celly,ids) ; 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(); }