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

[tor-commits] [stem/master] Splitting socket attribute from protocolinfo



commit 04975ff9d149d8a881edd8ce26925d785150eda1
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Nov 28 06:56:38 2011 -0800

    Splitting socket attribute from protocolinfo
    
    Bundling the requesting socket with the protocolinfo response was kinda clunky.
    I thought that it owuld make the api a little nicer, but in retrospect it's
    just weird so going to a more conventional tuple response instead.
---
 stem/connection.py                    |   41 +++++++++++++++++++-------------
 test/integ/connection/protocolinfo.py |    3 --
 test/unit/connection/protocolinfo.py  |    2 -
 3 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index fe17d6b..b740d0d 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -38,7 +38,7 @@ LOGGER = logging.getLogger("stem")
 
 AuthMethod = stem.util.enum.Enum("NONE", "PASSWORD", "COOKIE", "UNKNOWN")
 
-def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, keep_alive = False):
+def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, get_socket = False):
   """
   Issues a PROTOCOLINFO query to a control port, getting information about the
   tor process running on it.
@@ -46,11 +46,13 @@ def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, ke
   Arguments:
     control_addr (str) - ip address of the controller
     control_port (int) - port number of the controller
-    keep_alive (bool)  - keeps the socket used to issue the PROTOCOLINFO query
-                         open if True, closes otherwise
+    get_socket (bool)  - provides the socket with the response if True,
+                         otherwise the socket is closed when we're done
   
   Returns:
-    ProtocolInfoResponse with the response given by the tor process
+    stem.connection.ProtocolInfoResponse provided by tor, if get_socket is True
+    then this provides a tuple instead with both the response and connected
+    socket (stem.socket.ControlPort)
   
   Raises:
     stem.socket.ProtocolError if the PROTOCOLINFO response is malformed
@@ -65,27 +67,33 @@ def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, ke
     protocolinfo_response = control_socket.recv()
     ProtocolInfoResponse.convert(protocolinfo_response)
     
-    if keep_alive: protocolinfo_response.socket = control_socket
-    else: control_socket.close()
-    
     # attempt to expand relative cookie paths using our port to infer the pid
     if control_addr == "127.0.0.1":
       _expand_cookie_path(protocolinfo_response, stem.util.system.get_pid_by_port, control_port)
     
-    return protocolinfo_response
+    if get_socket:
+      return (protocolinfo_response, control_socket)
+    else:
+      control_socket.close()
+      return protocolinfo_response
   except stem.socket.ControllerError, exc:
     control_socket.close()
     raise exc
 
-def get_protocolinfo_by_socket(socket_path = "/var/run/tor/control", keep_alive = False):
+def get_protocolinfo_by_socket(socket_path = "/var/run/tor/control", get_socket = False):
   """
   Issues a PROTOCOLINFO query to a control socket, getting information about
   the tor process running on it.
   
   Arguments:
     socket_path (str) - path where the control socket is located
-    keep_alive (bool) - keeps the socket used to issue the PROTOCOLINFO query
-                        open if True, closes otherwise
+    get_socket (bool) - provides the socket with the response if True,
+                        otherwise the socket is closed when we're done
+  
+  Returns:
+    stem.connection.ProtocolInfoResponse provided by tor, if get_socket is True
+    then this provides a tuple instead with both the response and connected
+    socket (stem.socket.ControlSocketFile)
   
   Raises:
     stem.socket.ProtocolError if the PROTOCOLINFO response is malformed
@@ -100,13 +108,14 @@ def get_protocolinfo_by_socket(socket_path = "/var/run/tor/control", keep_alive
     protocolinfo_response = control_socket.recv()
     ProtocolInfoResponse.convert(protocolinfo_response)
     
-    if keep_alive: protocolinfo_response.socket = control_socket
-    else: control_socket.close()
-    
     # attempt to expand relative cookie paths using our port to infer the pid
     _expand_cookie_path(protocolinfo_response, stem.util.system.get_pid_by_open_file, socket_path)
     
-    return protocolinfo_response
+    if get_socket:
+      return (protocolinfo_response, control_socket)
+    else:
+      control_socket.close()
+      return protocolinfo_response
   except stem.socket.ControllerError, exc:
     control_socket.close()
     raise exc
@@ -160,7 +169,6 @@ class ProtocolInfoResponse(stem.socket.ControlMessage):
     auth_methods (tuple)               - AuthMethod types that tor will accept
     unknown_auth_methods (tuple)       - strings of unrecognized auth methods
     cookie_path (str)                  - path of tor's authentication cookie
-    socket (stem.socket.ControlSocket) - socket used to make the query
   """
   
   def convert(control_message):
@@ -196,7 +204,6 @@ class ProtocolInfoResponse(stem.socket.ControlMessage):
     self.protocol_version = None
     self.tor_version = None
     self.cookie_path = None
-    self.socket = None
     
     auth_methods, unknown_auth_methods = [], []
     
diff --git a/test/integ/connection/protocolinfo.py b/test/integ/connection/protocolinfo.py
index 8c23329..e1436f8 100644
--- a/test/integ/connection/protocolinfo.py
+++ b/test/integ/connection/protocolinfo.py
@@ -46,7 +46,6 @@ class TestProtocolInfo(unittest.TestCase):
     self.assertNotEqual(None, protocolinfo_response.tor_version)
     self.assertNotEqual(None, protocolinfo_response.auth_methods)
     
-    self.assertEqual(None, protocolinfo_response.socket)
     self.assert_protocolinfo_attr(protocolinfo_response, connection_type)
   
   def test_get_protocolinfo_by_port(self):
@@ -77,7 +76,6 @@ class TestProtocolInfo(unittest.TestCase):
     
     if test.runner.OPT_PORT in test.runner.CONNECTION_OPTS[connection_type]:
       protocolinfo_response = stem.connection.get_protocolinfo_by_port(control_port = test.runner.CONTROL_PORT)
-      self.assertEqual(None, protocolinfo_response.socket)
       self.assert_protocolinfo_attr(protocolinfo_response, connection_type)
     else:
       # we don't have a control port
@@ -104,7 +102,6 @@ class TestProtocolInfo(unittest.TestCase):
     
     if test.runner.OPT_SOCKET in test.runner.CONNECTION_OPTS[connection_type]:
       protocolinfo_response = stem.connection.get_protocolinfo_by_socket(socket_path = test.runner.CONTROL_SOCKET_PATH)
-      self.assertEqual(None, protocolinfo_response.socket)
       self.assert_protocolinfo_attr(protocolinfo_response, connection_type)
     else:
       # we don't have a control socket
diff --git a/test/unit/connection/protocolinfo.py b/test/unit/connection/protocolinfo.py
index ecca8b6..bc0fef3 100644
--- a/test/unit/connection/protocolinfo.py
+++ b/test/unit/connection/protocolinfo.py
@@ -92,7 +92,6 @@ class TestProtocolInfoResponse(unittest.TestCase):
     self.assertEquals((stem.connection.AuthMethod.NONE, ), control_message.auth_methods)
     self.assertEquals((), control_message.unknown_auth_methods)
     self.assertEquals(None, control_message.cookie_path)
-    self.assertEquals(None, control_message.socket)
   
   def test_password_auth(self):
     """
@@ -148,7 +147,6 @@ class TestProtocolInfoResponse(unittest.TestCase):
     self.assertEquals((), control_message.auth_methods)
     self.assertEquals((), control_message.unknown_auth_methods)
     self.assertEquals(None, control_message.cookie_path)
-    self.assertEquals(None, control_message.socket)
   
   def test_relative_cookie(self):
     """



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits