[tor-commits] [nyx/master] Use Connection struct in constructors

atagar at torproject.org atagar at torproject.org
Tue Sep 22 17:08:39 UTC 2015


commit 005d7c3634dae07c8d412144e8d73ed7e96cdee9
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jul 16 08:45:08 2015 -0700

    Use Connection struct in constructors
    
    Our tracker uses a connection struct, so no point in passing around all these
    attributes individually. We also now use the connection's start_time rather
    than determining it in the ConnectionEntry.
---
 nyx/connections/circ_entry.py |    9 +++++----
 nyx/connections/conn_entry.py |   29 ++++++++++++++---------------
 nyx/connections/conn_panel.py |   21 ++++++++-------------
 3 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/nyx/connections/circ_entry.py b/nyx/connections/circ_entry.py
index 529242c..5e27243 100644
--- a/nyx/connections/circ_entry.py
+++ b/nyx/connections/circ_entry.py
@@ -9,6 +9,7 @@ followed by an entry for each hop in the circuit. For instance:
 """
 
 import curses
+import time
 
 import nyx.util.tracker
 import nyx.util.ui_tools
@@ -20,7 +21,7 @@ from stem.util import str_tools
 
 class CircEntry(conn_entry.ConnectionEntry):
   def __init__(self, circuit_id, status, purpose, path):
-    conn_entry.ConnectionEntry.__init__(self, '127.0.0.1', '0', '127.0.0.1', '0')
+    conn_entry.ConnectionEntry.__init__(self, nyx.util.tracker.Connection(time.time(), False, '127.0.0.1', 0, '127.0.0.1', 0, 'tcp'))
 
     self.circuit_id = circuit_id
     self.status = status
@@ -85,13 +86,13 @@ class CircHeaderLine(conn_entry.ConnectionLine):
   """
 
   def __init__(self, circuit_id, purpose):
-    conn_entry.ConnectionLine.__init__(self, '127.0.0.1', '0', '0.0.0.0', '0', False, False)
+    conn_entry.ConnectionLine.__init__(self, nyx.util.tracker.Connection(time.time(), False, '127.0.0.1', 0, '0.0.0.0', 0, 'tcp'), False, False)
     self.circuit_id = circuit_id
     self.purpose = purpose
     self.is_built = False
 
   def set_exit(self, exit_address, exit_port, exit_fingerprint):
-    conn_entry.ConnectionLine.__init__(self, '127.0.0.1', '0', exit_address, exit_port, False, False)
+    conn_entry.ConnectionLine.__init__(self, nyx.util.tracker.Connection(time.time(), False, '127.0.0.1', 0, exit_address, exit_port, 'tcp'), False, False)
     self.is_built = True
     self.foreign.fingerprint_overwrite = exit_fingerprint
 
@@ -136,7 +137,7 @@ class CircLine(conn_entry.ConnectionLine):
   """
 
   def __init__(self, remote_address, remote_port, remote_fingerprint, placement_label):
-    conn_entry.ConnectionLine.__init__(self, '127.0.0.1', '0', remote_address, remote_port)
+    conn_entry.ConnectionLine.__init__(self, nyx.util.tracker.Connection(time.time(), False, '127.0.0.1', 0, remote_address, remote_port, 'tcp'))
     self.foreign.fingerprint_overwrite = remote_fingerprint
     self.placement_label = placement_label
     self.include_port = False
diff --git a/nyx/connections/conn_entry.py b/nyx/connections/conn_entry.py
index 041e57a..7693982 100644
--- a/nyx/connections/conn_entry.py
+++ b/nyx/connections/conn_entry.py
@@ -3,7 +3,6 @@ Connection panel entries related to actual connections to or from the system
 (ie, results seen by netstat, lsof, etc).
 """
 
-import time
 import curses
 
 import nyx.util.tracker
@@ -127,9 +126,10 @@ class ConnectionEntry(entries.ConnectionPanelEntry):
   application, and controller categories.
   """
 
-  def __init__(self, local_address, local_port, remote_address, remote_port):
+  def __init__(self, conn):
     entries.ConnectionPanelEntry.__init__(self)
-    self.lines = [ConnectionLine(local_address, local_port, remote_address, remote_port)]
+    self.connection = conn
+    self.lines = [ConnectionLine(conn)]
 
   def get_sort_value(self, attr, listing_type):
     """
@@ -157,7 +157,7 @@ class ConnectionEntry(entries.ConnectionPanelEntry):
     elif attr == entries.SortAttr.CATEGORY:
       return Category.index_of(connection_line.get_type())
     elif attr == entries.SortAttr.UPTIME:
-      return connection_line.start_time
+      return self.connection.start_time
     elif attr == entries.SortAttr.COUNTRY:
       if connection.is_private_address(self.lines[0].foreign.get_address()):
         return ''
