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

arigo@codespeak.net arigo@codespeak.net
Tue, 20 Jan 2004 16:58:33 +0100 (MET)


Author: arigo
Date: Tue Jan 20 16:58:32 2004
New Revision: 2861

Modified:
   shpy/trunk/dist/shpy/net/gateway.py
   shpy/trunk/dist/shpy/net/structure.py
   shpy/trunk/dist/shpy/ui_pygame.py
Log:
Possibly more robust protocol...


Modified: shpy/trunk/dist/shpy/net/gateway.py
==============================================================================
--- shpy/trunk/dist/shpy/net/gateway.py	(original)
+++ shpy/trunk/dist/shpy/net/gateway.py	Tue Jan 20 16:58:32 2004
@@ -1,6 +1,6 @@
 import autopath
 
-import threading, pickle, Queue, select, StringIO
+import threading, struct, Queue, select, StringIO, socket
 
 from unittest2.tool import dyncode
 from shpy.net import common 
@@ -48,18 +48,24 @@
         self.queue = queue
 
     def run(self):
-        sockfile = self.sock.makefile('rb')
-        unpickler = pickle.Unpickler(sockfile)
-        while 1:
-            try:
-                string = unpickler.load()
+        def recv(bytes, sock=self.sock):
+            "Read exactly 'bytes' bytes from the socket."
+            buf = ""
+            while len(buf) < bytes:
+                t = sock.recv(bytes - len(buf))
+                if not t:
+                    raise EOFError
+                buf += t
+            return buf
+            
+        try:
+            while 1:
+                header = recv(struct.calcsize("!i"))
+                stringlen, = struct.unpack("!i", header)
+                string = recv(stringlen)
                 self.queue.put(string)
-            except EOFError:
-                break
-            except:
-                import traceback
-                traceback.print_exc()
-                self.gateway.exit()
+        except (EOFError, socket.error):
+            pass
 
 
 class QueueExecutor(threading.Thread):
@@ -88,13 +94,14 @@
         self.sock = sock
         self.queue = queue
     def run(self):
-        while 1: 
-            obj = self.queue.get()
-            if obj is None:
-                break
-            f = self.sock.makefile('wb')
-            pickler = pickle.Pickler(f)
-            pickler.dump(obj)
-            f.close()
-            #print 'sent', `obj`
-
+        try:
+            while 1: 
+                obj = self.queue.get()
+                if obj is None:
+                    break
+                data = struct.pack("!i", len(obj)) + obj
+                self.sock.sendall(data)
+                #print 'sent', `obj`
+        finally:
+            print 'SockQueueSender is leaving'
+            self.gateway.running = 0

Modified: shpy/trunk/dist/shpy/net/structure.py
==============================================================================
--- shpy/trunk/dist/shpy/net/structure.py	(original)
+++ shpy/trunk/dist/shpy/net/structure.py	Tue Jan 20 16:58:32 2004
@@ -46,7 +46,7 @@
         maplock.release()
 
 def getstructure(structid):
-    print "hello, i'm getstructure changing the dict id", id(id2structure)
+    print "getstructure(%r)" % structid
     maplock.acquire()
     try:
         try:

Modified: shpy/trunk/dist/shpy/ui_pygame.py
==============================================================================
--- shpy/trunk/dist/shpy/ui_pygame.py	(original)
+++ shpy/trunk/dist/shpy/ui_pygame.py	Tue Jan 20 16:58:32 2004
@@ -200,14 +200,17 @@
         lines.append('''if 1:
             for c in shared.getclientlist():
                 if c is not gateway:
-                    rerepresent = c.ns['representstructure']
-                    relines = []
-                    for structid in %r:
-                        obj = getstructure(structid)
-                        relines.append(rerepresent(obj))
-                    relines.append("terminal.postrepaintevent()")
-                    c.exec_remote('\\n'.join(relines))
-
+                    try:
+                        rerepresent = c.ns['representstructure']
+                        relines = []
+                        for structid in %r:
+                            obj = getstructure(structid)
+                            relines.append(rerepresent(obj))
+                        relines.append("terminal.postrepaintevent()")
+                        c.exec_remote('\\n'.join(relines))
+                    except:
+                        import traceback
+                        traceback.print_exc()
         ''' % [getstructureid(struct) for struct in structures])
         self.servergateway.exec_remote('\n'.join(lines))