[shpy-commit] r2867 - in shpy/trunk/dist/shpy: . net
arigo@codespeak.net
arigo@codespeak.net
Tue, 20 Jan 2004 19:39:47 +0100 (MET)
Author: arigo
Date: Tue Jan 20 19:39:46 2004
New Revision: 2867
Modified:
shpy/trunk/dist/shpy/net/gateway.py
shpy/trunk/dist/shpy/net/register.py
shpy/trunk/dist/shpy/ui_pygame.py
Log:
Some reorganization of the classes in gateway.py.
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 19:39:46 2004
@@ -2,19 +2,46 @@
from unittest2.tool import dyncode
-class SocketGateway:
- def __init__(self, sock, ns = None):
+class SocketIO:
+ def __init__(self, sock):
+ self.sock = sock
+ try:
+ sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
+ sock.setsockopt(socket.SOL_IP, socket.IP_TOS, 0x10) # IPTOS_LOWDELAY
+ except socket.error, e:
+ print "Cannot set socket option:", str(e)
+
+ def read(self, bytes):
+ "Read exactly 'bytes' bytes from the socket."
+ buf = ""
+ while len(buf) < bytes:
+ t = self.sock.recv(bytes - len(buf))
+ print 'recv -->', len(t)
+ if not t:
+ raise EOFError
+ buf += t
+ return buf
+
+ def write(self, data):
+ self.sock.sendall(data)
+
+ def close(self):
+ self.sock.shutdown(2)
+
+
+class Gateway:
+ def __init__(self, io, ns = None):
if ns is None:
ns = {}
ns['gateway'] = self
+ self.io = io
self.ns = ns
- self.sock = sock
self.incoming = Queue.Queue()
self.outgoing = Queue.Queue()
self.threads = [
- SockQueueReader(self.sock, self.incoming),
+ SockQueueReader(self.incoming),
QueueExecutor(self.incoming),
- SockQueueSender(self.sock, self.outgoing)
+ SockQueueSender(self.outgoing)
]
for t in self.threads:
t.gateway = self
@@ -23,8 +50,8 @@
def exit(self):
self.running = 0
- print "closing socket"
- self.sock.shutdown(2)
+ print "closing i/o connexion"
+ self.io.close()
print "putting none to queues"
self.incoming.put(None)
self.outgoing.put(None)
@@ -38,23 +65,14 @@
def exec_remote(self, source):
self.outgoing.put(source)
+
class SockQueueReader(threading.Thread):
- def __init__(self, sock, queue):
+ def __init__(self, queue):
threading.Thread.__init__(self, name="queuereader")
- self.sock = sock
self.queue = queue
def run(self):
- 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
-
+ recv = self.gateway.io.read
try:
while 1:
header = recv(struct.calcsize("!i"))
@@ -84,21 +102,21 @@
l = traceback.format_exception(*sys.exc_info())
errortext = "".join(l)
self.gateway.exec_remote("print %r" % errortext)
-
+
class SockQueueSender(threading.Thread):
- def __init__(self, sock, queue):
+ def __init__(self, queue):
threading.Thread.__init__(self, name="sockqueuesender")
- self.sock = sock
self.queue = queue
def run(self):
+ send = self.gateway.io.write
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`
+ send(data)
+ print 'sent ->', len(obj)
finally:
print 'SockQueueSender is leaving'
self.gateway.running = 0
Modified: shpy/trunk/dist/shpy/net/register.py
==============================================================================
--- shpy/trunk/dist/shpy/net/register.py (original)
+++ shpy/trunk/dist/shpy/net/register.py Tue Jan 20 19:39:46 2004
@@ -4,7 +4,7 @@
from shpy.net import gateway, structure
-class ServerGateway(gateway.SocketGateway):
+class ServerGateway(gateway.Gateway):
def __init__(self, hostport = ':8888', ns = None):
if isinstance(hostport, str):
@@ -12,20 +12,23 @@
hostport = (host, int(port))
source = [inspect.getsource(gateway)]
- source.append('thread = SocketGateway(clientsock)')
+ source.append('thread = Gateway(SocketIO(clientsock))')
source.append('pickler.dump("ok")')
source = "\n".join(source)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(hostport)
- sockfile = sock.makefile('r+b',0)
- p = pickle.Pickler(sockfile)
+ sockfile = sock.makefile('wb',0)
+ p = pickle.Pickler(sockfile, bin=True)
p.dump(source)
+ sockfile.close()
+ sockfile = sock.makefile('rb',0)
u = pickle.Unpickler(sockfile)
res = u.load()
+ sockfile.close()
assert res=='ok', ("could not establish server gateway %r" %
hostport)
- gateway.SocketGateway.__init__(self, sock, ns)
+ gateway.Gateway.__init__(self, gateway.SocketIO(sock), ns)
def import_remote(self, filename, modname, reload=False):
filename = os.path.join(autopath.thisdir, filename)
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 19:39:46 2004
@@ -7,8 +7,8 @@
from shpy.net.structure import Structure, representstructure, getstructureid
from shpy import info
-RESOLUTION = (768, 512)
-#RESOLUTION = (400, 300)
+#RESOLUTION = (768, 512)
+RESOLUTION = (300, 300)
#FONT = 'lucon.ttf'
FONT = None
HEIGHT = 24