[shpy-commit] r2821 - in shpy/trunk/dist/shpy/net: . test

hpk@codespeak.net hpk@codespeak.net
Sun, 18 Jan 2004 15:29:23 +0100 (MET)


Author: hpk
Date: Sun Jan 18 15:29:22 2004
New Revision: 2821

Added:
   shpy/trunk/dist/shpy/net/gateway.py
      - copied, changed from r2818, shpy/trunk/dist/shpy/net/dispatch.py
Removed:
   shpy/trunk/dist/shpy/net/dispatch.py
Modified:
   shpy/trunk/dist/shpy/net/README.txt
   shpy/trunk/dist/shpy/net/register.py
   shpy/trunk/dist/shpy/net/test/test_basic.py
Log:
- renamed SocketConnection -> SocketGateway

- improved README.txt 



Modified: shpy/trunk/dist/shpy/net/README.txt
==============================================================================
--- shpy/trunk/dist/shpy/net/README.txt	(original)
+++ shpy/trunk/dist/shpy/net/README.txt	Sun Jan 18 15:29:22 2004
@@ -2,17 +2,22 @@
 -------------------------------------------
 
 - bootstrap: client initializes server loop
-  on the server (which needs to run a 'startserver.py'
-  process).  Note that both client and server run 
-  the same dispatch.SocketConnection thread, 
+  on the server. Note that both client and server 
+  run the same gateway.SocketGateway thread, 
   accepting incoming source code and offering
-  a remote invocation API. 
+  a remote invocation API. Thus the two hosts
+  are symetrically gatewaying calls to each other. 
+
+  a client registers with a server by invoking
+  shpy.net.register.register('server:port'). The server
+  is expected to be a 'startserver.py' i.e. a
+  server which unpickles strings and executes them. 
 
 - in order to remotely execute source code you call
 
     exec_remote(source) 
 
-  on your connection. this will asynchronously execute 
+  on your gateway. this will asynchronously execute 
   the given source code on the other side. Note that there 
   there is no return value, just as with the usual 
   python 'exec' statement. exec_remote returns 

Deleted: /shpy/trunk/dist/shpy/net/dispatch.py
==============================================================================
--- /shpy/trunk/dist/shpy/net/dispatch.py	Sun Jan 18 15:29:22 2004
+++ (empty file)
@@ -1,67 +0,0 @@
-import threading, pickle
-
-from shpy.net import common 
-
-class SocketConnection(threading.Thread):
-    def __init__(self, sock):
-        import threading # XXX well 
-        threading.Thread.__init__(self)
-        self.sock = sock 
-        self.lock = threading.RLock()
-        self.running = 1
-        self.setDaemon(1)
-        self.start()
-
-    def run(self):
-        # here we want to select for incoming calls. 
-        # but only if we have the lock.  
-        import select
-        while self.running:
-            self.lock.acquire()
-            try:
-                readlist = [self.sock]
-                r,w,e = select.select(readlist, [], [], 0.1)
-                if self.sock in r:
-                    self.exec_incoming()
-            finally:
-                self.lock.release()
-
-    def exec_incoming(self):
-        # we have the lock here already!
-        print "executing incoming data. "
-        sockfile = self.sock.makefile('r+b')
-        pickler = pickle.Pickler(sockfile)
-        unpickler = pickle.Unpickler(sockfile)
-
-        source, funcname, args, kwargs = unpickler.load()
-        print "received source", source
-        print "will call %s(*%s, **%s)" % (funcname, args, kwargs)
-
-        try:
-            ns = {}
-            exec source in ns 
-            func = ns[funcname] 
-            res = func(*args, **kwargs)
-        except:
-            import sys
-            res = common.ExceptionInfo(sys.exc_info())
-        
-        pickler.dump(res)
-
-    def exec_remote(self, func, *args, **kwargs):
-        import inspect
-        self.lock.acquire()
-        try:
-            funcsource = inspect.getsource(func)
-            sockfile = self.sock.makefile('r+b')
-            pickler = pickle.Pickler(sockfile)
-            unpickler = pickle.Unpickler(sockfile)
-
-            print "sending %s(*%s, **%s)" %(func.func_name,
-                                            args, kwargs)
-            pickler.dump((funcsource, func.func_name, args, kwargs))
-            res = unpickler.load()
-            print "received result", res
-            return res
-        finally:
-            self.lock.release()

Copied: shpy/trunk/dist/shpy/net/gateway.py (from r2818, shpy/trunk/dist/shpy/net/dispatch.py)
==============================================================================
--- shpy/trunk/dist/shpy/net/dispatch.py	(original)
+++ shpy/trunk/dist/shpy/net/gateway.py	Sun Jan 18 15:29:22 2004
@@ -2,7 +2,7 @@
 
 from shpy.net import common 
 
-class SocketConnection(threading.Thread):
+class SocketGateway(threading.Thread):
     def __init__(self, sock):
         import threading # XXX well 
         threading.Thread.__init__(self)

Modified: shpy/trunk/dist/shpy/net/register.py
==============================================================================
--- shpy/trunk/dist/shpy/net/register.py	(original)
+++ shpy/trunk/dist/shpy/net/register.py	Sun Jan 18 15:29:22 2004
@@ -7,10 +7,10 @@
         hostport = (host, int(port))
 
     import inspect, socket, pickle 
-    from shpy.net import dispatch 
+    from shpy.net import gateway 
 
-    source = [inspect.getsource(dispatch)]
-    source.append('thread = SocketConnection(clientsock)')
+    source = [inspect.getsource(gateway)]
+    source.append('thread = SocketGateway(clientsock)')
     source.append('pickler.dump("ok")')
     source = "\n".join(source)
 
@@ -21,5 +21,7 @@
     p.dump(source)
     u = pickle.Unpickler(sockfile)
     res = u.load()
-    assert res=='ok'
-    return dispatch.SocketConnection(sock)
+    assert res=='ok', ("could not establish server gateway %r" %
+                       hostport)
+
+    return gateway.SocketGateway(sock)

Modified: shpy/trunk/dist/shpy/net/test/test_basic.py
==============================================================================
--- shpy/trunk/dist/shpy/net/test/test_basic.py	(original)
+++ shpy/trunk/dist/shpy/net/test/test_basic.py	Sun Jan 18 15:29:22 2004
@@ -9,7 +9,7 @@
 
 hostport = 'localhost:8887'
 
-def getserver(started=[]):
+def getservergateway(started=[]):
     if started:
         return started[0]
     try:
@@ -22,18 +22,18 @@
     proc = popen2.Popen4('python %s %s' % (pyfile, hostport))
     atexit.register(os.kill, proc.pid, 15)
     time.sleep(1.0)
-    conn = register(hostport)
-    atexit.register(setattr, conn, 'running', 0)
-    started.append(conn)
+    gw = register(hostport)
+    atexit.register(setattr, gw, 'running', 0)
+    started.append(gw)
 
 def test_server_initialization():
-    conn = getserver()
-    res = conn.exec_remote(functest.f)
+    gw = getservergateway()
+    res = gw.exec_remote(functest.f)
     check.equal(res, 42)
 
 def test_callback():
-    conn = getserver()
-    #conn.exec_remote(functest.callback)
+    gw = getservergateway()
+    #gw.exec_remote(functest.callback)
 
 if __name__=='__main__':
     main()