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

[or-cvs] r22475: {arm} added: notice in the header panel when tor's disconnected (in arm/trunk: . interface util)



Author: atagar
Date: 2010-06-06 00:31:54 +0000 (Sun, 06 Jun 2010)
New Revision: 22475

Modified:
   arm/trunk/TODO
   arm/trunk/interface/headerPanel.py
   arm/trunk/util/torTools.py
Log:
added: notice in the header panel when tor's disconnected



Modified: arm/trunk/TODO
===================================================================
--- arm/trunk/TODO	2010-06-05 21:34:41 UTC (rev 22474)
+++ arm/trunk/TODO	2010-06-06 00:31:54 UTC (rev 22475)
@@ -9,8 +9,6 @@
       progress - /init and /util are done and /interface is in progress. Known
       bugs are being fixed while refactoring.
         * <done> header panel
-          - check if tor's been shut down or merely control port disconnect
-              (overwrite flags with tor status)?
         * graph panel
           - revise effective bandwidth
               Take into account 'MaxAdvertisedBandwidth', as per:

Modified: arm/trunk/interface/headerPanel.py
===================================================================
--- arm/trunk/interface/headerPanel.py	2010-06-05 21:34:41 UTC (rev 22474)
+++ arm/trunk/interface/headerPanel.py	2010-06-06 00:31:54 UTC (rev 22475)
@@ -62,6 +62,7 @@
       log.log(log.WARN, "Config: %s is expected to be an integer (defaulting to %i)" % (UPDATE_RATE_CFG, DEFAULT_UPDATE_RATE))
       self._updateRate = DEFAULT_UPDATE_RATE
     
+    self._isTorConnected = True
     self._lastUpdate = -1       # time the content was last revised
     self._isLastDrawWide = False
     self._isChanged = False     # new stats to be drawn if true
@@ -141,7 +142,7 @@
     
     sysFields = ((0, "cpu: %s%%" % self.vals["ps/%cpu"]),
                  (13, "mem: %s (%s%%)" % (memoryLabel, self.vals["ps/%mem"])),
-                 (34, "pid: %s" % (self.vals["ps/pid"] if self.vals["ps/etime"] else "")),
+                 (34, "pid: %s" % (self.vals["ps/pid"] if self._isTorConnected else "")),
                  (47, "uptime: %s" % self.vals["ps/etime"]))
     
     for (start, label) in sysFields:
@@ -153,14 +154,21 @@
     self.addstr(y, x, "fingerprint: %s" % self.vals["tor/fingerprint"])
     
     # Line 5 / Line 3 Left (flags)
-    flagLine = "flags: "
-    for flag in self.vals["tor/flags"]:
-      flagColor = FLAG_COLORS[flag] if flag in FLAG_COLORS.keys() else "white"
-      flagLine += "<b><%s>%s</%s></b>, " % (flagColor, flag, flagColor)
+    if self._isTorConnected:
+      flagLine = "flags: "
+      for flag in self.vals["tor/flags"]:
+        flagColor = FLAG_COLORS[flag] if flag in FLAG_COLORS.keys() else "white"
+        flagLine += "<b><%s>%s</%s></b>, " % (flagColor, flag, flagColor)
+      
+      if len(self.vals["tor/flags"]) > 0: flagLine = flagLine[:-2]
+      else: flagLine += "<b><cyan>none</cyan></b>"
+      
+      self.addfstr(2 if isWide else 4, 0, flagLine)
+    else:
+      statusTime = torTools.getConn().getStatus()[1]
+      statusTimeLabel = time.strftime("%H:%M %m/%d/%Y", time.localtime(statusTime))
+      self.addfstr(2 if isWide else 4, 0, "<b><red>Tor Disconnected</red></b> (%s)" % statusTimeLabel)
     
