def TokenId(self, token_id): global client_token_id print('Get Sever token_id {}'.format(token_id)) self.token_id = token_id client_token_id = token_id conn_client = ConnectService.Client(self.protocol) conn_client.TokenId(self.token_id)
def client_closed(self, oprot): client = ConnectService.Client(oprot) client_md5 = get_md5(str(oprot)) client_list_tmp.pop(client_md5) if client_md5 in client_list.keys(): client_list.pop(client_md5) print('Delete client oprot == {}'.format(client_md5))
def get_protocol(service_name): transport = TSocket.TSocket('127.0.0.1', 9999) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) defined_protocol = TMultiplexedProtocol(protocol, service_name) transport.open() #打开链接 time.sleep(0.5) if transport.isOpen(): connect_handler = ConnectServiceNoneHandler() conn_processor = ConnectService.Processor(connect_handler) if conn_processor.process(protocol, protocol): return defined_protocol return None
def cmd_client_process(protocol, cmdstr=None): try: conn_client = ConnectService.Client(protocol) cmd = CommandMsg() cmd.cmdstr = cmdstr while True: conn_client.Command(cmd) time.sleep(3) except TTransport.TTransportException as tx: print("cmd_client_process Error TTransport.TTransportException == {}". format(tx)) except Exception as x: print("Error Exeption == {}".format(x))
def Ping(self, token_id, time): try: print('Get client ping.............. {} '.format(token_id)) if token_id in client_list.keys(): if client_list[token_id].trans.isOpen(): client = ConnectService.Client(client_list[token_id]) int_time = int(dt.datetime.now().timestamp() - dt.datetime.utcfromtimestamp(0).timestamp()) client.Ping(token_id, int_time) except TTransport.TTransportException as tx: print("2222222 Ping Error TTransport.TTransportException == {}". format(tx)) except Exception as x: print("Error Exeption == {}".format(x))
def client_connected(self, oprot): global client_list_tmp try: client = ConnectService.Client(oprot) client_md5 = get_md5(str(oprot)) client_list_tmp[client_md5] = oprot if oprot.trans.isOpen(): client.TokenId(client_md5) print('Client.token_id called {}'.format(client_md5)) except TTransport.TTransportException as tx: print( "1111111 client_connected Error TTransport.TTransportException == {}" .format(tx)) except Exception as x: print("Error Exeption == {}".format(x))
def cmd_server_process(): try: transport = TSocket.TSocket('127.0.0.1', 9999) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) mul_protocol = TMultiplexedProtocol(protocol, "ConnectService") connect_handler = ConnectServiceHandler(mul_protocol) conn_processor = ConnectService.Processor(connect_handler) transport.open() #打开链接 #startThreadService(cmd_client_process, mul_protocol, cmdstr="TestCmd") while True: conn_processor.process(protocol, protocol) except TTransport.TTransportException as tx: print("cmd_server_process Error TTransport.TTransportException == {}". format(tx)) except Exception as x: print("Error Exeption == {}".format(x))
def do_command_call(): global client_list try: while True: client_tmp_list = client_list.copy() for token_id in client_tmp_list.keys(): client = ConnectService.Client(client_tmp_list[token_id]) int_time = int(dt.datetime.now().timestamp() - dt.datetime.utcfromtimestamp(0).timestamp()) client.Ping(token_id, int_time) time.sleep(1) time.sleep(1) except TTransport.TTransportException as tx: print("do_command_call Error TTransport.TTransportException == {}". format(tx)) except Exception as x: print("Error Exeption == {}".format(x))
if msq_type: connect_handler.client_connected(protocol) else: connect_handler.client_closed(protocol) except Exception as x: print("Error Exeption == {}".format(x)) mng_handler = ManageServiceHandler() mng_processor = ManageService.Processor(mng_handler) file_handler = FileServiceHandler() file_processor = FileService.Processor(file_handler) connect_handler = ConnectServiceHandler() conn_processor = ConnectService.Processor(connect_handler) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() processor = TMultiplexedProcessor() #使用TMultiplexedProcessor接收多个处理 processor.registerProcessor("ManageService", mng_processor) processor.registerProcessor("FileService", file_processor) processor.registerProcessor("ConnectService", conn_processor) transport = TSocket.TServerSocket('0.0.0.0', 9999) #server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) server.setNumThreads(6000) # 1个主线程 1个监听线程 + 多少个通信线程 # start client connect handler: save client socket info conn_mq_thread = threading.Thread(target=thrift_connect_handler,