[tor-commits] [stem/master] Adding support for CIRC_BW events

atagar at torproject.org atagar at torproject.org
Mon Nov 4 07:29:15 UTC 2013


commit eb808565f5e2ea8885eabba682e6ff97873be056
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Nov 3 13:05:12 2013 -0800

    Adding support for CIRC_BW events
    
    Pretty much identical to their CONN_BW counterpart (minus a connection type).
    Also added in...
    
    https://gitweb.torproject.org/torspec.git/commitdiff/6f2919a
---
 docs/change_log.rst          |    1 +
 stem/response/events.py      |   39 +++++++++++++++++++++++++++++++++++++++
 stem/version.py              |    2 ++
 test/unit/response/events.py |   16 ++++++++++++++++
 4 files changed, 58 insertions(+)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index a55eb95..f066b9b 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -43,6 +43,7 @@ The following are only available within stem's `git repository
 
   * Added the id attribute to the :class:`~stem.response.events.ORConnEvent` (:spec:`6f2919a`)
   * Added `support for CONN_BW events <api/response.html#stem.response.events.ConnectionBandwidthEvent>`_ (:spec:`6f2919a`)
+  * Added `support for CIRC_BW events <api/response.html#stem.response.events.CircuitBandwidthEvent>`_ (:spec:`6f2919a`)
 
 .. _version_1.1:
 
diff --git a/stem/response/events.py b/stem/response/events.py
index dcdefbc..b892649 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -970,6 +970,44 @@ class ConnectionBandwidthEvent(Event):
 
     self._log_if_unrecognized('type', stem.ConnectionType)
 
+
+class CircuitBandwidthEvent(Event):
+  """
+  Event emitted every second with the bytes sent and received by tor on a
+  per-circuit basis.
+
+  The CIRC_BW event was introduced in tor version 0.2.5.2-alpha.
+
+  .. versionadded:: 1.1.0-dev
+
+  :var str id: circuit identifier
+  :var long read: bytes received by tor that second
+  :var long written: bytes sent by tor that second
+  """
+
+  _KEYWORD_ARGS = {
+    "ID": "id",
+    "READ": "read",
+    "WRITTEN": "written",
+  }
+
+  _VERSION_ADDED = stem.version.Requirement.EVENT_CIRC_BW
+
+  def _parse(self):
+    if not self.id:
+      raise stem.ProtocolError("CIRC_BW event is missing its id")
+    elif not self.read:
+      raise stem.ProtocolError("CIRC_BW event is missing its read value")
+    elif not self.written:
+      raise stem.ProtocolError("CIRC_BW event is missing its written value")
+    elif not self.read.isdigit() or not self.written.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's bytes sent and received should be a positive numeric value, received: %s" % self)
+    elif not tor_tools.is_valid_circuit_id(self.id):
+      raise stem.ProtocolError("Circuit IDs must be one to sixteen alphanumeric characters, got '%s': %s" % (self.id, self))
+
+    self.read = long(self.read)
+    self.written = long(self.written)
+
 EVENT_TYPE_TO_CLASS = {
   "ADDRMAP": AddrMapEvent,
   "AUTHDIR_NEWDESCS": AuthDirNewDescEvent,
@@ -997,6 +1035,7 @@ EVENT_TYPE_TO_CLASS = {
   "STREAM_BW": StreamBwEvent,
   "TRANSPORT_LAUNCHED": TransportLaunchedEvent,
   "CONN_BW": ConnectionBandwidthEvent,
+  "CIRC_BW": CircuitBandwidthEvent,
   "WARN": LogEvent,
 
   # accounting for a bug in tor 0.2.0.22
diff --git a/stem/version.py b/stem/version.py
index fbc3f31..b725ade 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -44,6 +44,7 @@ easily parsed and compared, for instance...
   **EVENT_STREAM_BW**                   STREAM_BW events
   **EVENT_TRANSPORT_LAUNCHED**          TRANSPORT_LAUNCHED events
   **EVENT_CONN_BW**                     CONN_BW events
+  **EVENT_CIRC_BW**                     CIRC_BW events
   **EXTENDCIRCUIT_PATH_OPTIONAL**       EXTENDCIRCUIT queries can omit the path if the circuit is zero
   **FEATURE_EXTENDED_EVENTS**           'EXTENDED_EVENTS' optional feature
   **FEATURE_VERBOSE_NAMES**             'VERBOSE_NAMES' optional feature
@@ -343,6 +344,7 @@ Requirement = stem.util.enum.Enum(
   ("EVENT_STREAM_BW", Version('0.1.2.8-beta')),
   ("EVENT_TRANSPORT_LAUNCHED", Version('0.2.5.0-alpha')),
   ("EVENT_CONN_BW", Version('0.2.5.2-alpha')),
+  ("EVENT_CIRC_BW", Version('0.2.5.2-alpha')),
   ("EXTENDCIRCUIT_PATH_OPTIONAL", Version("0.2.2.9")),
   ("FEATURE_EXTENDED_EVENTS", Version("0.2.2.1-alpha")),
   ("FEATURE_VERBOSE_NAMES", Version("0.2.2.1-alpha")),
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 071f760..6b7b6f1 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -333,6 +333,10 @@ CONN_BW = "650 CONN_BW ID=11 TYPE=DIR READ=272 WRITTEN=817"
 CONN_BW_BAD_WRITTEN_VALUE = "650 CONN_BW ID=11 TYPE=DIR READ=272 WRITTEN=817.7"
 CONN_BW_BAD_MISSING_ID = "650 CONN_BW TYPE=DIR READ=272 WRITTEN=817"
 
+CIRC_BW = "650 CIRC_BW ID=11 READ=272 WRITTEN=817"
+CIRC_BW_BAD_WRITTEN_VALUE = "650 CIRC_BW ID=11 READ=272 WRITTEN=817.7"
+CIRC_BW_BAD_MISSING_ID = "650 CIRC_BW READ=272 WRITTEN=817"
+
 
 def _get_event(content):
   controller_event = mocking.get_message(content)
@@ -1207,6 +1211,18 @@ class TestEvents(unittest.TestCase):
     self.assertRaises(ProtocolError, _get_event, CONN_BW_BAD_WRITTEN_VALUE)
     self.assertRaises(ProtocolError, _get_event, CONN_BW_BAD_MISSING_ID)
 
+  def test_circ_bw_event(self):
+    event = _get_event(CIRC_BW)
+
+    self.assertTrue(isinstance(event, stem.response.events.CircuitBandwidthEvent))
+    self.assertEqual(CIRC_BW.lstrip("650 "), str(event))
+    self.assertEqual("11", event.id)
+    self.assertEqual(272, event.read)
+    self.assertEqual(817, event.written)
+
+    self.assertRaises(ProtocolError, _get_event, CIRC_BW_BAD_WRITTEN_VALUE)
+    self.assertRaises(ProtocolError, _get_event, CIRC_BW_BAD_MISSING_ID)
+
   def test_unrecognized_enum_logging(self):
     """
     Checks that when event parsing gets a value that isn't recognized by stem's





More information about the tor-commits mailing list