pull in additional changes from 2.0 branch.
[monitor.git] / monitor / Rpyc / Factories.py
diff --git a/monitor/Rpyc/Factories.py b/monitor/Rpyc/Factories.py
new file mode 100644 (file)
index 0000000..9de3b69
--- /dev/null
@@ -0,0 +1,57 @@
+"""\r
+the factory: \r
+exposes a nice and easy interface to the internals of rpyc. \r
+this module, along with Utils, are the only modules most clients will need.\r
+"""\r
+\r
+from Stream import SocketStream, PipeStream\r
+from Channel import Channel\r
+from Connection import Connection\r
+from AsyncNetProxy import AsyncNetProxy\r
+from weakref import WeakValueDictionary\r
+from Lib import DEFAULT_PORT\r
+\r
+\r
+__all__ = ["SocketConnection", "AuthSocketConnection", "PipeConnection", "Async"]\r
+_async_proxy_cache = WeakValueDictionary()\r
+\r
+class LoginError(Exception):\r
+    pass\r
+\r
+def SocketConnection(host, port = DEFAULT_PORT, **kw):\r
+    """shorthand for creating a conneciton over a socket to a server"""\r
+    return Connection(Channel(SocketStream.from_new_socket(host, port, **kw)))\r
+\r
+def _create_auth_connection(chan, username, password):\r
+    from Authentication import login\r
+    if not login(chan, username, password):\r
+        raise LoginError("the server did not accept the login")\r
+    return Connection(chan)\r
+    \r
+def AuthSocketConnection(host, username, password, port = DEFAULT_PORT, **kw):\r
+    """shorthand for creating a conneciton over a socket to a server, with authentication"""\r
+    chan = Channel(SocketStream.from_new_socket(host, port, **kw))\r
+    return _create_auth_connection(chan, username, password)\r
+\r
+def PipeConnection(incoming, outgoing):\r
+    """shorthand for creating a conneciton over a pipe"""\r
+    return Connection(Channel(PipeStream(incoming, outgoing)))\r
+\r
+def AuthPipeConnection(incoming, outgoing, username, password):\r
+    """shorthand for creating a conneciton over a pipe"""\r
+    chan = Channel(PipeStream(incoming, outgoing))\r
+    return _create_auth_connection(chan, username, password)\r
+\r
+def Async(proxy):\r
+    """a factory for creating asynchronous proxies for existing synchronous ones"""\r
+    key = id(proxy)\r
+    if key in _async_proxy_cache:\r
+        return _async_proxy_cache[key]\r
+    else:\r
+        new_proxy = AsyncNetProxy(proxy)\r
+        _async_proxy_cache[key] = new_proxy\r
+        return new_proxy\r
+\r
+\r
+\r
+\r