[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1209: Speed up saving geographic location information by not doing (trunk/src/gui/network)



Author: edmanm
Date: 2006-09-19 22:14:05 -0400 (Tue, 19 Sep 2006)
New Revision: 1209

Modified:
   trunk/src/gui/network/netviewer.cpp
   trunk/src/gui/network/netviewer.h
   trunk/src/gui/network/routerlistwidget.cpp
   trunk/src/gui/network/routerlistwidget.h
Log:
Speed up saving geographic location information by not doing a linear search
for matching IP addresses in the server list.


Modified: trunk/src/gui/network/netviewer.cpp
===================================================================
--- trunk/src/gui/network/netviewer.cpp	2006-09-18 21:41:24 UTC (rev 1208)
+++ trunk/src/gui/network/netviewer.cpp	2006-09-20 02:14:05 UTC (rev 1209)
@@ -235,6 +235,7 @@
       if (!_resolveQueue.contains(ip)) {
         _resolveQueue << ip;
       }
+      _resolveMap.insertMulti(rd.ip(), rd.id());
     }
   }
 
@@ -263,7 +264,7 @@
 
   foreach (QString router, circuit.hops()) {
     /* Try to find and select each router in the path */
-    RouterListItem *item = ui.treeRouterList->findRouterItem(router);
+    RouterListItem *item = ui.treeRouterList->findRouterByName(router);
     if (item) {
       routers.append(item->descriptor());
     }
@@ -297,15 +298,26 @@
 NetViewer::resolved(int id, QList<GeoIp> geoips)
 {
   Q_UNUSED(id);
-  QList<RouterListItem *> routers;
+  QString ip;
+  RouterListItem *router;
   
   foreach (GeoIp geoip, geoips) {
-    /* Do something with our awesome new information */
-    routers = ui.treeRouterList->findRouterItems(geoip.ip());
-    foreach (RouterListItem *item, routers) {
-      item->setLocation(geoip.toLocation());
-      _map->addRouter(item->name(), geoip.latitude(), geoip.longitude());
+    /* Find all routers that are at this IP address */
+    ip = geoip.ip().toString();
+    QList<QString> ids = _resolveMap.values(ip);
+
+    /* Update their geographic location information with the results of this
+     * GeoIP query. */
+    foreach (QString id, ids) {
+      router = ui.treeRouterList->findRouterById(id);
+      if (router) {
+        /* Save the location information in the descriptor */
+        router->setLocation(geoip.toLocation());
+        /* Plot the router on the map */
+        _map->addRouter(router->name(), geoip.latitude(), geoip.longitude());
+      }
     }
+    _resolveMap.remove(ip);
   }
 
   /* Update the circuit lines */

Modified: trunk/src/gui/network/netviewer.h
===================================================================
--- trunk/src/gui/network/netviewer.h	2006-09-18 21:41:24 UTC (rev 1208)
+++ trunk/src/gui/network/netviewer.h	2006-09-20 02:14:05 UTC (rev 1209)
@@ -31,6 +31,7 @@
 #include <QStringList>
 #include <QEvent>
 #include <QTimer>
+#include <QHash>
 #include <control/torcontrol.h>
 #include <util/geoip/geoipresolver.h>
 #include <gui/common/vidaliawindow.h>
@@ -91,9 +92,11 @@
   GeoIpResolver _geoip;
   /** Queue for IPs pending resolution to geographic information. */
   QList<QHostAddress> _resolveQueue;
+  /** Maps pending GeoIP requests to server IDs. */
+  QHash<QString, QString> _resolveMap;
   /** Timer used to delay GeoIP requests until we've received "a chunk" of them. */
   QTimer _resolveQueueTimer;
-
+  
   /** Qt Designer generated object **/
   Ui::NetViewer ui;
 };

Modified: trunk/src/gui/network/routerlistwidget.cpp
===================================================================
--- trunk/src/gui/network/routerlistwidget.cpp	2006-09-18 21:41:24 UTC (rev 1208)
+++ trunk/src/gui/network/routerlistwidget.cpp	2006-09-20 02:14:05 UTC (rev 1209)
@@ -127,29 +127,24 @@
   }
 }
 
-/** Finds the list item for the given descriptor. Returns 0 if not found. */
+/** Finds the list item whose key ID matches <b>id</b>. Returns 0 if not 
+ * found. */
 RouterListItem*
-RouterListWidget::findItem(RouterDescriptor rd)
+RouterListWidget::findRouterById(QString id)
 {
-  QList<QTreeWidgetItem *> list = findItems(rd.name(), 
-                                            Qt::MatchExactly, 
-                                            NameColumn);
-
-  /* Many routers can have the same name, so find a match on the ID. */
-  foreach (QTreeWidgetItem *item, list) {
-    RouterListItem *ri = (RouterListItem *)item;
-    if (ri->id() == rd.id()) {
-      return ri;
-    }
+  if (_idmap.contains(id)) {
+    return _idmap.value(id);
   }
   return 0;
 }
 
-/** Finds the list item for the given router name. Returns 0 if not found.*/
+/** Finds the list item whose router nickname matches <b>name</b>. If more 
+ * than one router exists with given name, the first match will be returned. 
+ * Returns 0 if not found. */
 RouterListItem*
-RouterListWidget::findRouterItem(QString router)
+RouterListWidget::findRouterByName(QString name)
 {
-  QList<QTreeWidgetItem *> list = findItems(router,
+  QList<QTreeWidgetItem *> list = findItems(name,
                                             Qt::MatchExactly,
                                             NameColumn);
   /* It's possible that more than one router could have the same name, but
@@ -161,38 +156,25 @@
   return 0;
 }
 
-/** Finds the list item or items for the given router IP. */
-QList<RouterListItem *>
-RouterListWidget::findRouterItems(QHostAddress ip)
-{
-  RouterListItem *item;
-  QList<RouterListItem *> items;
-  
-  int itemCount = topLevelItemCount();
-  for (int i = 0; i < itemCount; i++) {
-    item = (RouterListItem *)topLevelItem(i);
-    if (ip.toString() == item->descriptor().ip()) {
-      items << item;
-    }
-  }
-  return items;
-}
-
 /** Adds a router descriptor to the list. */
 void
 RouterListWidget::addRouter(RouterDescriptor rd)
 {
-  if (!rd.name().isEmpty()) {
-    RouterListItem *item = findItem(rd);
+  QString id = rd.id();
+  if (!id.isEmpty()) {
+    RouterListItem *item = findRouterById(id);
     if (item) {
       /* This is an updated descriptor, so remove the old item and we'll 
        * add a new, updated item. */
       delete takeTopLevelItem(indexOfTopLevelItem(item));
+      _idmap.remove(id);
     }
 
     /* Add the router item to the list and store its descriptor. */
-    insertSorted(new RouterListItem(this, rd));
-  
+    item = new RouterListItem(this, rd);
+    insertSorted(item);
+    _idmap.insert(id, item);
+
     /* Set our status tip to the number of servers in the list */
     setStatusTip(tr("%1 servers total").arg(topLevelItemCount()));
   }

Modified: trunk/src/gui/network/routerlistwidget.h
===================================================================
--- trunk/src/gui/network/routerlistwidget.h	2006-09-18 21:41:24 UTC (rev 1208)
+++ trunk/src/gui/network/routerlistwidget.h	2006-09-20 02:14:05 UTC (rev 1209)
@@ -28,6 +28,7 @@
 #define _ROUTERLISTWIDGET_H
 
 #include <QObject>
+#include <QHash>
 #include <QTreeWidget>
 #include <QHostAddress>
 #include <QKeyEvent>
@@ -56,10 +57,13 @@
   
   /** Adds a new descriptor the list. */
   void addRouter(RouterDescriptor rd);
-  /** Finds the list item for the given router name. */
-  RouterListItem* findRouterItem(QString router);
-  /** Finds the list item or items for the given router IP. */
-  QList<RouterListItem *> findRouterItems(QHostAddress ip);
+  /** Finds the list item whose router nickname matches <b>name</b>. If more
+   * than one router exists with given name, the first match will be
+   * returned.  Returns 0 if not found. */
+  RouterListItem* findRouterByName(QString name);
+  /** Finds the list item whose key ID matches <b>id</b>. Returns 0 if not 
+   * found. */
+  RouterListItem* findRouterById(QString id);
   /** Deselects all currently selected routers. */
   void deselectAll();
 
@@ -78,8 +82,9 @@
 private:
   /** Inserts a new item into the router list, maintaining the current order.*/
   void insertSorted(RouterListItem *item);
-  /** Finds the list item for the given descriptor. */
-  RouterListItem* findItem(RouterDescriptor rd);
+
+  /** Maps a server ID to that server's list item. */
+  QHash<QString,RouterListItem*> _idmap;
 };
 
 #endif