From bc3c0241136a6f107e3dbe71ae5906727c6ea5e0 Mon Sep 17 00:00:00 2001 From: ced <> Date: Sat, 27 Jan 2007 18:04:04 +0000 Subject: [PATCH] KERNEL & CLIENT: add socket to client bzr revid: ced-03b3c4b66589c71ef47e688002e648c7887b94ae --- bin/netsvc.py | 21 +++++++++++++-------- bin/tiny_socket.py | 9 ++++++++- bin/tinyerp-server.py | 1 + 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bin/netsvc.py b/bin/netsvc.py index bd946e0fc39..f49034709dc 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -276,20 +276,20 @@ class HttpDaemon(object): import tiny_socket class TinySocketClientThread(threading.Thread): - def __init__(self, sock): + def __init__(self, sock, threads): threading.Thread.__init__(self) self.sock = sock + self.threads = threads def run(self): import traceback import time + import select try: self.running = True ts = tiny_socket.mysocket(self.sock) while self.running: - c = time.time() msg = ts.myreceive() - print time.time() - c try: s=LocalService(msg[0]) @@ -300,6 +300,7 @@ class TinySocketClientThread(threading.Thread): if res!=None: r=res result = r + ts.mysend(result) except Exception, e: print "Exception in call:" print '-'*60 @@ -311,8 +312,10 @@ class TinySocketClientThread(threading.Thread): import pdb tb = sys.exc_info()[2] pdb.post_mortem(tb) - ts.mysend(s, exception=True) - ts.mysend(result) + ts.mysend(e, exception=True) + self.sock.shutdown(socket.SHUT_RDWR) + self.threads.remove(self) + return True except Exception, e: print "exception", e self.sock.close() @@ -332,6 +335,7 @@ class TinySocketServerThread(threading.Thread): self.threads = [] def run(self): + import select try: self.running = True while self.running: @@ -339,18 +343,19 @@ class TinySocketServerThread(threading.Thread): (clientsocket, address) = self.socket.accept() #now do something with the clientsocket #in this case, we'll pretend this is a threaded server - ct = TinySocketClientThread(clientsocket) + ct = TinySocketClientThread(clientsocket, self.threads) ct.start() self.threads.append(ct) # print "threads size:", len(self.threads) except Exception, e: + print "exception", e return False def stop(self): self.running=False for t in self.threads: - if t: - t.stop() + print "threads" + t.join() self.socket.shutdown(socket.SHUT_RDWR) # vim:noexpandtab: diff --git a/bin/tiny_socket.py b/bin/tiny_socket.py index e7a9624c7fd..52690d09c1b 100644 --- a/bin/tiny_socket.py +++ b/bin/tiny_socket.py @@ -12,6 +12,9 @@ class mysocket: self.sock.settimeout(60) def connect(self, host, port): self.sock.connect((host, port)) + def disconnect(self): + self.sock.shutdown(socket.SHUT_RDWR) + self.sock.close() def mysend(self, msg, exception=False): msg = cPickle.dumps(msg) size = len(msg) @@ -36,4 +39,8 @@ class mysocket: if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk - return cPickle.loads(msg) + res = cPickle.loads(msg) + if isinstance(res,Exception): + raise res + else: + return res diff --git a/bin/tinyerp-server.py b/bin/tinyerp-server.py index 70518098b5d..ffd8e659e4c 100755 --- a/bin/tinyerp-server.py +++ b/bin/tinyerp-server.py @@ -233,6 +233,7 @@ logger.notifyChannel("web-services", netsvc.LOG_INFO, "starting TinySocket servi def handler(signum, frame): from tools import config tinySocket.stop() + tinySocket.join() httpd.stop() netsvc.Agent.quit() if config['pidfile']: