def parsePayload(self, payload_buf): result = AI_FAILURE try: payload = AiCommonPayload() (self.direction, data_len, bin_len, data, bin_data) = payload.ParseFromString(payload_buf) if data_len > 0: data = binascii.a2b_hex(data) log_print(LOG_LEVEL_DEBUG, "Car Analyse: Parse payload, data={0}.".format(data)) dict_data = json.loads(data) self.format = dict_data["format"] self.image_width = dict_data["image_width"] self.image_height = dict_data["image_height"] self.process_result = dict_data["process_result"] self.score = dict_data["score"] self.cost_time = dict_data["cost_time"] self.json_data = dict_data["json_data"] self.bin_len = bin_len self.bin_data = bin_data self.data_normal = True result = AI_SUCCESS except Exception as ee: result = AI_FAILURE exstr = traceback.format_exc() print(exstr) return result
def node_generate_request_package(cmd, busi_buf, in_buf_len): if not busi_buf: log_print(LOG_LEVEL_DEBUG, "Request business data buffer is None") return None pkg = AiNetworkPackage(cmd=cmd, dataLen=in_buf_len, pkgData=busi_buf) (out_pkg_buf, out_pkg_len) = pkg.SerializeToString() return [out_pkg_buf, out_pkg_len]
def start_server_listen(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setblocking(False) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind((bind_ip, bind_port)) server.listen(5) log_print(LOG_LEVEL_DEBUG, 'Listening on {}:{}'.format(bind_ip, bind_port)) return server
def ParseFromString(self, data): if not data: log_print(LOG_LEVEL_DEBUG, 'Data is None') return None structHeader = struct.Struct(self.front_part_format) (self.prefix, self.magic, self.command, self.length) = structHeader.unpack_from(data[:structHeader.size]) if self.length > 0: self.data = data[structHeader.size:(self.length + structHeader.size)] return [self.command, self.length, self.data]
def run(self): log_print( LOG_LEVEL_DEBUG, 'MonitorThread(%d %s) handle client connection start.' % (self.ident, self.name)) self.runing = True while self.running == True: time.sleep(100.0) if self.busi_thread is None or self.busi_thread.isRunning( ) == False: self.busi_thread = restart_business_thread(self.taskQueue) self.running = False log_print(LOG_LEVEL_DEBUG, 'MonitorThread is terminate')
def net_read_package(sock_conn, data_buffer): start_time = time.time() if sock_conn == None or data_buffer == None: return [-1, data_buffer, 0] result = -1 total_byte = 0 n_recv = 0 retry = 16 check = False offset = -1 while retry > 0: try: data = sock_conn.recv(AI_TCP_BUF_MAX_LEN) except BlockingIOError as e: pass if not data: result = 0 break n_recv = len(data) data_buffer += data total_byte += n_recv if total_byte < AI_PKG_FRONT_PART_SIZE: retry -= 1 continue check, offset, need_recv = node_check_package_buffer( data_buffer, total_byte) while need_recv: data_continue = None try: data_continue = sock_conn.recv(AI_TCP_BUF_MAX_LEN) except BlockingIOError as e: pass if not data_continue: log_print(LOG_LEVEL_ERROR, "Receive data error") result = 0 break n_recv = len(data_continue) data_buffer += data_continue total_byte += n_recv check, offset, need_recv = node_check_package_buffer( data_buffer, total_byte) if check: result = total_byte break retry -= 1 return [result, data_buffer, offset]
def do_car_identify(self, jpeg_image): ret = False car_info = None start_time = time.time() success = False cmd = API_CMD_CAR_BRAND_QUERY try: format = 0 imageWidth = 0 imageHeight = 0 jsonData = "{}" binData = jpeg_image binLen = len(binData) car_pkg = CarComparePackage() req_buf, req_buf_len = car_pkg.createRequest( format=format, imageWidth=imageWidth, imageHeight=imageHeight, binLen=binLen, binData=binData, jsonData=jsonData, processResult=0, score=0.0, costTime=0) (result, resp_pkg) = net_business_communicate(self.car_identify_ip, self.car_identify_port, cmd, req_buf, req_buf_len) network_time = time.time() if result == AI_SUCCESS: (respCmd, resp_busi_len, resp_busi_buf) = resp_pkg.GetData() success = car_pkg.parsePayload(resp_busi_buf) if success == AI_SUCCESS: json_dict = json.loads(car_pkg.json_data) if json_dict["process_result"] == AI_SUCCESS: car_brand_id = json_dict["id"] car_brand_name = json_dict["name"] car_info = [car_brand_id, car_brand_name] ret = True except Exception as ee: exstr = traceback.format_exc() print(exstr) log_print( LOG_LEVEL_DEBUG, "Car identify analyse cost %f s." % (time.time() - start_time)) return ret, car_info
def ParseFromString(self, data): if not data: log_print(LOG_LEVEL_DEBUG, 'Data is None') return None structHeader = struct.Struct(self.default_format) (self.direction, self.length, self.binlen) = structHeader.unpack_from(data[:structHeader.size]) if self.length > 0: self.payload = data[structHeader.size:(self.length + structHeader.size)] if self.binlen > 0: self.bindata = data[self.length + structHeader.size:(self.length + self.binlen + structHeader.size)] return [ self.direction, self.length, self.binlen, self.payload, self.bindata ]
def net_write_package(sock_conn, data_buffer, data_len): if sock_conn == None or data_buffer == None or data_len < 1: return -1 start_time = time.time() result = -1 total_byte = 0 n_send = 0 retry = 16 while total_byte < data_len and retry > 0: n_send = sock_conn.send(data_buffer[total_byte:]) if n_send == 0: log_print(LOG_LEVEL_DEBUG, 'Maybe the socket has closed.') result = 0 break if n_send < 0: log_print(LOG_LEVEL_DEBUG, 'write socket error.') break total_byte += n_send retry -= 1 log_print( LOG_LEVEL_DEBUG, 'net_write_package() end, cost time = %f s' % (time.time() - start_time)) if total_byte > 0: return total_byte else: return result
def run(self): log_print( LOG_LEVEL_DEBUG, 'CarAnalyseServerClinetThread(%d %s) handle client connetion start.' % (self.ident, self.name)) service_start_time = time.time() data_recv = bytes() n_recv, data_recv, offset = net_read_package(self.client_socket, data_recv) if n_recv > 0: req_start = time.time() req_pkg = AiNetworkPackage() (cmdReq, reqBusiLen, reqBusiData) = req_pkg.ParseFromString(data_recv[offset:]) log_print(LOG_LEVEL_DEBUG, 'Request: cmd=%d, BusiDataLen=%d' % (cmdReq, reqBusiLen)) log_print( LOG_LEVEL_DEBUG, 'request analyse cost time=%f s' % (time.time() - req_start)) if cmdReq == API_CMD_HEART_BEAT: log_print(LOG_LEVEL_DEBUG, 'Heart beat request process') sys.stdout.flush() timestamp = int(round(time.time())) load_score = 50 resp_pkg = HeartBeatPackage() (resp_buf, resp_buf_len) = resp_pkg.createResponse( flag=0, status=1, capacity=1.0, listen_port=bind_port, api_cmd=API_CMD_CAR_ANALYSE, load_score=load_score, ip=bind_ip, timestamp=timestamp) else: log_print(LOG_LEVEL_DEBUG, 'Business request process') task = CarAnalyseServerTask(self, reqBusiData, reqBusiLen) log_print(LOG_LEVEL_DEBUG, 'Now create a task...') self.queue.put(task) start_time = time.time() while self.sleep_counter > 0: if self.completed == True: break time.sleep(0.001) self.sleep_counter -= 1 cost_time = int(round(time.time() - start_time) * 1000) if self.completed == False: self.result = 1 log_print(LOG_LEVEL_DEBUG, 'Business process timeout') byte_start = time.time() resp_pkg = CarComparePackage() (resp_buf, resp_buf_len) = resp_pkg.createResponse( jsonData=self.json_data, processResult=self.result, costTime=cost_time) cur_time = time.time() log_print( LOG_LEVEL_DEBUG, 'Construct response cost time = %f s' % (cur_time - byte_start)) transmit_start = time.time() resp_pkg_buf, resp_pkg_len = node_generate_response_package( cmdReq, resp_buf, resp_buf_len) n_send = net_write_package(self.client_socket, resp_pkg_buf, resp_pkg_len) if n_send > 0: log_print( LOG_LEVEL_DEBUG, 'Send response ok. cost time = %f s' % (time.time() - transmit_start)) else: log_print( LOG_LEVEL_DEBUG, 'Send response failed. cost time = %f s' % (time.time() - transmit_start)) else: log_print(LOG_LEVEL_DEBUG, 'Receive request error') self.client_socket.close() service_end_time = time.time() log_print( LOG_LEVEL_DEBUG, 'service process cost time = %f s' % (service_end_time - service_start_time)) sys.stdout.flush()
def signal_handler(signum): log_print(LOG_LEVEL_DEBUG, 'Received signal: {0}'.format(signum)) time.sleep(1) busiWorker.stop() time.sleep(5) exit()
def net_business_communicate(ip_addr, port, cmd, busi_buf, in_buf_len): result = AI_SUCCESS sock_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock_conn.settimeout(30.0) server_addr = (ip_addr, port) try: sock_conn.connect(server_addr) log_print(LOG_LEVEL_DEBUG, "Connection to %s:%d success!" % (ip_addr, port)) except socket.timeout as e: log_print(LOG_LEVEL_ERROR, 'Connection to %s:%d failed' % (ip_addr, port)) return [AI_FAILURE, None] n_send = 0 n_recv = 0 try: (request_buf, req_buf_len) = node_generate_request_package(cmd, busi_buf, in_buf_len) if request_buf != None and req_buf_len > AI_PKG_FRONT_PART_SIZE: n_send = net_write_package(sock_conn, request_buf, req_buf_len) log_print(LOG_LEVEL_DEBUG, "net_write_package() %d bytes" % (n_send)) else: log_print(LOG_LEVEL_DEBUG, "Generate request error!") result = AI_FAILURE except socket.timeout: log_print(LOG_LEVEL_ERROR, "Send data socket timeout.") result = AI_FAILURE except socket.error as socketerror: log_print(LOG_LEVEL_ERROR, "Send data socket error. {0}".format(socketerror)) result = AI_FAILURE if result == AI_SUCCESS: try: resp_pkg = None offset = -1 data_recv = bytes() if n_send > 0: n_recv, data_recv, offset = net_read_package( sock_conn, data_recv) if n_recv > 0: resp_pkg = AiNetworkPackage() (cmdResp, respBusiLen, respBusiData) = resp_pkg.ParseFromString( data_recv[offset:]) log_print( LOG_LEVEL_DEBUG, "Response: cmd=%d, BusiDataLen=%d" % (cmdResp, respBusiLen)) else: log_print(LOG_LEVEL_DEBUG, "Receive response error") result = AI_FAILURE else: log_print( LOG_LEVEL_DEBUG, "Error: Send request to %s:%d error!" % (ip_addr, port)) result = AI_FAILURE except socket.timeout: log_print(LOG_LEVEL_ERROR, "Receive data socket timeout.") result = AI_FAILURE except socket.error as socketerror: log_print(LOG_LEVEL_ERROR, 'Receive to %s:%d failed' % (ip_addr, port)) return [AI_FAILURE, None] sock_conn.close() return [result, resp_pkg]