[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Adding get_latest_heartbeat()
commit 6bc92816dc4356a204fdde0f160ee344875143a3
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sun Dec 16 18:50:46 2012 -0800
Adding get_latest_heartbeat()
Adding a method to the BaseController to get the time that we last heard from
tor. This is useful to figure out if our controller connection has stalled, but
only works if we're continually using our controller (such as by listening for
BW events).
---
stem/control.py | 14 ++++++++++++++
test/integ/control/base_controller.py | 11 +++++++++++
2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/stem/control.py b/stem/control.py
index a1100c1..1e5250a 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -49,6 +49,7 @@ providing its own for interacting at a higher level.
|- connect - connects or reconnects to tor
|- close - shuts down our connection to the tor process
|- get_socket - provides the socket used for control communication
+ |- get_latest_heartbeat - timestamp for when we last heard from tor
|- add_status_listener - notifies a callback of changes in our status
|- remove_status_listener - prevents further notification of status changes
+- __enter__ / __exit__ - manages socket connection
@@ -237,6 +238,8 @@ class BaseController(object):
self._socket._connect = self._connect
self._socket._close = self._close
+ self._last_heartbeat = 0.0 # timestamp for when we last heard from tor
+
if self._socket.is_alive():
self._launch_threads()
@@ -357,6 +360,16 @@ class BaseController(object):
return self._socket
+ def get_latest_heartbeat(self):
+ """
+ Provides the unix timestamp for when we last heard from tor. This is zero
+ if we've never received a message.
+
+ :returns: float for the unix timestamp of when we last heard from tor
+ """
+
+ return self._last_heartbeat
+
def add_status_listener(self, callback, spawn = True):
"""
Notifies a given function when the state of our socket changes. Functions
@@ -523,6 +536,7 @@ class BaseController(object):
while self.is_alive():
try:
control_message = self._socket.recv()
+ self._last_heartbeat = time.time()
if control_message.content()[-1][0] == "650":
# asynchronous message, adds to the event queue and wakes up its handler
diff --git a/test/integ/control/base_controller.py b/test/integ/control/base_controller.py
index b4bb354..8709469 100644
--- a/test/integ/control/base_controller.py
+++ b/test/integ/control/base_controller.py
@@ -183,6 +183,17 @@ class TestBaseController(unittest.TestCase):
self.assertTrue(re.match("650 BW [0-9]+ [0-9]+\r\n", bw_event.raw_content()))
self.assertEquals(("650", " "), bw_event.content()[0][:2])
+ def test_get_latest_heartbeat(self):
+ """
+ Basic check for get_latest_heartbeat().
+ """
+
+ # makes a getinfo query, then checks that the heartbeat is close to now
+ with test.runner.get_runner().get_tor_socket() as control_socket:
+ controller = stem.control.BaseController(control_socket)
+ controller.msg("GETINFO version")
+ self.assertTrue((time.time() - controller.get_latest_heartbeat()) < 5)
+
def test_status_notifications(self):
"""
Checks basic functionality of the add_status_listener() and
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits