gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r5674 - gnunet-qt/src/plugins/stats


From: gnunet
Subject: [GNUnet-SVN] r5674 - gnunet-qt/src/plugins/stats
Date: Sat, 29 Sep 2007 18:55:07 -0600 (MDT)

Author: durner
Date: 2007-09-29 18:55:07 -0600 (Sat, 29 Sep 2007)
New Revision: 5674

Added:
   gnunet-qt/src/plugins/stats/statsThread.cc
   gnunet-qt/src/plugins/stats/statsThread.h
Modified:
   gnunet-qt/src/plugins/stats/stats.pro.in
   gnunet-qt/src/plugins/stats/stats.ui
   gnunet-qt/src/plugins/stats/statsPlugin.cc
   gnunet-qt/src/plugins/stats/statsPlugin.h
Log:
automatic status refresh, show num. of connected nodes in status bar

Modified: gnunet-qt/src/plugins/stats/stats.pro.in
===================================================================
--- gnunet-qt/src/plugins/stats/stats.pro.in    2007-09-29 23:25:55 UTC (rev 
5673)
+++ gnunet-qt/src/plugins/stats/stats.pro.in    2007-09-30 00:55:07 UTC (rev 
5674)
@@ -13,6 +13,6 @@
 target.path = @prefix@/@DLLDIR@
 INSTALLS += target
 
-SOURCES = statsPlugin.cc
-HEADERS = statsPlugin.h ../../include/gnunet_qt_common.h
+SOURCES = statsPlugin.cc statsThread.cc
+HEADERS = statsPlugin.h statsThread.h ../../include/gnunet_qt_common.h
 FORMS = stats.ui
\ No newline at end of file

Modified: gnunet-qt/src/plugins/stats/stats.ui
===================================================================
--- gnunet-qt/src/plugins/stats/stats.ui        2007-09-29 23:25:55 UTC (rev 
5673)
+++ gnunet-qt/src/plugins/stats/stats.ui        2007-09-30 00:55:07 UTC (rev 
5674)
@@ -1,4 +1,7 @@
 <ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
  <class>WndStats</class>
  <widget class="QWidget" name="WndStats" >
   <property name="geometry" >
@@ -45,47 +48,9 @@
      </column>
     </widget>
    </item>
-   <item>
-    <layout class="QHBoxLayout" >
-     <property name="margin" >
-      <number>0</number>
-     </property>
-     <property name="spacing" >
-      <number>6</number>
-     </property>
-     <item>
-      <spacer>
-       <property name="orientation" >
-        <enum>Qt::Horizontal</enum>
-       </property>
-       <property name="sizeHint" >
-        <size>
-         <width>40</width>
-         <height>20</height>
-        </size>
-       </property>
-      </spacer>
-     </item>
-     <item>
-      <widget class="QPushButton" name="pbRefresh" >
-       <property name="text" >
-        <string>Refresh</string>
-       </property>
-       <property name="icon" >
-        <iconset resource="../../../pixmaps/pixmaps.qrc" 
>:/pixmaps/refresh.png</iconset>
-       </property>
-       <property name="iconSize" >
-        <size>
-         <width>20</width>
-         <height>20</height>
-        </size>
-       </property>
-      </widget>
-     </item>
-    </layout>
-   </item>
   </layout>
  </widget>
+ <pixmapfunction></pixmapfunction>
  <resources>
   <include location="../../../pixmaps/pixmaps.qrc" />
  </resources>

Modified: gnunet-qt/src/plugins/stats/statsPlugin.cc
===================================================================
--- gnunet-qt/src/plugins/stats/statsPlugin.cc  2007-09-29 23:25:55 UTC (rev 
5673)
+++ gnunet-qt/src/plugins/stats/statsPlugin.cc  2007-09-30 00:55:07 UTC (rev 
5674)
@@ -46,80 +46,76 @@
   
   this->config = config;
   this->errorContext = errorContext;
+  statsThread = new GStatsThread(config, errorContext);
   
-  connect(pbRefresh, SIGNAL(clicked(bool)), this, SLOT(refresh()));
+  connect(statsThread, SIGNAL(stat(const char *, qulonglong)), this,
+    SLOT(processStat(const char *, qulonglong)), Qt::DirectConnection);
   
   treeStats->setColumnWidth(0, 550);
