def handle_close_wait_rpc_response_control(self, response_suc_flag=False, response_message=None): """ 关闭等待PRC执行结果控制,根据ACS返回结果进程处理,并将消息发送回RPC请求端 """ if not self.rpc_request_node_message: return #[step-0] tmp_request_msg = self.rpc_request_node_message[0] tmp_request_client_conn = self.rpc_request_node_message[1] #[step-1] if (True == response_suc_flag and None != response_message): # send rpc response message to client ResponseClientHandle.handle_send_response(response_message, tmp_request_client_conn) log.debug_info("Agent send rpc response suc to client") else: # send rpc except message to client err_info = "ACS server's rpc response message type error" log.debug_err(err_info) ResponseClientHandle.handle_except(tmp_request_msg, tmp_request_client_conn, err_info) log.debug_info("Agent send rpc response error to client") #[step-2] #reset self.rpc_request_node_message value self.rpc_request_node_message = None
def start_queue_event_ctrl_thread(self, queue_type, request): """ 状态消息队列处理线程 """ if queue_type in self.dict_queue_expire_property: tmp_expire = self.dict_queue_expire_property[queue_type] if -1 == tmp_expire or 1 == tmp_expire: # changed queue expire property value self.dict_queue_expire_property[queue_type] = 0 # create one thread to call queue event control thread tmp_queue_event_ctr_thread = DUTQueueEventCtrlThread( self, queue_type) tmp_thread_name = "tname_" tmp_thread_name += str(queue_type) tmp_queue_event_ctr_thread.setName(tmp_thread_name) log.debug_err("new thread name=%s" % tmp_thread_name) # start queue event control thread tmp_queue_event_ctr_thread.start() else: pass else: err_info = "DUTQueue queue_type(=%d) not found!" % queue_type log.debug_err(err_info) message = request[0] conn = request[1] ResponseClientHandle.handle_except(message, conn, err_info) return DUTQueue.DUTQUEUE_FAIL return DUTQueue.DUTQUEUE_SUC
def send_event_to_WLserver(dut_obj_handle, message, conn, block=True): """ used to send tcp request to Worklist Server """ try: thread_i = httpclienttoworklist.HttpClientToWorklistServer( dut_handle=dut_obj_handle, url=agentcfg.WL_HTTP_SERVER_URL, timeout=agentcfg.AGENT2WL_HTTP_CLIENT_TIMEOUT) thread_i.msg = ResponseClientHandle.switch_msg_stream_type_dict2str( message) thread_i.msg_type = ( ResponseClientHandle.switch_msg_stream_type_str2dict(message) ).get(event.KEY_MESSAGE) thread_i.conn = conn thread_i.start() if True == block: thread_i.join() except Exception, e: err_info = ( "(Request from client %s)Start http client to WorkList's occurs exception:%s" % (ResponseClientHandle.get_client_ipaddress(conn), e)) log.debug_err(err_info) ResponseClientHandle.handle_except(message, conn, err_info)
def handle_WLexec_start_request(request_event_msg, message, conn): """ 构建工单开始消息,并分发到DUTQueue对象,加入到request_queue队列排队,等待处理 """ log.debug_err("handle_WLexec_start_request start") # construct worklist execute start request tmp_WLexecutestart_msg = constructWLrequesthandle.ConstructWLRequestHandle.construct_WLexec_start_request( message) if None == tmp_WLexecutestart_msg: err_info = "Construct WLexecute start request fail." log.debug_err(err_info) ResponseClientHandle.handle_except(request_event_msg, conn, err_info) return else: try: #dispath worklist exectrue start request to queue object tmp_dict_msg = ResponseClientHandle.switch_msg_stream_type_str2dict( tmp_WLexecutestart_msg) DUTqueuemanagement.DUTQueueManagement.insert_dut_obj( [tmp_dict_msg, conn]) except Exception, e: err_info = "DUTQueueManagement dispath request event occurs exception:%s" % e log.debug_err(err_info) ResponseClientHandle.handle_except(request_event_msg, conn, err_info)
def handle_WLreserve_request(request): """ 构建工单预约消息,并分发到DUTQueue对象 """ log.debug_err("handle_WLreserve_request start") message = request[0] conn = request[1] # construct worklist reseve request tmp_WLreserve_msg = constructWLrequesthandle.ConstructWLRequestHandle.construct_WLreserve_request( message) if None == tmp_WLreserve_msg: err_info = "DUTQueue construct WLreserve request fail." log.debug_err(err_info) ResponseClientHandle.handle_except(message, conn, err_info) else: try: #dispath message to queue object tmp_dict_msg = ResponseClientHandle.switch_msg_stream_type_str2dict( tmp_WLreserve_msg) DUTqueuemanagement.DUTQueueManagement.insert_dut_obj( [tmp_dict_msg, conn]) except Exception, e: err_info = "DUTQueueManagement dispath request event occurs exception:%s" % e log.debug_err(err_info) ResponseClientHandle.handle_except(message, conn, err_info)
def handle_ACS_worklist_info_response(self, message, conn): """ check acs server response worklist info data and call continue event """ response = ResponseClientHandle.switch_msg_stream_type_str2dict( message) msg_type = response.get(event.KEY_MESSAGE) msg_group = int(msg_type) & 0xFF00 if (msg_group == event.EVENT_WORKLIST_GROUP): # check worklist reseve response if (msg_type == event.EV_WORKLIST_RESERVE_RSP): log.debug_info("ACS server's response worklist reserve suc") # call worklist execute start request DUTqueue.ResponseWLexecHandle.handle_WLexec_start_request( self.msg, response, None) elif (msg_type == event.EV_WORKLIST_RESERVE_FAIL): log.debug_info("ACS server's response worklist reserve fail") ResponseClientHandle.handle_send_response(response, conn) # check worklist start response elif (msg_type == event.EV_WORKLIST_EXEC_START_RSP): log.debug_info( "ACS server's response worklist execute start suc") # call worklist execute request DUTqueue.ResponseWLexecHandle.handle_WLexec_request( self.dut_obj_handle, self.msg, response, conn) elif (msg_type == event.EV_WORKLIST_EXEC_START_FAIL): log.debug_info( "ACS server's response worklist execute start fail") ResponseClientHandle.handle_send_response(response, conn) # check worklist finish response elif (msg_type == event.EV_WORKLIST_EXEC_FINISH_RSP): log.debug_info( "ACS server's response worklist execute finish suc") elif (msg_type == event.EV_WORKLIST_EXEC_FINISH_FAIL): log.debug_info( "ACS server's response worklist execute finish fail") # check worklist build/bind/download response else: ResponseClientHandle.handle_send_response(response, conn) else: err_info = "Unsupport msg event group:%d" % msg_group log.debug_info(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info)
def handle_request_except(self, message, conn, err_info): """ 异常处理模块,分工单执行异常或普通消息处理异常 """ if (event.EV_WORKLIST_EXECUTE_RQST == self.msg_type): DUTqueue.ResponseWLexecHandle.handle_WLexec_request_except( self.msg, err_info) else: ResponseClientHandle.handle_except(message, conn, err_info)
def send_event_rpc_methond(self, message, conn): """ 发送RPC消息请求到ACS """ # reset flag to default value self.request_rpc_suc_flag = False self.rpc_request_node_message = [message, conn] log.debug_err("send_event_rpc_methond") # send rpc request to ACS Server RequestClientHandle.send_event_to_acsserver(self, message, conn, True) # check send http request response if False == self.request_rpc_suc_flag: log.app_info( "(Request from client %s)ACS server's http request response except" % ResponseClientHandle.get_client_ipaddress(conn)) else: log.app_info( "(Request from client %s)Begin wait ACS server's rpc response data." % ResponseClientHandle.get_client_ipaddress(conn)) self.check_RPC_status_totalcount = int( self.wait_request_response_timeout_length / 10) while self.rpc_request_node_message and self.check_RPC_status_totalcount > 0: self.check_RPC_status_totalcount -= 1 log.debug_err("self.check_RPC_status_totalcount=%d" % self.check_RPC_status_totalcount) sleep(10) if 0 == self.check_RPC_status_totalcount: err_info = ( "(Request from client %s)Wait ACS server's rpc event response timeout" % ResponseClientHandle.get_client_ipaddress(conn)) log.debug_err(err_info) ResponseClientHandle.handle_except(message, conn, err_info) #send rpc request timeout to ACS Server #add by wangjun 20130624 tmp_rpc_timeout_message = self.build_rpc_request_timeout_message( message) RequestClientHandle.send_event_to_acsserver( None, tmp_rpc_timeout_message, None, True) log.app_info( "(Request from client %s)End wait ACS server's rpc response data." % ResponseClientHandle.get_client_ipaddress(conn)) #reset self.rpc_request_node_message value self.rpc_request_node_message = None
def get_dutqueue_requestqueue_node_data(dut_obj, parent_msg, conn): """ 如果当前有RPC正在执行,则将消息返回给USER,USER端自己做处理。现在该方法已经停止使用。 """ event_list = [] tmp_request_queue = [] tmp_request_queue.extend(dut_obj.request_queue_priority_high) tmp_request_queue.extend(dut_obj.request_queue_priority_normal) if dut_obj.cur_handle_request != []: tmp_request_queue.insert(0, dut_obj.cur_handle_request) for element in tmp_request_queue: # get request message in queue # message=element[0], conn=element[1] reuqest_msg = element[0] msg_type = reuqest_msg.get(event.KEY_MESSAGE) msg_group = int(msg_type) & 0xFF00 msg_obj = reuqest_msg.get(event.KEY_OBJECT) try: strio = StringIO(msg_obj) ret_obj = pickle.load(strio) except Exception, e: err_info = "Unpickle event.KEY_OBJECT occurs error:%s" % e ResponseClientHandle.handle_except(parent_msg, conn, err_info) return tmp_dict = {} if msg_group == event.EVENT_RPC_GROUP: tmp_dict[ret_obj.rpc_name] = ret_obj.rpc_args elif (msg_group == event.EVENT_WORKLIST_GROUP and msg_type == event.EV_WORKLIST_EXEC_START_RQST): tmp_dict["MsgWorklistExecute"] = ret_obj.id_ event_list.append(tmp_dict)
def dispath_event_to_WLrpc(self, msg_type, request): """ 将工单RPC方法加入到WLrpc_queue队列 """ # dispatch try: if not self.WLexec_request_node_message: log.debug_err("DUTQueue not found running worklist request.") self.reset_check_WLexec_status_totalcount() log.debug_info("Reset check_WLexec_status_totalcount=%d" % self.check_WLexec_status_totalcount) self.join_WLrpc_event_queue(request) except Exception, e: err_info = "DUTQueue dispath_event_to_WLrpc function occurs exception:%s" % e log.debug_err(err_info) message = request[0] conn = request[1] ResponseClientHandle.handle_except(message, conn, err_info)
def dispath_event(self, msg_type, msg_priority_level, request, msg_key_sender): """ 分发消息总入口 """ log.debug_info( "(Request from client %s)DUTQueue dispath event key_sender=%s" % (ResponseClientHandle.get_client_ipaddress( request[1]), msg_key_sender)) tmp_intercept_flag = False if (event.KEY_SENDER_USER == msg_key_sender or event.KEY_SENDER_AGENT == msg_key_sender): #处理AGENT自己构建的消息 #add by wangjun 20130717 if event.KEY_SENDER_AGENT == msg_key_sender: msg_key_sender = event.KEY_SENDER_USER tmp_intercept_flag = self.handle_event_user2agent( msg_type, msg_priority_level, request, msg_key_sender) elif (event.KEY_SENDER_ACS == msg_key_sender): tmp_intercept_flag = self.handle_event_acsserver2agent( msg_type, msg_priority_level, request, msg_key_sender) elif (event.KEY_SENDER_WORKLIST == msg_key_sender): tmp_intercept_flag = self.handle_event_worklistserver2agent( msg_type, msg_priority_level, request, msg_key_sender) else: log.debug_info( "(Request from client %s)DUTQueue dispath event error,unsupport message key_sender" % ResponseClientHandle.get_client_ipaddress(request[1])) if not tmp_intercept_flag: err_info = "DUTQueue dispath event error" log.debug_err(err_info) message = request[0] conn = request[1] ResponseClientHandle.handle_except(message, conn, err_info)
def handle_WLexec_request(dut_handle, request_event_msg, message, conn): """ 构建工单执行消息,并将消息发送到Worklist Server """ log.debug_err("handle_WLexec_request start") # construct worklist execute request tmp_WLexecute_msg = constructWLrequesthandle.ConstructWLRequestHandle.construct_WLexec_request( message) if None == tmp_WLexecute_msg: err_info = "Construct WLexecute request fail." log.debug_err(err_info) ResponseClientHandle.handle_except(request_event_msg, conn, err_info) return else: # send worklist execture request to worklist Server if not dut_handle: err_info = "dut_handle is None." log.debug_err(err_info) ResponseClientHandle.handle_except(request_event_msg, conn, err_info) return if not (isinstance(dut_handle, DUTQueue)): err_info = "dut_handle instance not DUTQueue object." log.debug_err(err_info) ResponseClientHandle.handle_except(request_event_msg, conn, err_info) return dut_handle.send_worklist_execute_event_to_WLserver( dut_handle, tmp_WLexecute_msg, conn) return
def dispath_event_to_normal(self, msg_type, msg_priority_level, request): """ 将消息分发到默认的处理队列query_queue或request_queue """ # dispatch try: msg_group = int(msg_type) & 0xFF00 if (msg_group == event.EVENT_QUERY_GROUP or msg_group == event.EVENT_CONFIGURE_GROUP): self.join_query_event_queue(msg_priority_level, request) elif (msg_group == event.EVENT_RPC_GROUP): self.join_request_event_queue(msg_priority_level, request) elif (msg_group == event.EVENT_WORKLIST_GROUP): #特殊处理工单消息,进入agent自己构建的工单执行逻辑模块。reserve->start->execute->finish if msg_type == event.EV_WORKLIST_EXECUTE_RQST: #进入工单预约流程 log.debug_info( "msg_type == event.EV_WORKLIST_RESERVE_RQST") ResponseWLexecHandle.handle_WLreserve_request(request) elif msg_type == event.EV_WORKLIST_EXEC_START_RQST: #进入工单开始流程,这里开始加入RPC队列等待执行。 log.debug_info( "msg_type == event.EV_WORKLIST_EXEC_START_RQST") self.join_request_event_queue(msg_priority_level, request) else: # /******************************************************** #msg_type is: # event.EV_WORKLIST_BUILD_RQST or # event.EV_WORKLIST_BIND_PHISIC_RQST or # event.EV_WORKLIST_BIND_LOGIC_RQST or # event.EV_WORKLIST_RESERVE_RQST or # event.EV_WORKLIST_EXEC_FINISH_RQST # event.EV_WORKLIST_DOWNLOAD_RQST # event.EV_WORKLIST_QUERY_RQST # ********************************************************* self.join_query_event_queue(msg_priority_level, request) else: err_info = "DUTQueue dispath_event_to_normal: user message group(=%d) not support." % msg_group log.debug_err(err_info) message = request[0] conn = request[1] ResponseClientHandle.handle_except(message, conn, err_info) except Exception, e: err_info = "DUTQueue dispath_event_to_normal function occurs exception:%s" % e log.debug_err(err_info) message = request[0] conn = request[1] ResponseClientHandle.handle_except(message, conn, err_info)
def insert_dut_obj(request): """ 解析消息,根据消息类型,分发到DUT_ID对应的DUTQueue对象消息队列中 """ #检查消息的完整性 tmp_msg_is_complete = ResponseClientHandle.check_message_complete( request[0]) if False == tmp_msg_is_complete: err_info = "Recv client request event data incomplete" log.debug_err(err_info) ResponseClientHandle.handle_except(request[0], request[1], err_info) return DUTQueueManagement.delete_expired_dut_obj() message = request[0] message_type = message.get(event.KEY_MESSAGE) #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #add by wangjun 20131024 #拦截守护进程的心跳包数据并发送响应结果 if (message_type == event.EV_QUERY_IS_HANG_RQST): rc_status = DUTQueueManagement.keepalive_request_response( message, request[1]) if True == rc_status: #消息不在往下分发 return #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #add by wangjun 20130716 #获取消息的sequence id message_sequence_id = message.get(event.KEY_SEQUENCE) #将消息加入到MessageSequenceManagement管理队列中,如果该消息已经处理过了, #则直接向client发送处理结果,并返回False标志,否则,返回True,继续消息分发流程 insert_sequence_rc = MessageSequenceManagement.insert_message_sequence_obj( message_sequence_id, request[1]) if False == insert_sequence_rc: return #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ log.app_info( "(Request from client %s)Insert request(message==%s) to DUTQueue" % (ResponseClientHandle.get_client_ipaddress( request[1]), event.get_event_desc(message_type))) dut_id = DUTQueueManagement.get_dut_id(message) if None == dut_id: err_info = "Not found message request sn data" log.debug_err(err_info) if (message_type == event.EV_WORKLIST_EXECUTE_RSP_RQST ): #add by wangjun 20130531 DUTqueue.ResponseWLexecHandle.handle_WLexec_request_except( request[0], err_info) else: ResponseClientHandle.handle_except(request[0], request[1], err_info) return log_dut_id = "insert_dut_obj get_dut_id=" + dut_id log.debug_err(log_dut_id) dut_obj_handle = DUTQueueManagement.get_dut_obj_handle(dut_id) if None == dut_obj_handle: if (message_type == event.EV_WORKLIST_EXECUTE_RSP_RQST ): #add by wangjun 20130531 err_info = "Not found execute worklist DUTQueue running process" log.debug_err(err_info) DUTqueue.ResponseWLexecHandle.handle_WLexec_request_except( request[0], err_info) else: dut_obj_handle = DUTQueueManagement.new_dut_obj(dut_id) if None == dut_obj_handle: return conn = request[1] #add by wangjun 20130523 message_key_sender = message.get(event.KEY_SENDER) if (None == message_key_sender): message_key_sender = event.KEY_SENDER_USER #open QUEUE_INIT key control to GUI #change by wangjun 20130826 if (event.KEY_SENDER_USER == message_key_sender): dut_obj_request_queue_busy = DUTQueueManagement.handle_dutqueue_request_queue_status_check( dut_obj_handle, message, conn) if True == dut_obj_request_queue_busy: log.debug_err("DUTQueue request queue busy") return else: pass #log.debug_err("insert_dut_obj call dispath_event") message_priority_level = message.get(event.KEY_PRIORITY_LEVEL) dut_obj_handle.dispath_event(message_type, message_priority_level, request, message_key_sender)
def run(self): """ override run """ #更新从Agent发送出去消息的KEY_SEQUENCE属性 #add by wangjun 20130722 self.construct_agent_send_message_sequence_property() # send http request and recv response log.app_info( "(Request from client %s)Send request(message=%s) to ACS server's" % (ResponseClientHandle.get_client_ipaddress( self.conn), event.get_event_desc(self.msg_type))) res, self.response = httpclienthandle.HttClientHandle.send_http_msg( in_message=self.msg, in_timeout=self.timeout, in_try_count=3, in_url=self.url, in_method=self.method) log.app_info( "(Request from client %s)Recv ACS server's response(request message=%s)" % (ResponseClientHandle.get_client_ipaddress( self.conn), event.get_event_desc(self.msg_type))) # parse response data try: if res == "error": err_info = "Agent to ACS server's http client error:" + self.response log.debug_err(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info) return elif res == "fail": err_info = "Agent to ACS server's http client fail:" + self.response log.debug_err(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info) return elif res == "response": #检查消息的完整性 check_complete_flag = ResponseClientHandle.check_message_complete( self.response) if False == check_complete_flag: err_info = "Recv HTTP server's response incomplete" log.debug_info(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info) return else: #处理响应消息 #log.debug_info(self.response) self.handle_acsserver_response(self.response, self.conn) return except Exception, e: err_info = "Pickle object occurs exception: %s" % e log.debug_err(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info) return
log.debug_err(err_info) return responseclient.g_http_client_count += 1 log.app_info( "acceptConnection: Agent HTTP connected client count: %d" % responseclient.g_http_client_count) t = threading.Thread(target=root_dispath_evetn, args=(msg, conn)) t.setDaemon(True) t.start() except Exception, e: err_info = "DUTQueueManagement dispath request event occurs exception:%s" % e log.debug_info(err_info) ResponseClientHandle.handle_except(msg, conn, err_info) class AgentHandleRequest(http.Request): """ """ # 定义了路径映射功能 dict_page_handlers = { agentcfg.AGENT_HTTP_SERVER_PATH: handle_dispath_request, } def process(self): """ """ self.setHeader("Content-Type", "text/html")
def handle_acsserver_response(self, message, conn): """ check acs server response data and call continue event """ response = ResponseClientHandle.switch_msg_stream_type_str2dict( message) msg_type = response.get(event.KEY_MESSAGE) msg_group = int(msg_type) & 0xFF00 #特殊处理AGENT 构建的给ACS的TIMOUT消息响应 if self.msg_type == event.EV_RPC_AGENT_TIMEOUT_POST: if msg_type == event.EV_RPC_AGENT_TIMEOUT_RSP: log.debug_info( "ACS server's response check agent timeout rpc request suc" ) else: log.debug_info( "ACS server's response check agent timeout rpc request fail" ) return #检查消息的合法性 #response message type error if not self.handle_response_message_type_verify( msg_group, msg_type, response): #check EV_RPC_CHECK_FAIL response if (msg_type == event.EV_RPC_CHECK_FAIL): tmp_obj = response.get(event.KEY_OBJECT) strio = StringIO(tmp_obj) tmp_msg_key_obj = pickle.load(strio) if not (isinstance(tmp_msg_key_obj, event.MsgUserRpcCheck)): err_info = "ACS server's rpc response message type error" log.debug_info(err_info) else: tmp_response_dict_ret = tmp_msg_key_obj.dict_ret if "str_result" in tmp_response_dict_ret: rc_str_result = tmp_response_dict_ret.get("str_result") err_info = "ACS server's rpc response check message fail, str_result: " + rc_str_result log.debug_info(err_info) else: err_info = "ACS server's rpc response message not found dict_ret data" log.debug_info(err_info) else: err_info = "ACS server's rpc response message type error" log.debug_info(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info) return #response rpc post if (msg_group == event.EVENT_QUERY_GROUP or msg_group == event.EVENT_CONFIGURE_GROUP): # send response to user or ACS ResponseClientHandle.handle_send_response(response, conn) elif (msg_group == event.EVENT_RPC_GROUP): if not DUTqueue.WAIT_RPC_RESPONSE_POST_FALG: # send response to user or ACS ResponseClientHandle.handle_send_response(response, conn) else: self.set_rpc_request_ACSServer_check_response("request_suc") #response worklist build/bind/reserve/start/finish info post elif (msg_group == event.EVENT_WORKLIST_GROUP): self.handle_ACS_worklist_info_response(response, conn) else: err_info = "Unsupport msg event group:%d" % msg_group log.debug_info(err_info) ResponseClientHandle.handle_except(self.msg, self.conn, err_info)