diff --git a/PeerNetQt/src/PeerNetQt.pro b/PeerNetQt/src/PeerNetQt.pro index c411e5b9e..e31d5ff39 100644 --- a/PeerNetQt/src/PeerNetQt.pro +++ b/PeerNetQt/src/PeerNetQt.pro @@ -18,16 +18,19 @@ QT += network xml script SOURCES += main.cpp\ mainwindow.cpp \ dhtwindow.cpp \ + dhtquery.cpp \ peernet.cc \ netstatebox.cc HEADERS += mainwindow.h \ dhtwindow.h \ + dhtquery.h \ peernet.h \ netstatebox.h FORMS += mainwindow.ui \ - dhtwindow.ui + dhtwindow.ui \ + dhtquery.ui librs { diff --git a/PeerNetQt/src/dhtquery.cpp b/PeerNetQt/src/dhtquery.cpp new file mode 100644 index 000000000..d940b2229 --- /dev/null +++ b/PeerNetQt/src/dhtquery.cpp @@ -0,0 +1,321 @@ +#include "dhtquery.h" +#include "ui_dhtquery.h" +#include +#include + +#include +#include +#include +#include + +DhtQuery::DhtQuery(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::DhtQuery) +{ + ui->setupUi(this); + + // tick for gui update. + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(update())); + timer->start(1000); + + // connect add Peer button. + //connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addPeer())); + //connect(ui->chatLineEdit, SIGNAL(returnPressed()), this, SLOT(sendChat())); +} + +DhtQuery::~DhtQuery() +{ + delete ui; +} + +void DhtQuery::changeEvent(QEvent *e) +{ + QMainWindow::changeEvent(e); + switch (e->type()) { + case QEvent::LanguageChange: + ui->retranslateUi(this); + break; + default: + break; + } +} + +void DhtQuery::setPeerNet(PeerNet *pnet) +{ + mPeerNet = pnet; +} + +void DhtQuery::setQueryId(std::string id) +{ + mQueryId = id; +} + +void DhtQuery::update() +{ + //std::cerr << "DhtQuery::update()" << std::endl; + updateDhtQuery(); + + +} + + +#define QTW_COL_BUCKET 0 +#define QTW_COL_PEERID 1 +#define QTW_COL_IPADDR 2 +#define QTW_COL_FLAGS 3 +#define QTW_COL_FOUND 4 +#define QTW_COL_SEND 5 +#define QTW_COL_RECV 6 + +void DhtQuery::updateDhtQuery() +{ + + /* Hackish display of all Dht peers, should be split into buckets (as children) */ + time_t now = time(NULL); + + bdQuerySummary query; + if (!mPeerNet->get_query_status(mQueryId, query)) + { + return; + } + + ui->queryLine->setText(QString::fromStdString(mQueryId)); + + std::ostringstream statestr; + switch(query.mState) + { + default: + statestr << "Unknown"; + break; + case BITDHT_QUERY_READY: + statestr << "Ready"; + break; + case BITDHT_QUERY_QUERYING: + statestr << "Querying"; + break; + case BITDHT_QUERY_FAILURE: + statestr << "Failure"; + break; + case BITDHT_QUERY_FOUND_CLOSEST: + statestr << "Found Closest"; + break; + case BITDHT_QUERY_PEER_UNREACHABLE: + statestr << "Unreachable"; + break; + case BITDHT_QUERY_SUCCESS: + statestr << "Success"; + break; + } + ui->queryStatusLabel->setText(QString::fromStdString(statestr.str())); + + + std::ostringstream extrastr; + extrastr << "QueryLimit: "; + bdStdPrintNodeId(extrastr, &(query.mLimit)); + extrastr << " QueryFlags: " << query.mQueryFlags; + extrastr << " QueryAge: " << now - query.mQueryTS; + extrastr << " SearchTime: " << query.mSearchTime; + extrastr << " IdleRetryPeriod: " << query.mQueryIdlePeerRetryPeriod; + + ui->queryExtraLabel->setText(QString::fromStdString(extrastr.str())); + + std::multimap::iterator cit; + std::list::iterator lit; + + //std::multimap mClosest; + //std::multimap mPotentialClosest; + //std::list mPotentialProxies; + + QTreeWidget *qcTreeWidget = ui->closestTreeWidget; + qcTreeWidget->clear(); + + + for(cit = query.mClosest.begin(); cit != query.mClosest.end(); cit++) + { + /* find the entry */ + QTreeWidgetItem *item = NULL; + bdPeer *bdp = &(cit->second); + + /* insert */ + item = new QTreeWidgetItem(); + + int dist = bdStdBucketDistance(&(cit->first)); + + std::ostringstream buckstr; + buckstr << "(" << std::setw(3) << std::setfill('0') << dist << ") "; + bdStdPrintNodeId(buckstr, &(cit->first)); + + std::ostringstream ipstr; + ipstr << inet_ntoa(bdp->mPeerId.addr.sin_addr); + ipstr << ":" << ntohs(bdp->mPeerId.addr.sin_port); + + std::ostringstream idstr; + bdStdPrintNodeId(idstr, &(bdp->mPeerId.id)); + + std::ostringstream flagsstr; + flagsstr << "0x" << std::hex << std::setfill('0') << bdp->mPeerFlags; + + std::ostringstream foundstr; + foundstr << now - bdp->mFoundTime << " secs ago"; + + std::ostringstream lastsendstr; + if (bdp->mLastSendTime == 0) + { + lastsendstr << "never"; + } + else + { + lastsendstr << now - bdp->mLastSendTime << " secs ago"; + } + + std::ostringstream lastrecvstr; + if (bdp->mLastRecvTime == 0) + { + lastrecvstr << "never"; + } + else + { + lastrecvstr << now - bdp->mLastRecvTime << " secs ago"; + } + + item -> setData(QTW_COL_BUCKET, Qt::DisplayRole, QString::fromStdString(buckstr.str())); + item -> setData(QTW_COL_IPADDR, Qt::DisplayRole, QString::fromStdString(ipstr.str())); + item -> setData(QTW_COL_PEERID, Qt::DisplayRole, QString::fromStdString(idstr.str())); + item -> setData(QTW_COL_FLAGS, Qt::DisplayRole, QString::fromStdString(flagsstr.str())); + + item -> setData(QTW_COL_FOUND, Qt::DisplayRole, QString::fromStdString(foundstr.str())); + item -> setData(QTW_COL_SEND, Qt::DisplayRole, QString::fromStdString(lastsendstr.str())); + item -> setData(QTW_COL_RECV, Qt::DisplayRole, QString::fromStdString(lastrecvstr.str())); + + qcTreeWidget->addTopLevelItem(item); + } + + + QTreeWidget *qpcTreeWidget = ui->potTreeWidget; + qpcTreeWidget->clear(); + + for(cit = query.mPotentialClosest.begin(); cit != query.mPotentialClosest.end(); cit++) + { + /* find the entry */ + QTreeWidgetItem *item = NULL; + bdPeer *bdp = &(cit->second); + + /* insert */ + item = new QTreeWidgetItem(); + + int dist = bdStdBucketDistance(&(cit->first)); + + std::ostringstream buckstr; + buckstr << "(" << std::setw(3) << std::setfill('0') << dist << ") "; + bdStdPrintNodeId(buckstr, &(cit->first)); + + std::ostringstream ipstr; + ipstr << inet_ntoa(bdp->mPeerId.addr.sin_addr); + ipstr << ":" << ntohs(bdp->mPeerId.addr.sin_port); + + std::ostringstream idstr; + bdStdPrintNodeId(idstr, &(bdp->mPeerId.id)); + + std::ostringstream flagsstr; + flagsstr << "0x" << std::hex << std::setfill('0') << bdp->mPeerFlags; + + std::ostringstream foundstr; + foundstr << now - bdp->mFoundTime << " secs ago"; + + std::ostringstream lastsendstr; + if (bdp->mLastSendTime == 0) + { + lastsendstr << "never"; + } + else + { + lastsendstr << now - bdp->mLastSendTime << " secs ago"; + } + + std::ostringstream lastrecvstr; + if (bdp->mLastRecvTime == 0) + { + lastrecvstr << "never"; + } + else + { + lastrecvstr << now - bdp->mLastRecvTime << " secs ago"; + } + + item -> setData(QTW_COL_BUCKET, Qt::DisplayRole, QString::fromStdString(buckstr.str())); + item -> setData(QTW_COL_IPADDR, Qt::DisplayRole, QString::fromStdString(ipstr.str())); + item -> setData(QTW_COL_PEERID, Qt::DisplayRole, QString::fromStdString(idstr.str())); + item -> setData(QTW_COL_FLAGS, Qt::DisplayRole, QString::fromStdString(flagsstr.str())); + + item -> setData(QTW_COL_FOUND, Qt::DisplayRole, QString::fromStdString(foundstr.str())); + item -> setData(QTW_COL_SEND, Qt::DisplayRole, QString::fromStdString(lastsendstr.str())); + item -> setData(QTW_COL_RECV, Qt::DisplayRole, QString::fromStdString(lastrecvstr.str())); + + qpcTreeWidget->addTopLevelItem(item); + } + + QTreeWidget *qpTreeWidget = ui->proxyTreeWidget; + qpTreeWidget->clear(); + + for(lit = query.mPotentialProxies.begin(); lit != query.mPotentialProxies.end(); lit++) + { + /* find the entry */ + QTreeWidgetItem *item = NULL; + bdPeer *bdp = &(*lit); + + /* insert */ + item = new QTreeWidgetItem(); + + std::ostringstream buckstr; + buckstr << "n/a"; + //bdStdPrintNodeId(buckstr, &(lit->first)); + + std::ostringstream ipstr; + ipstr << inet_ntoa(bdp->mPeerId.addr.sin_addr); + ipstr << ":" << ntohs(bdp->mPeerId.addr.sin_port); + + std::ostringstream idstr; + bdStdPrintNodeId(idstr, &(bdp->mPeerId.id)); + + std::ostringstream flagsstr; + flagsstr << "0x" << std::hex << std::setfill('0') << bdp->mPeerFlags; + + std::ostringstream foundstr; + foundstr << now - bdp->mFoundTime << " secs ago"; + + std::ostringstream lastsendstr; + if (bdp->mLastSendTime == 0) + { + lastsendstr << "never"; + } + else + { + lastsendstr << now - bdp->mLastSendTime << " secs ago"; + } + + std::ostringstream lastrecvstr; + if (bdp->mLastRecvTime == 0) + { + lastrecvstr << "never"; + } + else + { + lastrecvstr << now - bdp->mLastRecvTime << " secs ago"; + } + + item -> setData(QTW_COL_BUCKET, Qt::DisplayRole, QString::fromStdString(buckstr.str())); + item -> setData(QTW_COL_IPADDR, Qt::DisplayRole, QString::fromStdString(ipstr.str())); + item -> setData(QTW_COL_PEERID, Qt::DisplayRole, QString::fromStdString(idstr.str())); + item -> setData(QTW_COL_FLAGS, Qt::DisplayRole, QString::fromStdString(flagsstr.str())); + + item -> setData(QTW_COL_FOUND, Qt::DisplayRole, QString::fromStdString(foundstr.str())); + item -> setData(QTW_COL_SEND, Qt::DisplayRole, QString::fromStdString(lastsendstr.str())); + item -> setData(QTW_COL_RECV, Qt::DisplayRole, QString::fromStdString(lastrecvstr.str())); + + qpTreeWidget->addTopLevelItem(item); + } +} + + + diff --git a/PeerNetQt/src/dhtquery.h b/PeerNetQt/src/dhtquery.h new file mode 100644 index 000000000..d7a5adb25 --- /dev/null +++ b/PeerNetQt/src/dhtquery.h @@ -0,0 +1,34 @@ +#ifndef DHTQUERYWINDOW_H +#define DHTQUERYWINDOW_H + +#include +#include "peernet.h" + +namespace Ui { + class DhtQuery; +} + +class DhtQuery : public QMainWindow { + Q_OBJECT +public: + DhtQuery(QWidget *parent = 0); + ~DhtQuery(); + + void setPeerNet(PeerNet *pnet); + void setQueryId(std::string id); + + void updateDhtQuery(); + +public slots: + void update(); + +protected: + void changeEvent(QEvent *e); + +private: + Ui::DhtQuery *ui; + PeerNet *mPeerNet; + std::string mQueryId; +}; + +#endif // DHTQUERYWINDOW_H diff --git a/PeerNetQt/src/dhtquery.ui b/PeerNetQt/src/dhtquery.ui new file mode 100644 index 000000000..96b35e243 --- /dev/null +++ b/PeerNetQt/src/dhtquery.ui @@ -0,0 +1,195 @@ + + + DhtQuery + + + true + + + + 0 + 0 + 644 + 590 + + + + DhtQueryWindow + + + + + + + + + true + + + Query Peer Id + + + true + + + + + + + Query Status + + + + + + + + + Query Extra + + + + + + + Qt::Vertical + + + + + Metrix + + + + + Closest Id + + + + + Ip Address + + + + + Flags + + + + + Found + + + + + Last Sent + + + + + Last Recv + + + + + + + Metrix + + + + + Potential Peer Id + + + + + IP Address + + + + + Flags + + + + + Found + + + + + Last Sent + + + + + Last Recv + + + + + + + Metrix + + + + + Ip Address + + + + + Potential Proxy Id + + + + + Flags + + + + + Found + + + + + Last Sent + + + + + Last Recv + + + + + + + + + + + 0 + 0 + 644 + 22 + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/PeerNetQt/src/dhtwindow.cpp b/PeerNetQt/src/dhtwindow.cpp index f93e8bb0b..ba18c9a1d 100644 --- a/PeerNetQt/src/dhtwindow.cpp +++ b/PeerNetQt/src/dhtwindow.cpp @@ -1,4 +1,5 @@ #include "dhtwindow.h" +#include "dhtquery.h" #include "ui_dhtwindow.h" #include #include @@ -8,6 +9,13 @@ #include #include + +#define QTW_COL_PEERID 0 +#define QTW_COL_STATUS 1 +#define QTW_COL_FLAGS 2 +#define QTW_COL_RESULTS 3 + + DhtWindow::DhtWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::DhtWindow) @@ -20,8 +28,8 @@ DhtWindow::DhtWindow(QWidget *parent) : timer->start(1000); // connect add Peer button. - //connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addPeer())); - //connect(ui->chatLineEdit, SIGNAL(returnPressed()), this, SLOT(sendChat())); + connect(ui->queryTreeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(setQueryId())); + connect(ui->queryButton, SIGNAL(clicked()), this, SLOT(showQuery())); } DhtWindow::~DhtWindow() @@ -47,6 +55,36 @@ void DhtWindow::setPeerNet(PeerNet *pnet) } +void DhtWindow::setDhtQuery(DhtQuery *qw) +{ + mQueryWindow = qw; +} + + +void DhtWindow::showQuery() +{ + mQueryWindow->show(); +} + + +void DhtWindow::setQueryId() +{ + std::cerr << "DhtWindow::setQueryId()"; + std::cerr << std::endl; + + /* get the item that is selected in the queryWindow */ + QTreeWidget *queryTreeWidget = ui->queryTreeWidget; + QTreeWidgetItem *item = queryTreeWidget->currentItem(); + if (item) + { + std::string id = item->data(QTW_COL_PEERID, Qt::DisplayRole).toString().toStdString(); + mQueryWindow->setQueryId(id); + std::cerr << "Setting Query Id to: " << id; + std::cerr << std::endl; + } +} + + void DhtWindow::update() { //std::cerr << "DhtWindow::update()" << std::endl; @@ -146,10 +184,7 @@ void DhtWindow::updateDhtPeers() } -#define QTW_COL_PEERID 0 -#define QTW_COL_STATUS 1 -#define QTW_COL_FLAGS 2 -#define QTW_COL_RESULTS 3 + void DhtWindow::updateDhtQueries() { diff --git a/PeerNetQt/src/dhtwindow.h b/PeerNetQt/src/dhtwindow.h index bf7069086..1e8ea8ffa 100644 --- a/PeerNetQt/src/dhtwindow.h +++ b/PeerNetQt/src/dhtwindow.h @@ -4,6 +4,8 @@ #include #include "peernet.h" +#include "dhtquery.h" + namespace Ui { class DhtWindow; } @@ -15,12 +17,16 @@ public: ~DhtWindow(); void setPeerNet(PeerNet *pnet); + void setDhtQuery(DhtQuery *qw); void updateDhtPeers(); void updateDhtQueries(); public slots: void update(); + void setQueryId(); + void showQuery(); + protected: void changeEvent(QEvent *e); @@ -28,6 +34,7 @@ protected: private: Ui::DhtWindow *ui; PeerNet *mPeerNet; + DhtQuery *mQueryWindow; }; #endif // DHTWINDOW_H diff --git a/PeerNetQt/src/dhtwindow.ui b/PeerNetQt/src/dhtwindow.ui index 61a2c430c..daa113ff8 100644 --- a/PeerNetQt/src/dhtwindow.ui +++ b/PeerNetQt/src/dhtwindow.ui @@ -18,12 +18,41 @@ + + 0 + - - - DHT Details: + + + -1 - + + + + + 5 + 0 + + + + DHT Details: + + + + + + + + 1 + 0 + + + + Show Query Details + + + + diff --git a/PeerNetQt/src/main.cpp b/PeerNetQt/src/main.cpp index d2b8bbb99..20cfbe39e 100644 --- a/PeerNetQt/src/main.cpp +++ b/PeerNetQt/src/main.cpp @@ -5,6 +5,7 @@ #include #include "mainwindow.h" #include "dhtwindow.h" +#include "dhtquery.h" /* for static PThreads under windows... we need to init the library... * Not sure if this is needed? @@ -145,10 +146,15 @@ int main(int argc, char *argv[]) w.show(); DhtWindow dw; dw.hide(); + DhtQuery qw; + qw.hide(); w.setPeerNet(pnet); w.setDhtWindow(&dw); dw.setPeerNet(pnet); + dw.setDhtQuery(&qw); + qw.setPeerNet(pnet); + qw.setQueryId(""); return a.exec(); }