def __manager_msg__(self): while True: try: data = self.queue_in.get(True) #print_debug('接收到 发送到客户端的队列消息',data[:20]) if data[:20] == message.HEADER_HEART: try: json_data = message.msg_decode(data,message.HEADER_HEART) if not json_data: continue self.addr_client = tuple(json_data['user_info'].get('addr_client'))#(json_data['user_info'].get('addr_client'),json_data['user_info'].get('client_port')) print_success('客户端信息信息更新成功',self.hostname,self.addr_client) # 本地端口也发送给对端 self.__send_server_port__() except Exception as ex: print_warnning('客户端进程处理登录消息异常',ex) continue if not self.online: continue self.queue_to_client.put(data,True,1) except Exception as ex: print_error('客户端进程处理管理进程消息异常',ex) counter(server_log,'客户端进程处理管理进程消息异常', ex)
def listen(self): """read stream from Kafka and append to streamqueue Returns: list of list: dataset (nchannel x nsample) or None """ # If chunk size is too large, consume it multiple epochs chunk_size = self.__num_msgs msgs = [] while chunk_size > 100: msgs.extend(self.__consumer.consume(num_messages=100, timeout=1)) chunk_size -= 100 msgs.extend(self.__consumer.consume(num_messages=chunk_size, timeout=1)) print(f"INFO: Received {str(len(msgs))} messages" ) if self.__verbose else None if msgs is None or len(msgs) <= 0: return None self.__streamqueue.extendleft(msgs) # Enqueue if len(self.__streamqueue) < self.__num_msgs: return None # Dequeue msgs__ = [self.__streamqueue.pop() for i in range(0, self.__num_msgs)] timestamps, data = [], [] for msg in msgs__: time, values = msg_decode(msg.value()) timestamps.append(time) if time is not None else None data.append(values) if time is not None else None #TODO:// assert there is not big time gap in the data if len(data) < self.__num_msgs: return None print(timestamps[0], data[0]) if self.__verbose else None data = tuple(zip(*data)) self.data.append(data) self.timestamps.append(timestamps[0]) print(f"INFO: Sucessfully Read a chunk") if self.__verbose else None
def __udp_recv__(self): print_success('服务器端接收进程启动',self.hostname,self.server_recv_port) while True: try: # 验证码图片有300k吧 data,addr_client = self.udp_recv.recvfrom(300 * 1024) try: self.udp_recv.sendto('ACK'.encode(),addr_client) except Exception as ex: print_error('服务器接收进程响应ACK异常',self.hostname, ex) # 心跳消息,客户端软件重启等场景下,IP和端口可能需要更新 if data[:20] == message.HEADER_HEART: try: json_data = message.msg_decode(data,message.HEADER_HEART) if not json_data: continue #主要是更新IP self.addr_client = addr_client self.__send_server_port__() continue except Exception as ex: print_warnning('客户端进程处理登录消息异常',ex) continue # 如果IP变化,直接更新 elif addr_client[0] != self.addr_client[0]: self.addr_client = (addr_client[0],self.addr_client[1]) # 所有消息透传到处理进程 self.queue_from_client.put(data, True, 3) #服务器保持端口监听,不下线 except Exception as ex: counter(server_log, 'ERROR_RECV', self.hostname, ex) print_warnning('客户端接收进程异常',self.hostname,ex) time.sleep(2) continue
def __get_server_port__(self,data): try: json_temp = message.msg_decode(data, message.HEADER_HEART) if not json_temp: print_error('登录服务器响应消息非法', data[:20]) return False except Exception as ex: print_error('登录服务器响应消息异常',ex) return False try: # 获取服务器端口 self.server_recv_port = int(json_temp['user_info']['server_recv_port']) counter(self.log_file, 'PORT_SUCCESS', self.server_recv_port) print_success('登录服务器监听端口初始化成功', self.server_recv_port) return True except Exception as ex: counter(self.log_file, 'PORT_ERROR', ex) print_error('登录服务器,服务器响应成功,但解析消息异常', ex) return False
def __login_handle__(self): try: udp = socket(AF_INET, SOCK_DGRAM) print_success('服务器客户端管理进程启动', self.port) udp.bind(('', self.port)) except Exception as ex: print_error('服务器客户端管理进程启动异常,请重启',self.port) return while True: try: data, addr_client = udp.recvfrom(5*1024) udp.sendto('ACK'.encode(),addr_client) except Exception as ex: counter(server_log,'CLIENT_MANAGER', 'RECV_DATA', 'ERROR', ex) try: json_data = message.msg_decode(data, message.HEADER_HEART) if not json_data: counter(server_log, 'CLIENT_MANAGER', 'LOAD_DATA', 'FAIL', data[:20]) print_warnning('服务器客户端管理进程接收到异常消息',data[0:20]) continue hostname = json_data['user_info'].get('hostname', '') user_type = json_data['user_info'].get('user_type', '') except Exception as ex: counter(server_log, 'CLIENT_MANAGER', 'LOAD_DATA', 'ERROR', ex) continue #实体已经创立 if hostname in self.hosts: try: #IP添加进去 json_data['user_info']['addr_client'] = addr_client self.hosts[hostname]['queue_in'].put(message.msg_encode(json_data,message.HEADER_HEART)) print_infor('客户端更新', hostname, user_type, addr_client ) except Exception as ex: print_error('客户端更新异常',hostname,ex) counter(server_log, 'CLIENT_MANAGER', 'LOGIN_REFRESH_ERROR', hostname, ex) #新建实体 else: # 保存客户端登录信息 self.hosts[hostname] = { 'hostname': hostname, 'user_type': user_type, 'queue_in':Queue(100), #输入队列 'queue_out':Queue(100), #输出队列 'status':'offline', 'cxtj':'' } try: self.hosts[hostname]['listen'] = Process(target=__client_start__, args=(self.hosts[hostname]['queue_in'],self.hosts[hostname]['queue_out'], hostname,user_type,addr_client,)) self.hosts[hostname]['listen'].start() print_infor('客户端登录',hostname,user_type,addr_client) #counter(server_log, 'CLIENT_MANAGER', 'LOGIN', hostname,user_type,addr_client) except Exception as ex: print_infor('客户端登录异常', hostname, ex) counter(server_log, 'CLIENT_MANAGER', 'LOGIN', hostname, ex) if user_type != client.UI.value: try: # 广播给所有的UI更新状态 self.__send_type__(message.msg_encode(json_data, message.HEADER_NULL), client.UI.value) except Exception as ex: print_warnning('客户端登录状态通知到UI界面发生异常,可能会影响任务管理',ex)