def mapping_method(in_methon_from_module_handle, in_conn, in_sequence_id, in_method_name, in_cpe_id=None, in_method_parameters_list=[]): #执行method方法 log.debug_info("Run mapping_method START") #检测cpe id是否有效,如果是有效数据则需要预先调用'switch_cpe'接口 rc_status_flag,\ rc_result_data=MethodMappingAgent._run_swicth_cpe_method(in_methon_from_module_handle,in_cpe_id) #检查请求不是switch_cpe操作,并且上一个步骤,switch_cpe操作成功 if (TR069_SWITCH_CPE_METHOD_NAME != in_method_name and True == rc_status_flag): #调用in_method_name对应接口 rc_status_flag, rc_result_data = MethodMappingAgent._run_method( in_methon_from_module_handle, in_method_name, in_method_parameters_list) log.debug_info("Run mapping_method END") #给客户端回method执行响应 MethodMappingAgent._send_run_method_response_data_to_client( in_conn, in_sequence_id, rc_status_flag, rc_result_data)
def create_tr069_user_client_object(): """ 生成TR069 user client对象 """ #[1] #加载TR069库 try: from TR069.RF import TR069 except Exception, e: try: if cmp(e, "No module named TR069.RF"): #加载TR069库 TR069UserClientCfg._load_tr069_user_client_lib_path() log.debug_info('Load TR069 user client lib path') from TR069.RF import TR069 except Exception, e: if cmp(e, "No module named TR069.RF"): print "^^^^^^^^^^^^^^^ No module named TR069.RF" return None error_info = "%s" % str(e) log.debug_info(error_info) return None
def write_data_to_client(in_conn_handle, in_response_data): """ 通过Client连接句柄发送响应数据到连接客户端 """ global g_tcp_client_count global g_tcp_client_count_lock #写数据 if in_conn_handle: #检查当前系统中有效的连接接句柄总数 log.debug_err("Client connected handle count number=%d" % ClientConnectCount.get_tcp_client_count()) if 0 == ClientConnectCount.get_tcp_client_count(): in_conn_handle = None log.debug_info("write_data_to_client FAIL") return False try: in_conn_handle.send(str(in_response_data)) in_conn_handle.close() log.debug_info("write_data_to_client SUC") return True except Exception, e: err_info = "write_data_to_client occurs expection: %s" % e log.debug_err(err_info) log.debug_info("write_data_to_client FAIL") return False finally:
def get_method_request_control_thread_total_count(): """ 返回request处理线程对象总数 """ global client_request_control_thread_object_list log.debug_info("BUFLIST METHOD THREAD COUNT=%d" % len(client_request_control_thread_object_list)) return len(client_request_control_thread_object_list)
def construct_response_data(in_message_id, in_status, in_response_data): try: construct_xml_stream_data = XMLConstructControl.construct_xml_stream_data( in_message_id, in_status, in_response_data) #log.debug_info(construct_xml_stream_data) except Exception, e: log.debug_info(e) return None
def _dispath_top_request(): """ 下发消息到处理线程接口 """ try: #获取存在的request处理线程对象总数 temp_request_control_thread_total_count=DispathMethodRequest.get_method_request_control_thread_total_count() #获取空闲的request处理线程对象总数,当有处理线程空闲时,直接下发消息节点 temp_request_control_thread_idle_count=DispathMethodRequest.get_method_request_control_thread_idle_count() #存在空闲的request处理线程对象,直接下发消息节点 if temp_request_control_thread_idle_count>0: log.debug_info("Dispath client request to idle control thread") #获取客户端消息节点缓存队列最前面的消息节点 client_request_node=MethodNodeManagement.\ pop_client_request_node_from_wait_run_buffer_list() if not client_request_node: log.debug_info("BUFLIST: Not exist waiting method request") return False #存在空闲的request处理线程对象,直接下发消息节点到该对象 DispathMethodRequest.dispath_method_request_to_control_thread(client_request_node) return True elif temp_request_control_thread_total_count<OPEN_CONTROL_THREAD_MAX_COUNT: log.debug_info("Create new thread and dispath messages to the object") #获取客户端消息节点缓存队列最前面的消息节点 client_request_node=MethodNodeManagement.\ pop_client_request_node_from_wait_run_buffer_list() if not client_request_node: log.debug_info("BUFLIST: Not exist waiting method request") return False #request线程对象都处于忙状态或者不存在request线程对象,创建新的处理线程节点并直接下发消息节点到该对象 DispathMethodRequest.dispath_method_request_to_control_thread(client_request_node) return True except Exception, e: err_info = "Dispath client request to idle control thread occurs expection: %s" % e log.debug_err(err_info) #返回错误消息 in_conn_handle=client_request_node.get_client_conn_handle() ClientResponseHandle.send_error_info_data_to_client(in_conn_handle,err_info)
def start_service(): """ 启动服务器模块 """ try: StartMethodAgentServer(METHOD_AGENT_SERVER_IP, METHOD_AGENT_SERVER_PORT) except Exception, e: err_info = "iTest agent service occurs expection: %s" % e log.debug_err(err_info)
def push_request_method_data_to_property(self,in_methond_data): """ 将method数据保存到对象数据中 """ log.debug_info("METHOD_THREAD_STATUS=%s" % self._process_idle_flag ) if self._process_idle_flag: self._process_idle_flag=False self._method_data_noe=in_methond_data self._process_idle_time=datetime.datetime.now() return True else: return False
def run(self): """ 处理method请求 """ #退出线程 if not self._process_method_agent_obj: return while 1: #线程处于空闲状态 if not self._process_idle_flag: if self._method_data_noe: log.debug_info("Thread running method START" ) #读取method名字以及参数数据 tmp_client_conn=self._method_data_noe.get_client_conn_handle() tmp_sequence_id=self._method_data_noe.get_request_sequence_id() temp_method_name=self._method_data_noe.get_request_method_name() temp_method_parameters_list=self._method_data_noe.get_request_method_parameters() temp_cpe_id=self._method_data_noe.get_request_cpe_id() #调用方法映射接口执行method对应的方法 MethodMappingAgent.mapping_method(self._process_method_agent_obj, tmp_client_conn, tmp_sequence_id, temp_method_name, temp_cpe_id, temp_method_parameters_list) log.debug_info("Thread running method END" ) #还原线程空闲状态以及清空数据节点的值 self._process_idle_flag=True self._method_data_noe=None log.debug_info("Reset thread running data info" ) else: #获取当前系统时间 current_time_object=datetime.datetime.now() #log.debug_info(("current time: %s" % current_time_object.strftime('%Y-%m-%d %H:%M:%S' )) ) #获取线程空闲的时长,当空闲超过60分钟,退出线程,释放系统资源 difference_seconds=(current_time_object - self._process_idle_time).seconds if difference_seconds >= self._process_invalid_time_length: #将即将实现的thread对象从处理线程对象句柄列表中清除 DispathMethodRequest.destroy_client_request_control_thread(self) log.debug_info(("Invalid thread, call destroy_client_request_control_thread interface") ) break else: time.sleep(1) continue
def parse_recv_data(int_recvdatabuffer): """ 解析XML流数据 """ rc_flag = False try: print int_recvdatabuffer rc_flag, rc_data_list = XMLParseControl.parse_xml_stream_data( int_recvdatabuffer) #log.debug_info(rc_data_list) except Exception, e: log.debug_info(e) return None, None, []
def pop_client_request_node_from_wait_run_buffer_list(): """ 从客户端消息节点缓存队列中取出第0个节点 """ global client_request_node_list global client_request_node_list_lock client_request_node=None client_request_node_list_lock.acquire() if len(client_request_node_list): client_request_node=client_request_node_list.pop(0) log.debug_info("pop_client_request_node_from_wait_run_buffer_list SUC") client_request_node_list_lock.release() return client_request_node
def test_tr069_object_methond(): #[1] try: tr069_plugin_client = TR069UserClientCfg.create_tr069_user_client_object( ) #tr069_plugin_client=TR069() tr069_plugin_client.config_remote_server_addr('172.16.28.59') tr069_plugin_client.switch_cpe('021018-021018000074') temp_wl_id = tr069_plugin_client.init_worklist_wlan_add() tr069_plugin_client.bind_physic_worklist(temp_wl_id, '021018-021018000074') tr069_plugin_client.execute_worklist(temp_wl_id) except Exception, e: error_info = "%s" % str(e) log.debug_info(error_info) return False, error_info
def test_construct(): try: in_message_id = "0126345" in_status = 0 in_data0 = None in_data1 = ['testing1', 8000, 'testing2', 0.54728] in_data2 = { 'key1': 'somevalue1', 'key2': 'somevalue2', "key3": [ 1, 2, 5, 8, ], 'key4': { "chind_key1": "child_somevalue1", "chind_key2": ['a', 'bcdf'] } } in_data3 = [ 'arg1', ['arg2', { "arg3": "abc", "arg4": [1, 2, 4] }], { "arg5": "5cdef", "arg6": ["3df3", 3], "arg7": 1000 }, 0.75, 493 ] tflag = False in_data4 = [tflag, None] in_message_id = '0123456' out_construct_xml_stream_data = ConstructResponseData.construct_response_data( in_sequence_id, in_status, in_data1) log.debug_info(out_construct_xml_stream_data) except Exception, e: parse_process_log('\n\n%s' % e)
def start_socket_server(self): """ 启动TCP连接服务器,处理来自TCL客户端发送的连接请求,同时将请求的处理分发给子进程处理。 """ #检查SOCKET连接对象是否存在,如果存在则端口连接,并且重置SOCKET句柄。 if self.sock: self.sock.close() self.sock = None #创建TCP SOCKET 服务器。 try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind((self.addr, self.port)) self.sock.listen(MAX_LISTEM_CONNECT_NUMBER) except Exception, e: err_info = "Create socket server occurs exception:%s" % e log.debug_err(err_info) return
def get_method_request_control_thread_idle_count(): """ 返回空闲的request处理线程对象总数 """ global client_request_control_thread_object_list global client_request_control_thread_object_list_lock idle_client_request_control_thread_object_count=0 client_request_control_thread_object_list_lock.acquire() for thread_node in client_request_control_thread_object_list: if thread_node.get_process_idle_flag(): idle_client_request_control_thread_object_count += 1 client_request_control_thread_object_list_lock.release() log.debug_info("BUFLIST IDLE METHOD THREAD COUNT=%d" % idle_client_request_control_thread_object_count) return idle_client_request_control_thread_object_count
def get_idle_method_request_control_thread_handle(): """ 返回返回任一空闲的request处理线程对象句柄 """ global client_request_control_thread_object_list global client_request_control_thread_object_list_lock idle_thread_node_handle=None client_request_control_thread_object_list_lock.acquire() log.debug_info("BUFLIST METHOD THREAD COUNT=%d" % len(client_request_control_thread_object_list)) for thread_node in client_request_control_thread_object_list: if thread_node.get_process_idle_flag(): idle_thread_node_handle=thread_node break client_request_control_thread_object_list_lock.release() return idle_thread_node_handle
def test_parse(): try: message = """<?xml version='1.0' encoding='UTF-8'?> <root> <messageid>0123456</messageid> <function>test_method</function> <parameters> <parameter name='data1' type="basictype"> <value type="string">dGVzdGluZzE=</value> </parameter> <parameter name='data2' type="basictype"> <value type="string">dGVzdGluZzI=</value> </parameter> <parameter name='data3' type="list"> <value type="int">124</value> <value type="bool">True</value> <value type="string">dGVzdGluZzE=</value> </parameter> <parameter name='data4' type="dict"> <item> <key>test</key> <value type="string">dGVzdGluZzE=</value> </item> </parameter> </parameters> </root> """ tmp_sequence, \ tmp_method_name, \ tmp_method_parameters_list=ParseRecvData.parse_recv_data(message) log.debug_info( '\n\nmessage_id=%s, method_name=%s, parameters_list=%s' % (tmp_sequence, tmp_method_name, tmp_method_parameters_list)) except Exception, e: log.debug_err('\n\n%s' % e)
def handle_message(self, message): """ 处理消息 """ #log.debug_info("Recv total data:%s\n" % message) try: #解析消息体数据 tmp_sequence, \ tmp_method_name, \ tmp_cpe_id, \ tmp_method_parameters_list=ParseRecvData.parse_recv_data(message) except Exception, e: #解析消息体数据异常 err_info = "Parse recv message data occurs expection: %s" % e log.debug_err(err_info) #返回错误消息 ClientResponseHandle.send_error_info_data_to_client( self.conn, err_info) return False
def send_error_info_data_to_client(in_conn_handle, in_error_info): """ 构造错误信息并将错误返回给Client """ try: #获取消息message_id from methodsequencemanagement import MethodSequenceManagement in_sequence_id = MethodSequenceManagement.get_method_connect_handle_link_object_sequence_id_property( in_conn_handle) if None == in_sequence_id: in_sequence_id = 0 #构建返回XML数据 from dataprocess import ConstructResponseData out_data = ConstructResponseData.construct_response_data( in_sequence_id, ClientResponseHandle.RESPONSE_STATUS_FAILE, in_error_info) except Exception, e: err_info = "Construct response message data occurs expection: %s" % e log.debug_err(err_info) log.debug_info("send_error_info_data_to_client FAIL") return
def set_method_request_control_thread_management_property_data(in_thread_handle,in_method_data): """ 更新请求处理method相关数据内容 """ if not in_thread_handle: return False #更新请求处理method相关数据内容 temp_push_data_suc_flag=in_thread_handle.push_request_method_data_to_property(in_method_data) #数据处理对象句柄无效,错误处理 if not temp_push_data_suc_flag: err_info = "Dispath method request to control thread fail. Method control object handle is Invalid." log.debug_err(err_info) #返回错误消息 in_conn_handle=in_method_data.get_client_conn_handle() ClientResponseHandle.send_error_info_data_to_client(in_conn_handle,err_info) return False return True
def get_message_total_length(self, in_message_head): """ 取消息体有效数据长度 """ message_total_length = 0 split_data_pos = 0 #取消息体有效数据长度 split_pos = in_message_head.find( MESSAGE_TOTAL_LENGTH_DATA_SPLIT_STRING) if split_pos: message_len_str = in_message_head[0:split_pos] try: message_total_length = string.atoi(message_len_str) log.debug_info("Client send message data total length=%d" % message_total_length) except Exception, e: err_info = "get_message_total_length occurs expection: %s" % e log.debug_err(err_info) message_total_length = 0
def push_client_request_node_to_wait_run_buffer_list(in_request_node): """ 将消息节点加入到客户端消息节点缓存队列 """ global client_request_node_list global client_request_node_list_lock #启动客户端消息节点缓存队列管理线程 MethodNodeManagement._start_client_request_node_list_management_thread() #将请求节点插入到客户端消息节点缓存队列 client_request_node_list_lock.acquire() if in_request_node not in client_request_node_list: client_request_node_list.append(in_request_node) log.debug_info("push_client_request_node_to_wait_run_buffer_list SUC") else: log.debug_info("push_client_request_node_to_wait_run_buffer_list FAIL. Node is exist.") return False client_request_node_list_lock.release() #下发消息到处理线程接口 #当有空闲处理线程时,将请求直接下放,否则将请求加入一个列表,当检测到有空闲处理线程时,再下放。 return MethodNodeManagement._dispath_top_request()
def StartMethodAgentServer(in_ip, in_port): log.debug_info("Start method agent service...") #启动MethodAgentServer监听来自TCL客户端的消息请求 try: log.debug_info("Method Agent (ip=%s, port=%s) start." % (in_ip, in_port)) ss_obj = MethodAgentServer(in_ip, in_port) ss_obj.start_socket_server() except Exception, e: err_info = "Method agent service occurs expection: %s" % e log.debug_err(err_info)
def _run_method(in_methon_from_module_handle, in_method_name, in_method_parameters_list=[]): if not in_methon_from_module_handle: error_info = "Input method from module handle is None." return False, error_info if not in_method_name: error_info = "Input method name is None." return False, error_info method_name = in_method_name tcldataagent_object = in_methon_from_module_handle attr_isexist_flag = hasattr(tcldataagent_object, method_name) log.debug_info(attr_isexist_flag) if hasattr(tcldataagent_object, method_name): try: log.debug_info("Call method:%s" % method_name) method_args = in_method_parameters_list if not method_args: result = getattr(tcldataagent_object, method_name)() else: result = getattr(tcldataagent_object, method_name)(*method_args) except Exception, e: error_info = "%s" % str(e) log.debug_info(error_info) return False, error_info log.debug_info(result) return True, result
def send_response_data_to_client(in_conn_handle, in_response_data): """ 给Client回响应数据已更新消息处理状态 """ try: #发送响应到当前保存Client句柄 rc = ClientResponseHandle.write_data_to_client( in_conn_handle, in_response_data) #保存消息处理结果已经消息处理状态 from methodsequencemanagement import MethodSequenceManagement MethodSequenceManagement.finish_method_sequence_obj( in_conn_handle, in_response_data, rc) log.debug_info("send_response_data_to_client SUC") except Exception, e: err_info = "send_response_data_to_client occurs expection: %s" % e log.debug_err(err_info) log.debug_info("send_response_data_to_client FAIL")
def write_keepalive_data_to_client(in_conn_handle, in_response_data): """ 通过Client连接句柄发送心跳响应到连接客户端 """ #写心跳包数据 if in_conn_handle: try: #in_conn_handle.send(str(in_response_data)) log.debug_info("write_keepalive_data_to_client SUC") return True except Exception, e: err_info = "write_keepalive_data_to_client occurs expection: %s" % e log.debug_err(err_info) log.debug_info("write_keepalive_data_to_client FAIL") return False
def parse_process_log(in_value_string): if OPEN_XML_PROCESS_LOG: log.debug_info( in_value_string)
try: #in_conn_handle.send(str(in_response_data)) log.debug_info("write_keepalive_data_to_client SUC") return True except Exception, e: err_info = "write_keepalive_data_to_client occurs expection: %s" % e log.debug_err(err_info) log.debug_info("write_keepalive_data_to_client FAIL") return False else: log.debug_err( "write_keepalive_data_to_client in_conn_handle is None") return False @staticmethod def write_data_to_client(in_conn_handle, in_response_data): """ 通过Client连接句柄发送响应数据到连接客户端 """ global g_tcp_client_count global g_tcp_client_count_lock #写数据 if in_conn_handle: #检查当前系统中有效的连接接句柄总数 log.debug_err("Client connected handle count number=%d" %
def test_method_run_tr069_interface(): """ 测试 """ try: tr069userclient_object = TR069UserClientCfg.create_tr069_user_client_object( ) test_method_name = "switch_cpe" test_xml_data_list = [] test_xml_data_list.append('021018-021018000074') rc_info = MethodMappingAgent._run_method(tr069userclient_object, test_method_name, test_xml_data_list) test_method_name = "config_remote_server_addr" test_xml_data_list = [] test_xml_data_list.append('172.16.28.59') rc_info = MethodMappingAgent._run_method(tr069userclient_object, test_method_name, test_xml_data_list) test_method_name = "init_worklist_wlan_add" test_xml_data_list = [] rc_status_flag, rc_result_data = MethodMappingAgent._run_method( tr069userclient_object, test_method_name, test_xml_data_list) log.debug_info("%s: result data=%s" % (test_method_name, rc_result_data)) if not rc_status_flag: return temp_wl_id = rc_result_data test_method_name = "bind_physic_worklist" test_xml_data_list = [] test_xml_data_list.append(temp_wl_id) test_xml_data_list.append('021018-021018000074') rc_status_flag, rc_result_data = MethodMappingAgent._run_method( tr069userclient_object, test_method_name, test_xml_data_list) log.debug_info("%s: result data=%s" % (test_method_name, rc_result_data)) if not rc_status_flag: return test_method_name = "execute_worklist" test_xml_data_list = [] test_xml_data_list.append(temp_wl_id) rc_status_flag, rc_result_data = MethodMappingAgent._run_method( tr069userclient_object, test_method_name, test_xml_data_list) log.debug_info("%s: result data=%s" % (test_method_name, rc_result_data)) except Exception, e: error_info = "%s" % str(e) log.debug_info(error_info) return False, error_info
else: result = getattr(tcldataagent_object, method_name)(*method_args) except Exception, e: error_info = "%s" % str(e) log.debug_info(error_info) return False, error_info log.debug_info(result) return True, result else: error_info = "Not found insterface." log.debug_info(error_info) return False, error_info class TR069UserClientCfg(): @staticmethod def create_tr069_user_client_object(): """ 生成TR069 user client对象 """ #[1] #加载TR069库 try: from TR069.RF import TR069