2009-05-17 22:15:08 +04:00
|
|
|
# Derived from libmux, available in Plan 9 under /sys/src/libmux
|
|
|
|
# under the following terms:
|
|
|
|
#
|
|
|
|
# Copyright (C) 2003-2006 Russ Cox, Massachusetts Institute of Technology
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
# a copy of this software and associated documentation files (the
|
|
|
|
# "Software"), to deal in the Software without restriction, including
|
|
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
# the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
|
2010-07-10 01:17:39 +04:00
|
|
|
import os
|
2009-05-21 22:38:38 +04:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2009-05-17 22:15:08 +04:00
|
|
|
from pyxp import fields
|
|
|
|
from pyxp.dial import dial
|
|
|
|
from threading import *
|
|
|
|
Condition = Condition().__class__
|
|
|
|
|
|
|
|
__all__ = 'Mux',
|
|
|
|
|
|
|
|
class Mux(object):
|
2009-05-21 22:38:38 +04:00
|
|
|
def __init__(self, con, process, flush=None, mintag=0, maxtag=1<<16 - 1):
|
2009-05-17 22:15:08 +04:00
|
|
|
self.lock = RLock()
|
2010-07-10 01:17:39 +04:00
|
|
|
self.tagcond = Condition(self.lock)
|
2009-05-17 22:15:08 +04:00
|
|
|
self.outlock = RLock()
|
|
|
|
self.inlock = RLock()
|
|
|
|
self.process = process
|
2009-05-21 22:38:38 +04:00
|
|
|
self.flush = flush
|
2009-05-17 22:15:08 +04:00
|
|
|
self.wait = {}
|
|
|
|
self.free = set(range(mintag, maxtag))
|
|
|
|
self.mintag = mintag
|
|
|
|
self.maxtag = maxtag
|
|
|
|
self.muxer = None
|
|
|
|
|
2010-07-02 05:45:27 +04:00
|
|
|
self.async_mux = Queue(self.mux)
|
|
|
|
self.async_dispatch = Queue(self.async_dispatch)
|
|
|
|
|
2009-05-17 22:15:08 +04:00
|
|
|
if isinstance(con, basestring):
|
|
|
|
con = dial(con)
|
|
|
|
self.fd = con
|
|
|
|
|
|
|
|
if self.fd is None:
|
|
|
|
raise Exception("No connection")
|
|
|
|
|
2009-05-21 22:38:38 +04:00
|
|
|
def mux(self, rpc):
|
2010-07-10 01:17:39 +04:00
|
|
|
with self.lock:
|
|
|
|
try:
|
|
|
|
rpc.waiting = True
|
|
|
|
while self.muxer and self.muxer != rpc and rpc.data is None:
|
|
|
|
rpc.wait()
|
|
|
|
|
|
|
|
if rpc.data is None:
|
|
|
|
assert self.muxer in (rpc, None)
|
|
|
|
self.muxer = rpc
|
|
|
|
try:
|
|
|
|
self.lock.release()
|
|
|
|
while rpc.data is None:
|
|
|
|
data = self.recv()
|
|
|
|
if data is None:
|
|
|
|
raise Exception("unexpected eof")
|
|
|
|
self.dispatch(data)
|
|
|
|
finally:
|
|
|
|
self.lock.acquire()
|
|
|
|
self.electmuxer()
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(sys.stderr)
|
|
|
|
if rpc.tag in self.wait:
|
|
|
|
self.wait.pop(rpc.tag)
|
|
|
|
if self.flush:
|
|
|
|
self.flush(self, rpc.data)
|
|
|
|
raise
|
2009-05-21 22:38:38 +04:00
|
|
|
|
2010-07-02 05:45:27 +04:00
|
|
|
return rpc.data
|
2009-05-21 22:38:38 +04:00
|
|
|
|
|
|
|
def rpc(self, dat, async=None):
|
|
|
|
rpc = self.newrpc(dat, async)
|
|
|
|
if async:
|
2010-07-02 05:45:27 +04:00
|
|
|
self.async_mux.push(rpc)
|
2009-05-21 22:38:38 +04:00
|
|
|
else:
|
|
|
|
return self.mux(rpc)
|
2009-05-17 22:15:08 +04:00
|
|
|
|
2010-07-02 05:45:27 +04:00
|
|
|
def async_dispatch(self, rpc):
|
|
|
|
rpc.async(self, rpc.data)
|
|
|
|
|
2009-05-17 22:15:08 +04:00
|
|
|
def electmuxer(self):
|
2010-07-10 01:17:39 +04:00
|
|
|
for rpc in self.wait.itervalues():
|
2010-07-02 05:45:27 +04:00
|
|
|
if self.muxer != rpc and rpc.waiting:
|
|
|
|
self.muxer = rpc
|
|
|
|
rpc.notify()
|
|
|
|
return
|
2009-05-17 22:15:08 +04:00
|
|
|
self.muxer = None
|
|
|
|
|
|
|
|
def dispatch(self, dat):
|
|
|
|
with self.lock:
|
2010-07-10 01:17:39 +04:00
|
|
|
rpc = self.wait.get(dat.tag, None)
|
|
|
|
if rpc:
|
|
|
|
self.puttag(rpc)
|
|
|
|
rpc.dispatch(dat)
|
|
|
|
elif False:
|
|
|
|
print "bad rpc tag: %u (no one waiting on it)" % dat.tag
|
2009-05-17 22:15:08 +04:00
|
|
|
|
|
|
|
def gettag(self, r):
|
|
|
|
tag = 0
|
|
|
|
|
|
|
|
while not self.free:
|
2010-07-10 01:17:39 +04:00
|
|
|
self.tagcond.wait()
|
2009-05-17 22:15:08 +04:00
|
|
|
|
|
|
|
tag = self.free.pop()
|
|
|
|
|
|
|
|
if tag in self.wait:
|
|
|
|
raise Exception("nwait botch")
|
|
|
|
|
|
|
|
self.wait[tag] = r
|
|
|
|
|
|
|
|
r.tag = tag
|
2009-05-21 22:38:38 +04:00
|
|
|
r.orig.tag = r.tag
|
2009-05-17 22:15:08 +04:00
|
|
|
return r.tag
|
|
|
|
|
2009-05-21 22:38:38 +04:00
|
|
|
def puttag(self, rpc):
|
|
|
|
if rpc.tag in self.wait:
|
|
|
|
del self.wait[rpc.tag]
|
|
|
|
self.free.add(rpc.tag)
|
2010-07-10 01:17:39 +04:00
|
|
|
self.tagcond.notify()
|
2009-05-17 22:15:08 +04:00
|
|
|
|
|
|
|
def send(self, dat):
|
|
|
|
data = ''.join(dat.marshall())
|
|
|
|
n = self.fd.send(data)
|
|
|
|
return n == len(data)
|
|
|
|
def recv(self):
|
2010-07-10 01:17:39 +04:00
|
|
|
def readn(fd, n):
|
|
|
|
data = ''
|
|
|
|
while len(data) < n:
|
|
|
|
try:
|
|
|
|
s = fd.recv(n - len(data))
|
|
|
|
if len(s) == 0:
|
|
|
|
raise Exception('unexpected end of file')
|
|
|
|
data += s
|
|
|
|
except os.error, e:
|
|
|
|
if e.errno != os.errno.EINTR:
|
|
|
|
raise e
|
|
|
|
return data
|
|
|
|
|
2009-05-21 22:38:38 +04:00
|
|
|
try:
|
|
|
|
with self.inlock:
|
2010-07-10 01:17:39 +04:00
|
|
|
data = readn(self.fd, 4)
|
2009-05-21 22:38:38 +04:00
|
|
|
if data:
|
2010-07-10 01:17:39 +04:00
|
|
|
nmsg = fields.Int.decoders[4](data, 0)
|
|
|
|
data += readn(self.fd, nmsg - 4)
|
2009-05-21 22:38:38 +04:00
|
|
|
return self.process(data)
|
|
|
|
except Exception, e:
|
2010-07-10 01:17:39 +04:00
|
|
|
print e.__class__.__name__
|
|
|
|
print repr(e)
|
2010-07-02 05:45:27 +04:00
|
|
|
traceback.print_exc(sys.stderr)
|
2009-05-21 22:38:38 +04:00
|
|
|
return None
|
2009-05-17 22:15:08 +04:00
|
|
|
|
2009-05-21 22:38:38 +04:00
|
|
|
def newrpc(self, dat, async=None):
|
|
|
|
rpc = Rpc(self, dat, async)
|
2009-05-17 22:15:08 +04:00
|
|
|
tag = None
|
|
|
|
|
|
|
|
with self.lock:
|
|
|
|
self.gettag(rpc)
|
|
|
|
|
|
|
|
if rpc.tag >= 0 and self.send(dat):
|
|
|
|
return rpc
|
|
|
|
|
|
|
|
with self.lock:
|
|
|
|
self.puttag(rpc)
|
|
|
|
|
|
|
|
class Rpc(Condition):
|
2009-05-21 22:38:38 +04:00
|
|
|
def __init__(self, mux, data, async=None):
|
2009-05-17 22:15:08 +04:00
|
|
|
super(Rpc, self).__init__(mux.lock)
|
|
|
|
self.mux = mux
|
2009-05-21 22:38:38 +04:00
|
|
|
self.orig = data
|
|
|
|
self.data = None
|
|
|
|
self.async = async
|
2010-07-02 05:45:27 +04:00
|
|
|
self.waiting = False
|
2009-05-21 22:38:38 +04:00
|
|
|
|
2010-07-10 01:17:39 +04:00
|
|
|
def __repr__(self):
|
|
|
|
return '<Rpc tag=%s orig=%s data=%s async=%s waiting=%s>' % tuple(map(repr, (self.tag, self.orig, self.data, self.async, self.waiting)))
|
|
|
|
|
2009-05-21 22:38:38 +04:00
|
|
|
def dispatch(self, data=None):
|
2009-05-17 22:15:08 +04:00
|
|
|
self.data = data
|
2010-07-02 05:45:27 +04:00
|
|
|
self.notify()
|
|
|
|
if callable(self.async):
|
|
|
|
self.mux.async_dispatch(self)
|
|
|
|
|
|
|
|
class Queue(Thread):
|
2010-07-10 01:17:39 +04:00
|
|
|
_id = 1
|
|
|
|
|
2010-07-02 05:45:27 +04:00
|
|
|
def __init__(self, op):
|
2010-07-10 01:17:39 +04:00
|
|
|
super(Queue, self).__init__(name='Queue-%d-%s' % (Queue._id, repr(op)))
|
|
|
|
Queue._id += 1
|
2010-07-02 05:45:27 +04:00
|
|
|
self.cond = Condition()
|
|
|
|
self.op = op
|
|
|
|
self.queue = []
|
|
|
|
self.daemon = True
|
|
|
|
|
|
|
|
def __call__(self, item):
|
|
|
|
return self.push(item)
|
|
|
|
|
|
|
|
def push(self, item):
|
|
|
|
with self.cond:
|
|
|
|
self.queue.append(item)
|
|
|
|
if not self.is_alive():
|
|
|
|
self.start()
|
|
|
|
self.cond.notify()
|
|
|
|
def pop(self, item):
|
|
|
|
with self.cond:
|
|
|
|
if item in self.queue:
|
|
|
|
self.queue.remove(item)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.cond.acquire()
|
|
|
|
while True:
|
|
|
|
while self.queue:
|
|
|
|
item = self.queue.pop(0)
|
|
|
|
self.cond.release()
|
|
|
|
try:
|
|
|
|
self.op(item)
|
|
|
|
except Exception, e:
|
|
|
|
traceback.print_exc(sys.stderr)
|
|
|
|
self.cond.acquire()
|
|
|
|
self.cond.wait()
|
2009-05-17 22:15:08 +04:00
|
|
|
|
|
|
|
# vim:se sts=4 sw=4 et:
|