KERNEL & CLIENT: add socket to client

bzr revid: ced-03b3c4b66589c71ef47e688002e648c7887b94ae
This commit is contained in:
ced 2007-01-27 18:04:04 +00:00
parent 2160a47330
commit bc3c024113
3 changed files with 22 additions and 9 deletions

View File

@ -276,20 +276,20 @@ class HttpDaemon(object):
import tiny_socket import tiny_socket
class TinySocketClientThread(threading.Thread): class TinySocketClientThread(threading.Thread):
def __init__(self, sock): def __init__(self, sock, threads):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.sock = sock self.sock = sock
self.threads = threads
def run(self): def run(self):
import traceback import traceback
import time import time
import select
try: try:
self.running = True self.running = True
ts = tiny_socket.mysocket(self.sock) ts = tiny_socket.mysocket(self.sock)
while self.running: while self.running:
c = time.time()
msg = ts.myreceive() msg = ts.myreceive()
print time.time() - c
try: try:
s=LocalService(msg[0]) s=LocalService(msg[0])
@ -300,6 +300,7 @@ class TinySocketClientThread(threading.Thread):
if res!=None: if res!=None:
r=res r=res
result = r result = r
ts.mysend(result)
except Exception, e: except Exception, e:
print "Exception in call:" print "Exception in call:"
print '-'*60 print '-'*60
@ -311,8 +312,10 @@ class TinySocketClientThread(threading.Thread):
import pdb import pdb
tb = sys.exc_info()[2] tb = sys.exc_info()[2]
pdb.post_mortem(tb) pdb.post_mortem(tb)
ts.mysend(s, exception=True) ts.mysend(e, exception=True)
ts.mysend(result) self.sock.shutdown(socket.SHUT_RDWR)
self.threads.remove(self)
return True
except Exception, e: except Exception, e:
print "exception", e print "exception", e
self.sock.close() self.sock.close()
@ -332,6 +335,7 @@ class TinySocketServerThread(threading.Thread):
self.threads = [] self.threads = []
def run(self): def run(self):
import select
try: try:
self.running = True self.running = True
while self.running: while self.running:
@ -339,18 +343,19 @@ class TinySocketServerThread(threading.Thread):
(clientsocket, address) = self.socket.accept() (clientsocket, address) = self.socket.accept()
#now do something with the clientsocket #now do something with the clientsocket
#in this case, we'll pretend this is a threaded server #in this case, we'll pretend this is a threaded server
ct = TinySocketClientThread(clientsocket) ct = TinySocketClientThread(clientsocket, self.threads)
ct.start() ct.start()
self.threads.append(ct) self.threads.append(ct)
# print "threads size:", len(self.threads) # print "threads size:", len(self.threads)
except Exception, e: except Exception, e:
print "exception", e
return False return False
def stop(self): def stop(self):
self.running=False self.running=False
for t in self.threads: for t in self.threads:
if t: print "threads"
t.stop() t.join()
self.socket.shutdown(socket.SHUT_RDWR) self.socket.shutdown(socket.SHUT_RDWR)
# vim:noexpandtab: # vim:noexpandtab:

View File

@ -12,6 +12,9 @@ class mysocket:
self.sock.settimeout(60) self.sock.settimeout(60)
def connect(self, host, port): def connect(self, host, port):
self.sock.connect((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): def mysend(self, msg, exception=False):
msg = cPickle.dumps(msg) msg = cPickle.dumps(msg)
size = len(msg) size = len(msg)
@ -36,4 +39,8 @@ class mysocket:
if chunk == '': if chunk == '':
raise RuntimeError, "socket connection broken" raise RuntimeError, "socket connection broken"
msg = msg + chunk msg = msg + chunk
return cPickle.loads(msg) res = cPickle.loads(msg)
if isinstance(res,Exception):
raise res
else:
return res

View File

@ -233,6 +233,7 @@ logger.notifyChannel("web-services", netsvc.LOG_INFO, "starting TinySocket servi
def handler(signum, frame): def handler(signum, frame):
from tools import config from tools import config
tinySocket.stop() tinySocket.stop()
tinySocket.join()
httpd.stop() httpd.stop()
netsvc.Agent.quit() netsvc.Agent.quit()
if config['pidfile']: if config['pidfile']: