diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-11-20 14:10:33 -0500 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-11-20 14:10:33 -0500 |
commit | 902c8c38d261b1e73329ab4b9fefcfe11995c8b7 (patch) | |
tree | aef3b8de4aaafa64376b74b1ba4a0aa2b2cf47cc | |
parent | 14b0639181f8da7eeb42970b38b0d0701e9ddae6 (diff) | |
download | infinitytier-902c8c38d261b1e73329ab4b9fefcfe11995c8b7.tar.gz infinitytier-902c8c38d261b1e73329ab4b9fefcfe11995c8b7.zip |
UI basically works, almost ready for testing and packaging...
-rw-r--r-- | ZeroTierUI/mainwindow.cpp | 79 | ||||
-rw-r--r-- | ZeroTierUI/mainwindow.h | 24 | ||||
-rw-r--r-- | ZeroTierUI/mainwindow.ui | 2 | ||||
-rw-r--r-- | node/Node.cpp | 5 | ||||
-rw-r--r-- | node/Node.hpp | 11 |
5 files changed, 108 insertions, 13 deletions
diff --git a/ZeroTierUI/mainwindow.cpp b/ZeroTierUI/mainwindow.cpp index 0a9c7228..04a7919d 100644 --- a/ZeroTierUI/mainwindow.cpp +++ b/ZeroTierUI/mainwindow.cpp @@ -17,24 +17,32 @@ #include <QProcess> #include <QStringList> -static std::map< unsigned long,std::vector<std::string> > ztReplies; -static QMutex ztReplies_m; +// Globally visible +ZeroTier::Node::LocalClient *zeroTierClient = (ZeroTier::Node::LocalClient *)0; + +// Main window instance for app +static MainWindow *mainWindow = (MainWindow *)0; + static void handleZTMessage(void *arg,unsigned long id,const char *line) { + static std::map< unsigned long,std::vector<std::string> > ztReplies; + static QMutex ztReplies_m; + ztReplies_m.lock(); if (*line) { ztReplies[id].push_back(std::string(line)); ztReplies_m.unlock(); } else { // empty lines conclude transmissions - std::vector<std::string> resp(ztReplies[id]); - ztReplies.erase(id); - ztReplies_m.unlock(); + std::map< unsigned long,std::vector<std::string> >::iterator r(ztReplies.find(id)); + if (r != ztReplies.end()) { + MainWindow::ZTMessageEvent *event = new MainWindow::ZTMessageEvent(r->second); + ztReplies.erase(r); + ztReplies_m.unlock(); + QCoreApplication::postEvent(mainWindow,event); // must post since this may be another thread + } else ztReplies_m.unlock(); } } -// Globally visible -ZeroTier::Node::LocalClient *volatile zeroTierClient = (ZeroTier::Node::LocalClient *)0; - MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -42,6 +50,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->setupUi(this); this->startTimer(1000); this->setEnabled(false); // gets enabled when updates are received + mainWindow = this; } MainWindow::~MainWindow() @@ -49,6 +58,7 @@ MainWindow::~MainWindow() delete ui; delete zeroTierClient; zeroTierClient = (ZeroTier::Node::LocalClient *)0; + mainWindow = (MainWindow *)0; } void MainWindow::timerEvent(QTimerEvent *event) @@ -86,6 +96,57 @@ void MainWindow::timerEvent(QTimerEvent *event) zeroTierClient = new ZeroTier::Node::LocalClient(authToken.c_str(),0,&handleZTMessage,this); } + + zeroTierClient->send("info"); + zeroTierClient->send("listnetworks"); + zeroTierClient->send("listpeers"); +} + +void MainWindow::customEvent(QEvent *event) +{ + ZTMessageEvent *m = (ZTMessageEvent *)event; // only one custom event type so far + + if (m->ztMessage.size() == 0) + return; + + std::vector<std::string> hdr(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[0])); + if (hdr.size() < 2) + return; + if (hdr[0] != "200") + return; + + // Enable main window on valid communication with service + if (!this->isEnabled()) + this->setEnabled(true); + + if (hdr[1] == "info") { + if (hdr.size() >= 3) + this->myAddress = hdr[2].c_str(); + if (hdr.size() >= 4) + this->myStatus = hdr[3].c_str(); + if (hdr.size() >= 5) + this->myVersion = hdr[4].c_str(); + } else if (hdr[1] == "listnetworks") { + } else if (hdr[1] == "listpeers") { + this->numPeers = 0; + for(unsigned long i=1;i<m->ztMessage.size();++i) { + std::vector<std::string> l(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[i])); + if ((l.size() >= 5)&&((l[3] != "-")||(l[4] != "-"))) + ++this->numPeers; // number of direct peers online -- check for active IPv4 and/or IPv6 address + } + } + + if (this->myAddress.size()) { + QString st(this->myAddress); + st += " ("; + st += this->myStatus; + st += ", "; + st += QString::number(this->numPeers); + st += " peers)"; + while (st.size() < 38) + st += QChar::Space; + ui->statusAndAddressButton->setText(st); + } } void MainWindow::on_joinNetworkButton_clicked() @@ -143,5 +204,5 @@ void MainWindow::on_networkIdLineEdit_textChanged(const QString &text) void MainWindow::on_statusAndAddressButton_clicked() { - // QApplication::clipboard()->setText(ui->myAddressCopyButton->text()); + QApplication::clipboard()->setText(this->myAddress); } diff --git a/ZeroTierUI/mainwindow.h b/ZeroTierUI/mainwindow.h index c072a566..39c4318a 100644 --- a/ZeroTierUI/mainwindow.h +++ b/ZeroTierUI/mainwindow.h @@ -2,6 +2,8 @@ #define MAINWINDOW_H #include <QMainWindow> +#include <QEvent> +#include <QString> #include "../node/Node.hpp" #include "../node/Utils.hpp" @@ -12,18 +14,31 @@ class MainWindow; // Globally visible instance of local client for communicating with ZT1 // Can be null if not connected, or will point to current -extern ZeroTier::Node::LocalClient *volatile zeroTierClient; +extern ZeroTier::Node::LocalClient *zeroTierClient; class MainWindow : public QMainWindow { Q_OBJECT public: + class ZTMessageEvent : public QEvent + { + public: + ZTMessageEvent(const std::vector<std::string> &m) : + QEvent(QEvent::User), + ztMessage(m) + { + } + + std::vector<std::string> ztMessage; + }; + explicit MainWindow(QWidget *parent = 0); - ~MainWindow(); + virtual ~MainWindow(); protected: virtual void timerEvent(QTimerEvent *event); + virtual void customEvent(QEvent *event); private slots: void on_joinNetworkButton_clicked(); @@ -35,6 +50,11 @@ private slots: private: Ui::MainWindow *ui; + + QString myAddress; + QString myStatus; + QString myVersion; + unsigned int numPeers; }; #endif // MAINWINDOW_H diff --git a/ZeroTierUI/mainwindow.ui b/ZeroTierUI/mainwindow.ui index d4824d59..a09460ae 100644 --- a/ZeroTierUI/mainwindow.ui +++ b/ZeroTierUI/mainwindow.ui @@ -131,7 +131,7 @@ <string notr="true">border: 0;</string> </property> <property name="text"> - <string>0000000000 (OFFLINE, 0 peers) </string> + <string>0000000000 (OFFLINE, 0 peers) </string> </property> <property name="toolButtonStyle"> <enum>Qt::ToolButtonTextOnly</enum> diff --git a/node/Node.cpp b/node/Node.cpp index fe8cfb18..c88741a6 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -182,6 +182,11 @@ unsigned long Node::LocalClient::send(const char *command) } } +std::vector<std::string> Node::LocalClient::splitLine(const char *line) +{ + return Utils::split(line," ","\\","\""); +} + struct _NodeImpl { RuntimeEnvironment renv; diff --git a/node/Node.hpp b/node/Node.hpp index 238b5fce..2f7d43ed 100644 --- a/node/Node.hpp +++ b/node/Node.hpp @@ -71,6 +71,15 @@ public: unsigned long send(const char *command) throw(); + /** + * Split a line of results by space + * + * @param line Line to split + * @return Vector of fields + */ + static std::vector<std::string> splitLine(const char *line); + static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); } + private: // LocalClient is not copyable LocalClient(const LocalClient&); @@ -140,7 +149,7 @@ public: /** * Get the ZeroTier version in major.minor.revision string format - * + * * @return Version in string form */ static const char *versionString() |