def __res_server__(): # template (TARGET, func_name, paramter, content_id) template = (TARGET, "?", "?", "?") while True: try: request, request_id = sky_client.take(template, return_id=True, timeout=60000) if request is None: continue _, func_name, param = request param = utils.decode(param) log().info("REQUEST, func_name=%s, params=%s", func_name, param) if func_name in remote_call_book: function = remote_call_book[func_name] result = function(**param) log().info("Result=%s", str(result)) sky_client.write(tuple=(RECEIVER, request_id, utils.encode(result)), expire=3000) else: log().warn("invalid func_name:%s", func_name) except Exception as e: log().error("handle request error, exception:%s\n%s", e, traceback.format_exc())
def handle_add_event_listener(event_id, ref_res, condition): if protocal.FUNCTION_TYPE_CALLABLE in condition: condition = utils.string_to_function(condition[FUNCTION_TYPE_CALLABLE], "condition") elif protocal.FUNCTION_TYPE_DEFAULT in condition: if condition[FUNCTION_TYPE_DEFAULT] == default_condition.CONDITION_CLOCK_TICK: __clock_tick_event_receiver.append(event_id) return else: condition = default_condition.get_condition(condition[FUNCTION_TYPE_DEFAULT]) else: raise Exception("unknown condition:" + condition) res_manager.add_listener(ref_res, condition, lambda: sky_client.write(tuple=(RECEIVER, "Event", event_id)))
def wrapper(*args, **kwargs): func(*args, **kwargs) func_name = func.func_name param = utils.get_func_arguments(func, args, kwargs) log().info("CALL %s, param = %s", func_name, param) cid = sky_client.write(tuple=(TARGET, func_name, utils.encode(param)), fill_content_id=True) result = sky_client.take(template=(RECEIVER, cid, "?")) if result is None or len(result) < 3: log().warn("request failed, cid=%s", cid) log().warn("result=%s", str(result)) return return utils.decode(result[RESULT_IDX_CONTENT])
def tick_or_tock(): global __on_tick if not __on_tick: res_manager.changed_res_set.clear() res_manager.run_timer() __on_tick = True if len(__clock_tick_event_receiver) > 0: for receiver in __clock_tick_event_receiver: notify_id = sky_client.write((RECEIVER, "Event", receiver),fill_content_id=True) #sky_client.take(template=(TARGET, notify_id, '?')) else: res_manager.run_listener() Clock.tick() report() __on_tick = False