[or-cvs] [torperf/master] Update the entrycons script

sebastian at torproject.org sebastian at torproject.org
Fri Jan 28 07:49:21 UTC 2011


commit e25a73d61eda553e9aa6cc3e59d65728dd139c42
Author: Sebastian Hahn <sebastian at torproject.org>
Date:   Sat Sep 11 05:14:17 2010 +0200

    Update the entrycons script
    
    Make it work, check that it plays nice with TorCtl
---
 entrycons.py   |  110 ++++++++++++++++++++++++-------------------------------
 extra_stats.py |    3 +-
 2 files changed, 50 insertions(+), 63 deletions(-)

diff --git a/entrycons.py b/entrycons.py
index 70c7753..116806f 100644
--- a/entrycons.py
+++ b/entrycons.py
@@ -1,26 +1,23 @@
-import socket,sys,re,os,getopt
-sys.path.append("/home/broo/tor/torctl/trunk/python/TorCtl")
-import TorCtl
+#!/usr/bin/python
+import sys, time
+import TorCtl.TorCtl as TorCtl
+import TorCtl.TorUtil as TorUtil
 
 HOST = "127.0.0.1"
-PORT = 9060
-AUTH = "broom"
 
 SAMPLE_SIZE = 3
 
-slow = False
-
 class EntryTracker(TorCtl.ConsensusTracker):
-  global slow
   used_entries = []
 
-  def __init__(self, c):
-    TorCtl.ConsensusTracker.__init__(self, c)
+  def __init__(self, conn, slow):
+    TorCtl.ConsensusTracker.__init__(self, conn, consensus_only=False)
+    self._slow = slow
     self.set_entries()
 
   def new_consensus_event(self, n):
     TorCtl.ConsensusTracker.new_consensus_event(self, n)
-    print "DEBUG: New consensus arrived. Rejoice!"
+    TorUtil.plog("INFO", "New consensus arrived. Rejoice!")
     self.used_entries = []
     self.set_entries()
 
@@ -37,13 +34,15 @@ class EntryTracker(TorCtl.ConsensusTracker):
         nodes_list.remove(event.idhex)
         nodes_list.append(self.get_next_router(event.idhex, nodes_list))
         self.c.set_option("EntryNodes", ",".join(nodes_list))
-        print "DEBUG: Entry: " + event.nick + ":" + event.idhex + " died, and we replaced it with: " + nodes_list[2] + "!"
+        TorUtil.plog("NOTICE", "Entry: " + event.nick + ":" + event.idhex +
+                     " died, and we replaced it with: " + nodes_list[-1] + "!")
         nodes_tuple = self.c.get_option("EntryNodes")
         nodes_list = nodes_tuple[0][1]
-        print "DEBUG: New nodes_list: " + nodes_list
+        TorUtil.plog("INFO", "New nodes_list: " + nodes_list)
       except ValueError:
-        print "DEBUG: GUARD event notified of an entry death that is not in nodes_list! Mysterioush!"
-        print "DEBUG: It was: " + event.nick + " : " + event.idhex
+        TorUtil.plog("INFO", "GUARD event notified of an entry death that " +
+                     "is not in nodes_list! Mysterioush!")
+        TorUtil.plog("INFO", "It was: " + event.nick + " : " + event.idhex)
 
   def get_next_router(self, event, nodes_list):
     cons = self.current_consensus()
@@ -51,7 +50,9 @@ class EntryTracker(TorCtl.ConsensusTracker):
 
     i = 0
     while (1):
-      if ((sorted_routers[i].idhex not in self.used_entries) and (not sorted_routers[i].down)):
+      if ((sorted_routers[i].idhex not in self.used_entries) and
+          (not sorted_routers[i].down
+           and "Guard" in sorted_routers[i].flags)):
         self.used_entries.append(sorted_routers[i].idhex)
         return sorted_routers[i].idhex
       i += 1
@@ -59,63 +60,48 @@ class EntryTracker(TorCtl.ConsensusTracker):
   def set_entries(self):
     sorted_routers = self.sorted_r
 
-    if (not slow):
+    if (not self._slow):
       fast_entry_nodes = []
-      for i in range(SAMPLE_SIZE):
-        if (not sorted_routers[i].down):
+      for i in xrange(len(sorted_routers)):
+        if len(fast_entry_nodes) >= SAMPLE_SIZE: break
+        if (not sorted_routers[i].down and "Guard" in sorted_routers[i].flags):
           fast_entry_nodes.append(sorted_routers[i].idhex)
           self.used_entries.append(sorted_routers[i].idhex)
       self.c.set_option("EntryNodes", ",".join(fast_entry_nodes))
     else:
       sorted_routers.reverse()
-      i=0
-      for k in sorted_routers:
-        print sorted_routers[i].nickname + " : " + sorted_routers[i].idhex
-        i += 1
       slow_entry_nodes = []
-      for i in range(SAMPLE_SIZE):
-        if (not sorted_routers[i].down):
+      for i in xrange(len(sorted_routers)):
+        if len(slow_entry_nodes) >= SAMPLE_SIZE: break
+        if (not sorted_routers[i].down and "Guard" in sorted_routers[i].flags):
           slow_entry_nodes.append(sorted_routers[i].idhex)
           self.used_entries.append(sorted_routers[i].idhex)
       self.c.set_option("EntryNodes", ",".join(slow_entry_nodes))
 
-    if (not slow):
-      print "DEBUG: Changed EntryNodes to: " + ",".join(fast_entry_nodes)
+    if (not self._slow):
+      TorUtil.plog("NOTICE", "Changed EntryNodes to: " +
+                   ",".join(map(lambda x: self.ns_map[x].nickname+"="+x,
+                                fast_entry_nodes)))
     else:
-      print "DEBUG: SLOW: Changed EntryNodes to: " + ",".join(slow_entry_nodes)
-
-def usage():
-  print sys.argv[0] + " [options]"
-  print "Options:\n\t -h/--help: Print this cute message."
-  print "\t -s/--slow: Choose slowest guards."
-
-def main(argv):
-
-  global slow
-  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-  s.connect((HOST, PORT))
-  c = TorCtl.Connection(s)
-  th = c.launch_thread()
-  c.authenticate(AUTH)
-
-  c.set_events(["NEWCONSENSUS", "GUARD"])
-  c.set_option("StrictEntryNodes", "1")
-
-  try:
-    opts,args = getopt.getopt(argv, "hs", ["help", "slow"])
-  except getopt.GetoptError:
-    usage()
-    sys.exit(2)
-  for opt, arg in opts:
-    if opt in ("-h","--help"):
-      usage()
-      sys.exit()
-    elif opt in ("-s","--slow"):
-      print ".Shlow."
-      slow = True
-
-  EntryTracker(c)
-  th.join()
+      TorUtil.plog("NOTICE", "SLOW: Changed EntryNodes to: " +
+                   ",".join(map(lambda x: self.ns_map[x].nickname+"="+x,
+                                slow_entry_nodes)))
+
+def main():
+
+  port = int(sys.argv[1])
+  speed = sys.argv[2]
+  
+  if not speed in ("fast", "slow"):
+    TorUtil.plot("ERROR", "Second parameter must be 'fast' or 'slow'");
+
+  conn = TorCtl.connect(HOST, port)
+
+  conn.set_events(["NEWCONSENSUS", "NEWDESC", "NS", "GUARD"])
+  conn.set_option("StrictEntryNodes", "1")
+
+  EntryTracker(conn, speed == "slow")
+  conn.block_until_close()
 
 if __name__ == '__main__':
-  main(sys.argv[1:])
+  main()
diff --git a/extra_stats.py b/extra_stats.py
index 3da4c4e..f0c4ad4 100644
--- a/extra_stats.py
+++ b/extra_stats.py
@@ -10,9 +10,10 @@ class WriteStats(TorCtl.PostEventListener):
     self._filename = filename
     self._conn = None
 
-
   def connect(self):
     self._conn = TorCtl.connect(HOST, self._port)
+    if not self._conn
+      sys.exit(2)
 
   def setup_listener(self):
     self._conn.set_events(["Stream"])





More information about the tor-commits mailing list