def _fret_json(ret_str): try: dicts = json.loads(ret_str) except: raise RpcsyncwerkError('Invalid response format') if dicts.has_key('err_code'): raise RpcsyncwerkError(dicts['err_msg']) if dicts['ret']: return dicts['ret'] else: return None
def _fret_objlist(ret_str): try: dicts = json.loads(ret_str) except: raise RpcsyncwerkError('Invalid response format') if dicts.has_key('err_code'): raise RpcsyncwerkError(dicts['err_msg']) l = [] if dicts['ret']: for elt in dicts['ret']: l.append(_RpcsyncwerkObj(elt)) return l
class RpcsyncwerkServer(object): def __init__(self): self.services = {} def create_service(self, svcname): service = RpcsyncwerkService(svcname) self.services[svcname] = service def register_function(self, svcname, fn, fname=None): service = self.services[svcname] if fname == None: fname = fn.__name__ service.func_table[fname] = fn def _call_function(self, svcname, fcallstr): """input str -> output str""" try: argv = json.loads(fcallstr) except Exception, e: raise RpcsyncwerkError('bad call str: ' + str(e)) service = self.services[svcname] fname = argv[0] fn = service.func_table.get(fname, None) if fn is None: raise RpcsyncwerkError('No such funtion %s' % fname) ret = fn(*argv[1:]) return ret
def decorate(func): if ret_type == "void": fret = None elif ret_type == "object": fret = _fret_obj elif ret_type == "objlist": fret = _fret_objlist elif ret_type == "int": fret = _fret_int elif ret_type == "int64": fret = _fret_int elif ret_type == "string": fret = _fret_string elif ret_type == "json": fret = _fret_json else: raise RpcsyncwerkError('Invial return type') def newfunc(self, *args): array = [func.__name__] + list(args) fcall_str = json.dumps(array) ret_str = self.call_remote_func_sync(fcall_str) if fret: return fret(ret_str) return newfunc
def _call_function(self, svcname, fcallstr): """input str -> output str""" try: argv = json.loads(fcallstr) except Exception, e: raise RpcsyncwerkError('bad call str: ' + str(e))