-    if len(self.vals["tor/flags"]) > 0: flagLine = flagLine[:-2]
-    self.addfstr(2 if isWide else 4, 0, flagLine)
-    
     # Undisplayed / Line 3 Right (exit policy)
     if isWide:
       exitPolicy = self.vals["tor/exitPolicy"]
@@ -206,7 +214,7 @@
     while not self._halt:
       timeSinceReset = time.time() - self._lastUpdate
       
-      if self._isPaused or timeSinceReset < self._updateRate:
+      if self._isPaused or timeSinceReset < self._updateRate or not self._isTorConnected:
         sleepTime = max(0.5, self._updateRate - timeSinceReset)
         self._cond.acquire()
         if not self._halt: self._cond.wait(sleepTime)
@@ -234,8 +242,14 @@
       eventType - type of event detected
     """
     
-    if eventType == torTools.TOR_RESET:
+    if eventType == torTools.TOR_INIT:
+      self._isTorConnected = True
       self._update(True)
+      self.redraw()
+    elif eventType == torTools.TOR_CLOSED:
+      self._isTorConnected = False
+      self._update()
+      self.redraw(True)
   
   def _update(self, setStatic=False):
     """

Modified: arm/trunk/util/torTools.py
===================================================================
--- arm/trunk/util/torTools.py	2010-06-05 21:34:41 UTC (rev 22474)
+++ arm/trunk/util/torTools.py	2010-06-06 00:31:54 UTC (rev 22475)
@@ -22,10 +22,9 @@
 import sysTools
 
 # enums for tor's controller state:
-# TOR_INIT - attached to a new controller
-# TOR_RESET - restart or sighup signal received by tor (resetting internal state)
+# TOR_INIT - attached to a new controller or restart/sighup signal received
 # TOR_CLOSED - control port closed
-TOR_INIT, TOR_RESET, TOR_CLOSED = range(1, 4)
+TOR_INIT, TOR_CLOSED = range(1, 3)
 
 # Message logged by default when a controller event type can't be set (message
 # has the event type inserted into it). This skips logging entirely if None.
@@ -256,6 +255,8 @@
     self.listeners = []                 # callback functions for tor's state changes
     self.controllerEvents = {}          # mapping of successfully set controller events to their failure level/msg
     self._isReset = False               # internal flag for tracking resets
+    self._status = TOR_CLOSED           # current status of the attached control port
+    self._statusTime = 0                # unix timestamp for the duration of the status
     
     # cached information static for a connection (None if unset, "UNKNOWN" if
     # unable to be determined)
@@ -283,6 +284,9 @@
       
       self.connLock.release()
       
+      self._status = TOR_INIT
+      self._statusTime = time.time()
+      
       # notifies listeners that a new controller is available
       thread.start_new_thread(self._notifyStatusListeners, (TOR_INIT,))
   
@@ -298,6 +302,9 @@
       self.pid = None
       self.connLock.release()
       
+      self._status = TOR_CLOSED
+      self._statusTime = time.time()
+      
       # notifies listeners that the controller's been shut down
       thread.start_new_thread(self._notifyStatusListeners, (TOR_CLOSED,))
     else: self.connLock.release()
@@ -409,6 +416,15 @@
     
     return result
   
+  def getStatus(self):
+    """
+    Provides a tuple consisting of the control port's current status and unix
+    timestamp for when it became this way (zero if no status has yet to be
+    set).
+    """
+    
+    return (self._status, self._statusTime)
+  
   def addStatusListener(self, callback):
     """
     Directs further events related to tor's controller status to the callback
@@ -596,7 +612,11 @@
     
     if event.level == "NOTICE" and event.msg.startswith("Received reload signal (hup)"):
       self._isReset = True
-      thread.start_new_thread(self._notifyStatusListeners, (TOR_RESET,))
+      
+      self._status = TOR_INIT
+      self._statusTime = time.time()
+      
+      thread.start_new_thread(self._notifyStatusListeners, (TOR_INIT,))
   
   def _notifyStatusListeners(self, eventType):
     """