+  statsThread->start();
 }
 
 GStatsPlugin::~GStatsPlugin()
 {
-}
+  statsThread->stop();
+  if (!statsThread->wait(200))
+    statsThread->terminate();
 
-static int printStatistics(const char *name, unsigned long long value, void 
*param)
-{
-  QTreeWidgetItem *item = new QTreeWidgetItem(0);
-  
-  item->setText(0, QString::fromUtf8(name));
-  item->setText(1, QString::number(value));
-
-  ((GStatsParam *) param)->tree->addTopLevelItem(item);
-  
-  if (strcmp(name, "# of connected peers") == 0)
-     ((GStatsParam *) param)->plugin->setConnectedNodes(value);
-  
-  return OK;
+  delete statsThread;
 }
 
-void GStatsPlugin::refresh()
+void GStatsPlugin::processStat(const char *name, qulonglong value)
 {
-  int res;
-  struct ClientServerConnection *sock;
-  struct GStatsParam param;
-
-  treeStats->clear();
-
-  sock = client_connection_create(errorContext, config);
-  if (!sock)
-  {
-    GE_LOG(errorContext, (GE_KIND) (GE_ERROR | GE_USER | GE_IMMEDIATE),
-      qPrintable(tr("Error establishing connection with background process 
gnunetd.")));
-    return;
-  }
-
-  param.tree = treeStats;
-  param.plugin = this;
-
-#if HAVE_STATS
-  res = STATS_getStatistics(errorContext,
-                           sock,
-                           &printStatistics, 
-                           &param);
-#else
-  res = requestStatistics(errorContext, sock,
-    (StatisticsProcessor) &printStatistics, &param);
-#endif
-  if (res != OK)
-    GE_LOG(errorContext, (GE_KIND) (GE_ERROR | GE_USER | GE_IMMEDIATE),
-      qPrintable(tr("Error reading information from background process 
gnunetd.")));
-  
-  connection_destroy(sock);
-}
-
-void GStatsPlugin::setConnectedNodes(unsigned long long nodes)
-{
-    QPixmap icon;
-  
-    if (nodes == 0)
-      icon.load(":/pixmaps/network-status-offline.png");
-    else
+    int count;
+    bool found;
+    QString strName;
+    
+    strName = QString::fromUtf8(name);
+    found = false;
+    
+    /* populate statistics tree */
+    count = treeStats->topLevelItemCount();
+    while (count > 0)
     {
-      icon.load(":/pixmaps/network-status-online.png");
+      QTreeWidgetItem *item;
+      
+      item = treeStats->topLevelItem(count - 1);
+      
+      if (item->text(0) == strName)
+      {
+        item->setText(1, QString::number(value));
+        found = true;
+      }
+      count--;
     }
-
-    emit setNetworkStatus(icon, QString::number(nodes));
+    
+    if (!found)
+    {
+      QTreeWidgetItem *item = new QTreeWidgetItem(0);
+      
+      item->setText(0, strName);
+      item->setText(1, QString::number(value));
+    
+      treeStats->addTopLevelItem(item);
+    }
+    
+    /* display # of connected peers in status bar */
+    if (strName == "# of connected peers")
+    {
+      QPixmap icon;
+    
+      if (value == 0)
+        icon.load(":/pixmaps/network-status-offline.png");
+      else
+      {
+        icon.load(":/pixmaps/network-status-online.png");
+      }
+  
+      emit setNetworkStatus(icon, QString::number(value));
+    }
 }
 
+
 extern "C"
 {
   

Modified: gnunet-qt/src/plugins/stats/statsPlugin.h
===================================================================
--- gnunet-qt/src/plugins/stats/statsPlugin.h   2007-09-29 23:25:55 UTC (rev 
5673)
+++ gnunet-qt/src/plugins/stats/statsPlugin.h   2007-09-30 00:55:07 UTC (rev 
5674)
@@ -30,6 +30,7 @@
 #include <QObject>
 #include "gnunet_qt_common.h"
 #include "ui_stats.h"
+#include "statsThread.h"
 
 class GStatsPlugin : public GPlugin, public Ui::WndStats
 {
@@ -38,14 +39,14 @@
 public:
        GStatsPlugin(struct GC_Configuration *config, struct GE_Context 
*errorContext);
        virtual ~GStatsPlugin();
-  void setConnectedNodes(unsigned long long nodes);
 
 protected slots:
-  void refresh();
+  void processStat(const char *name, qulonglong value);
 
 protected:
   struct GC_Configuration *config;
   struct GE_Context *errorContext;
+  class GStatsThread *statsThread;
 };
 
 #endif /*STATSPLUGIN_H_*/

Added: gnunet-qt/src/plugins/stats/statsThread.cc
===================================================================
--- gnunet-qt/src/plugins/stats/statsThread.cc                          (rev 0)
+++ gnunet-qt/src/plugins/stats/statsThread.cc  2007-09-30 00:55:07 UTC (rev 
5674)
@@ -0,0 +1,95 @@
+/*
+     This file is part of gnunet-qt.
+     (C) 2007 Nils Durner (and other contributing authors)
+
+     gnunet-qt 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, or (at your
+     option) any later version.
+
+     gnunet-qt 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 gnunet-qt; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file src/plugins/stats/statsThread.cc
+ * @brief thread to gather stats from gnunetd
+ * @author Nils Durner
+ */
+
+#include <QTime>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_stats_lib.h>
+#include "statsThread.h"
+
+GStatsThread::GStatsThread(struct GC_Configuration *config,
+              struct GE_Context *errorContext, QObject *parent)
+{
+  this->config = config;
+  this->errorContext = errorContext;
+  
+  stopSignalled = false;
+}
+
+static int acquireStatistics(const char *name, unsigned long long value, void 
*param)
+{
+  ((GStatsThread *) param)->processStat(name, value);
+    
+  return OK;
+}
+
+void GStatsThread::processStat(const char *name, unsigned long long value)
+{
+   emit stat(name, value);
+}
+
+void GStatsThread::run()
+{
+  int res;
+  QTime timer;
+  struct ClientServerConnection *sock;
+
+  sock = client_connection_create(errorContext, config);
+  if (!sock)
+  {
+    GE_LOG(errorContext, (GE_KIND) (GE_ERROR | GE_USER | GE_IMMEDIATE),
+      qPrintable(tr("Error establishing connection with background process 
gnunetd.")));
+    return;
+  }
+
+  while (! stopSignalled)
+  {
+    timer.start();
+#if HAVE_STATS
+    res = STATS_getStatistics(errorContext,
+            sock,
+            &acquireStatistics, 
+            this);
+#else
+    res = requestStatistics(errorContext, sock,
+      (StatisticsProcessor) &acquireStatistics, this);
+#endif
+
+    if (res != OK)
+      GE_LOG(errorContext, (GE_KIND) (GE_ERROR | GE_USER | GE_IMMEDIATE),
+        qPrintable(tr("Error reading information from background process 
gnunetd.")));
+
+    while (timer.elapsed() < 1000 && !stopSignalled)
+      msleep(100);
+  }
+  
+  connection_destroy(sock);
+}
+
+void GStatsThread::stop()
+{
+  stopSignalled = true;
+}
+

Added: gnunet-qt/src/plugins/stats/statsThread.h
===================================================================
--- gnunet-qt/src/plugins/stats/statsThread.h                           (rev 0)
+++ gnunet-qt/src/plugins/stats/statsThread.h   2007-09-30 00:55:07 UTC (rev 
5674)
@@ -0,0 +1,51 @@
+/*
+     This file is part of gnunet-qt.
+     (C) 2007 Nils Durner (and other contributing authors)
+
+     gnunet-qt 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, or (at your
+     option) any later version.
+
+     gnunet-qt 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 gnunet-qt; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file src/plugins/stats/statsThread.h
+ * @brief thread to gather stats from gnunetd
+ * @author Nils Durner
+ */
+
+#ifndef STATSTHREAD_H_
+#define STATSTHREAD_H_
+
+#include <QThread>
+
+class GStatsThread : public QThread
+{
+  Q_OBJECT
+public:
+  GStatsThread(struct GC_Configuration *config,
+    struct GE_Context *errorContext, QObject *parent = NULL);
+  void run();
+  void stop();
+  
+  void processStat(const char *name, unsigned long long value);
+  
+protected:
+  struct GC_Configuration *config;
+  struct GE_Context *errorContext;
+  bool stopSignalled;
+signals:
+  void stat(const char *name, qulonglong value);
+};
+
+#endif /*STATSTHREAD_H_*/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]