@@ -172,13 +172,12 @@ class ConnectionLine(entries.ConnectionPanelLine):
   Display component of the ConnectionEntry.
   """
 
-  def __init__(self, local_address, local_port, remote_address, remote_port, include_port=True, include_expanded_addresses=True):
+  def __init__(self, conn, include_port=True, include_expanded_addresses=True):
     entries.ConnectionPanelLine.__init__(self)
 
-    self.local = Endpoint(local_address, int(local_port))
-    self.foreign = Endpoint(remote_address, int(remote_port))
-    self.start_time = time.time()
-    self.is_initial_connection = False
+    self.local = Endpoint(conn.local_address, int(conn.local_port))
+    self.foreign = Endpoint(conn.remote_address, int(conn.remote_port))
+    self.connection = conn
 
     # overwrite the local fingerprint with ours
 
@@ -219,14 +218,14 @@ class ConnectionLine(entries.ConnectionPanelLine):
     if listen_addr and ':' in listen_addr:
       my_or_port = listen_addr[listen_addr.find(':') + 1:]
 
-    if local_port in (my_or_port, my_dir_port):
+    if conn.local_port in (my_or_port, my_dir_port):
       self.base_type = Category.INBOUND
       self.local.is_or_port = True
-    elif local_port == my_socks_port:
+    elif conn.local_port == my_socks_port:
       self.base_type = Category.SOCKS
-    elif remote_port in my_hidden_service_ports:
+    elif conn.remote_port in my_hidden_service_ports:
       self.base_type = Category.HIDDEN
-    elif local_port == my_ctl_port:
+    elif conn.local_port == my_ctl_port:
       self.base_type = Category.CONTROL
     else:
       self.base_type = Category.OUTBOUND
@@ -286,11 +285,11 @@ class ConnectionLine(entries.ConnectionPanelLine):
     # fill in the current uptime and return the results
 
     if CONFIG['features.connection.markInitialConnections']:
-      time_prefix = '+' if self.is_initial_connection else ' '
+      time_prefix = '+' if self.connection.is_legacy else ' '
     else:
       time_prefix = ''
 
-    time_label = time_prefix + '%5s' % str_tools.time_label(current_time - self.start_time, 1)
+    time_label = time_prefix + '%5s' % str_tools.time_label(current_time - self.connection.start_time, 1)
     my_listing[2] = (time_label, my_listing[2][1])
 
     return my_listing
diff --git a/nyx/connections/conn_panel.py b/nyx/connections/conn_panel.py
index 87f1654..4fb3fd1 100644
--- a/nyx/connections/conn_panel.py
+++ b/nyx/connections/conn_panel.py
@@ -454,12 +454,11 @@ class ConnectionPanel(panel.Panel, threading.Thread):
     Fetches the newest resolved connections.
     """
 
-    # if we don't have an initialized resolver then this is a no-op
+    conn_resolver = nyx.util.tracker.get_connection_tracker()
 
-    if not nyx.util.tracker.get_connection_tracker().is_alive():
-      return
+    if not conn_resolver.is_alive():
+      return  # if we're not fetching connections then this is a no-op
 
-    conn_resolver = nyx.util.tracker.get_connection_tracker()
     current_resolution_count = conn_resolver.run_counter()
 
     with self._vals_lock:
@@ -469,7 +468,7 @@ class ConnectionPanel(panel.Panel, threading.Thread):
       # new_connections  [(local ip, local port, foreign ip, foreign port)...]
       # new_circuits     {circuit_id => (status, purpose, path)...}
 
-      new_connections = [(conn.local_address, conn.local_port, conn.remote_address, conn.remote_port) for conn in conn_resolver.get_value()]
+      new_connections = conn_resolver.get_value()
       new_circuits = {}
 
       for circ in tor_controller().get_circuits([]):
@@ -493,13 +492,9 @@ class ConnectionPanel(panel.Panel, threading.Thread):
             new_entries.append(old_entry)
             del new_circuits[old_entry.circuit_id]
         elif isinstance(old_entry, conn_entry.ConnectionEntry):
-          connection_line = old_entry.getLines()[0]
-          conn_attr = (connection_line.local.get_address(), connection_line.local.get_port(),
-                       connection_line.foreign.get_address(), connection_line.foreign.get_port())
-
-          if conn_attr in new_connections:
+          if old_entry.connection in new_connections:
             new_entries.append(old_entry)
-            new_connections.remove(conn_attr)
+            new_connections.remove(old_entry.connection)
 
       # Reset any display attributes for the entries we're keeping
 
@@ -508,8 +503,8 @@ class ConnectionPanel(panel.Panel, threading.Thread):
 
       # Adds any new connection and circuit entries.
 
-      for local_address, local_port, remote_address, remote_port in new_connections:
-        new_conn_entry = conn_entry.ConnectionEntry(local_address, local_port, remote_address, remote_port)
+      for conn in new_connections:
+        new_conn_entry = conn_entry.ConnectionEntry(conn)
         new_conn_line = new_conn_entry.getLines()[0]
 
         if new_conn_line.get_type() != conn_entry.Category.CIRCUIT:





More information about the tor-commits mailing list