def __init__(self, target_server: Server, cmd: str=ServerCommand.Com_Start, target: str="", in_data: Serializable=None, out_data: []=None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.target = target self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile)
def __init__(self, target_server: Server, cmd: str=ServerCommand.Com_Start, in_data: Serializable=None, out_data: []=None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile)
class HostController(threading.Thread): def __init__(self, target_server: Server, cmd: str=ServerCommand.Com_Start, in_data: Serializable=None, out_data: []=None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile) def is_connection_ok(self): try: s = self.sock.get_connection() s.connect((self.server.address.address, self.server.address.port)) return True except Exception as e: print(e) return False def get_status(self): status_cmd = CommandStruct(cmd=ServerCommand.Com_Status) response = self.processor.check_response_ex(status_cmd) outdata = response[1].data if not response[0]: print("get status failed") elif not isinstance(outdata, MiningList): print("output data type is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) self.server.status = outdata def stop_mining(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Stop_Mining) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("send data failed") def clear_cashe(self): cmd = CommandStruct(cmd=ServerCommand.Com_Clear_Cache) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def add_slave(self): if self.in_data is None: print("input data is None") cmd = CommandStruct(cmd=ServerCommand.Com_Add_Slave, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: print("send data failed") def del_slave(self): if self.in_data is None: raise ValueError("input data is None") cmd = CommandStruct(cmd=ServerCommand.Com_Del_Slave, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def database_status(self): cmd = CommandStruct(cmd=ServerCommand.Com_DataBase_Status) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, MiningList): raise ValueError("Database data is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def get_db_data(self): cmd = CommandStruct(cmd=ServerCommand.Com_Get_DB_DATA, data=self.in_data) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, MiningList): raise ValueError("Database data is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def set_db_filter(self): cmd = CommandStruct(cmd=ServerCommand.Com_Set_DB_Filter, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def remove_db(self): cmd = CommandStruct(cmd=ServerCommand.Com_Remove_DB, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def get_progress(self): cmd = CommandStruct(cmd=ServerCommand.Com_Progress) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, PrograssData): raise ValueError("output value is Wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def setup_host(self): cmd = CommandStruct(cmd=ServerCommand.Com_Setup, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("setup host failed") def start_filtering(self): cmd = CommandStruct(cmd=ServerCommand.Com_Start_Filter, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("setup host failed") def add_seed(self): cmd = CommandStruct(cmd=ServerCommand.Com_Add_Seed, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("add seeds failed") def run(self): try: start_cmd = CommandStruct(ServerCommand.Com_Start) stop_cmd = CommandStruct(ServerCommand.Com_Stop) if self.is_connection_ok() and self.processor.check_response_ex(start_cmd)[0]: if self.cmd == ServerCommand.Com_Status: # setup and start minging self.get_status() elif self.cmd == ServerCommand.Com_Stop_Mining: self.stop_mining() elif self.cmd == ServerCommand.Com_Add_Slave: self.add_slave() elif self.cmd == ServerCommand.Com_Del_Slave: self.del_slave() elif self.cmd == ServerCommand.Com_DataBase_Status: self.database_status() elif self.cmd == ServerCommand.Com_Progress: self.get_progress() elif self.cmd == ServerCommand.Com_Setup: self.setup_host() elif self.cmd == ServerCommand.Com_Start_Filter: self.start_filtering() elif self.cmd == ServerCommand.Com_Set_DB_Filter: self.set_db_filter() elif self.cmd == ServerCommand.Com_Add_Seed: self.add_seed() elif self.cmd == ServerCommand.Com_Get_DB_DATA: self.get_db_data() elif self.cmd == ServerCommand.Com_Clear_Cache: self.clear_cashe() elif self.cmd == ServerCommand.Com_Remove_DB: self.remove_db() else: pass try: # the last call may have exception due to network connection problem self.processor.check_response_ex(stop_cmd) except: pass except Exception as ex: print(ex)
class AbstractClient(threading.Thread): def __init__(self, target_server: Server, cmd: str=ServerCommand.Com_Start, target: str="", in_data: Serializable=None, out_data: []=None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.target = target self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile) def is_connection_ok(self): try: s = self.sock.get_connection() if isinstance(self.server.address, dict): self.server.address = Serializable.get_deserialized(self.server.address) s.connect((self.server.address.address, self.server.address.port)) return True except Exception as e: print(e) return False @property def handler_map(self) -> dict: # override this map inner_map = {} return inner_map def prototype_return_data(self, cmd: str): get_data_cmd = CommandStruct(cmd=cmd, target=self.target, data=self.in_data) response = self.processor.check_response_ex(get_data_cmd) outdata = response[1].data if not response[0]: raise ValueError("prototype_return_data: get data failed") else: if isinstance(self.out_data, list): self.out_data.append(outdata) def prototype_one_way(self, cmd: str): data_cmd = CommandStruct(cmd=cmd, target=self.target, data=self.in_data) response = self.processor.check_response_ex(data_cmd) if not response[0]: raise ValueError("prototype_one_way: command failed") def get_status(self): status_cmd = CommandStruct(cmd=ServerCommand.Com_Status) response = self.processor.check_response_ex(status_cmd) response_data = response[1].data if isinstance(response_data, dict): response_data = Serializable.get_deserialized(response_data) if not response[0]: raise ValueError("get status failed") elif response_data is not None and isinstance(response_data, ServerStatus): if isinstance(self.out_data, list): self.out_data.append(response_data) self.server.status = response_data def run(self): try: handler = self.handler_map.get(self.cmd) if handler is not None: start_cmd = CommandStruct(ServerCommand.Com_Start) stop_cmd = CommandStruct(ServerCommand.Com_Stop) if self.is_connection_ok() and self.processor.check_response_ex(start_cmd)[0]: handler() try: # the last call may have exception due to network connection problem self.processor.check_response_ex(stop_cmd) except: pass except Exception as ex: print(ex) pass
class HostController(threading.Thread): def __init__(self, target_server: Server, cmd: str = ServerCommand.Com_Start, in_data: Serializable = None, out_data: [] = None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile) def is_connection_ok(self): try: s = self.sock.get_connection() s.connect((self.server.address.address, self.server.address.port)) return True except Exception as e: print(e) return False def get_status(self): status_cmd = CommandStruct(cmd=ServerCommand.Com_Status) response = self.processor.check_response_ex(status_cmd) outdata = response[1].data if not response[0]: print("get status failed") elif not isinstance(outdata, MiningList): print("output data type is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) self.server.status = outdata def stop_mining(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Stop_Mining) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("send data failed") def clear_cashe(self): cmd = CommandStruct(cmd=ServerCommand.Com_Clear_Cache) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def add_slave(self): if self.in_data is None: print("input data is None") cmd = CommandStruct(cmd=ServerCommand.Com_Add_Slave, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: print("send data failed") def del_slave(self): if self.in_data is None: raise ValueError("input data is None") cmd = CommandStruct(cmd=ServerCommand.Com_Del_Slave, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def database_status(self): cmd = CommandStruct(cmd=ServerCommand.Com_DataBase_Status) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, MiningList): raise ValueError("Database data is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def get_db_data(self): cmd = CommandStruct(cmd=ServerCommand.Com_Get_DB_DATA, data=self.in_data) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, MiningList): raise ValueError("Database data is wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def set_db_filter(self): cmd = CommandStruct(cmd=ServerCommand.Com_Set_DB_Filter, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def remove_db(self): cmd = CommandStruct(cmd=ServerCommand.Com_Remove_DB, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("send data failed") def get_progress(self): cmd = CommandStruct(cmd=ServerCommand.Com_Progress) response = self.processor.check_response_ex(cmd) outdata = response[1].data if not response[0]: raise ValueError("send data failed") elif not isinstance(outdata, PrograssData): raise ValueError("output value is Wrong") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def setup_host(self): cmd = CommandStruct(cmd=ServerCommand.Com_Setup, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("setup host failed") def start_filtering(self): cmd = CommandStruct(cmd=ServerCommand.Com_Start_Filter, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("setup host failed") def add_seed(self): cmd = CommandStruct(cmd=ServerCommand.Com_Add_Seed, data=self.in_data) response = self.processor.check_response_ex(cmd) if not response[0]: raise ValueError("add seeds failed") def run(self): try: start_cmd = CommandStruct(ServerCommand.Com_Start) stop_cmd = CommandStruct(ServerCommand.Com_Stop) if self.is_connection_ok() and self.processor.check_response_ex( start_cmd)[0]: if self.cmd == ServerCommand.Com_Status: # setup and start minging self.get_status() elif self.cmd == ServerCommand.Com_Stop_Mining: self.stop_mining() elif self.cmd == ServerCommand.Com_Add_Slave: self.add_slave() elif self.cmd == ServerCommand.Com_Del_Slave: self.del_slave() elif self.cmd == ServerCommand.Com_DataBase_Status: self.database_status() elif self.cmd == ServerCommand.Com_Progress: self.get_progress() elif self.cmd == ServerCommand.Com_Setup: self.setup_host() elif self.cmd == ServerCommand.Com_Start_Filter: self.start_filtering() elif self.cmd == ServerCommand.Com_Set_DB_Filter: self.set_db_filter() elif self.cmd == ServerCommand.Com_Add_Seed: self.add_seed() elif self.cmd == ServerCommand.Com_Get_DB_DATA: self.get_db_data() elif self.cmd == ServerCommand.Com_Clear_Cache: self.clear_cashe() elif self.cmd == ServerCommand.Com_Remove_DB: self.remove_db() else: pass try: # the last call may have exception due to network connection problem self.processor.check_response_ex(stop_cmd) except: pass except Exception as ex: print(ex)
class MiningController(threading.Thread): def __init__(self, target_server: Server, cmd: str=ServerCommand.Com_Start, in_data: Serializable=None, out_data: []=None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile) def is_connection_ok(self): try: s = self.sock.get_connection() if isinstance(self.server.address, dict): self.server.address = Serializable.get_deserialized(self.server.address) s.connect((self.server.address.address, self.server.address.port)) return True except Exception as e: print(e) return False def get_status(self): status_cmd = CommandStruct(cmd=ServerCommand.Com_Status) response = self.processor.check_response_ex(status_cmd) response_data = response[1].data if isinstance(response_data, dict): response_data = Serializable.get_deserialized(response_data) if not response[0]: raise ValueError("get status failed") elif response_data is not None and isinstance(response_data, ServerStatus): if isinstance(self.out_data, type([])): self.out_data.append(response_data) self.server.status = response_data def send_data(self): data_cmd = CommandStruct(cmd=ServerCommand.Com_Data, data=self.in_data) response = self.processor.check_response_ex(data_cmd) if not response[0]: raise ValueError("send data failed") def get_data(self): get_data_cmd = CommandStruct(cmd=ServerCommand.Com_Get_Data) response = self.processor.check_response_ex(get_data_cmd) outdata = response[1].data if not response[0]: raise ValueError("get data failed") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def stop_slave(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Stop_Mining) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("stop slave failed") def setup_slave(self): setup_cmd = CommandStruct(cmd=ServerCommand.Com_Setup, data=self.in_data) response = self.processor.check_response_ex(setup_cmd) if not response[0]: raise ValueError("send data failed") def clear_cache(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Clear_Cache) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("clear cache failed") def run(self): try: start_cmd = CommandStruct(ServerCommand.Com_Start) stop_cmd = CommandStruct(ServerCommand.Com_Stop) if self.is_connection_ok() and self.processor.check_response_ex(start_cmd)[0]: if self.cmd == ServerCommand.Com_Status: self.get_status() elif self.cmd == ServerCommand.Com_Data: self.send_data() elif self.cmd == ServerCommand.Com_Get_Data: self.get_data() elif self.cmd == ServerCommand.Com_Stop_Mining: self.stop_slave() elif self.cmd == ServerCommand.Com_Setup: self.setup_slave() elif self.cmd == ServerCommand.Com_Clear_Cache: self.clear_cache() else: pass try: # the last call may have exception due to network connection problem self.processor.check_response_ex(stop_cmd) except: pass except Exception as ex: print(ex) pass
class MiningController(threading.Thread): def __init__(self, target_server: Server, cmd: str = ServerCommand.Com_Start, in_data: Serializable = None, out_data: [] = None): threading.Thread.__init__(self) self.server = target_server self.cmd = cmd self.in_data = in_data self.out_data = out_data self.sock = StreamSocket() self.processor = CommandProcessor(self.sock.rfile, self.sock.wfile) def is_connection_ok(self): try: s = self.sock.get_connection() if isinstance(self.server.address, dict): self.server.address = Serializable.get_deserialized( self.server.address) s.connect((self.server.address.address, self.server.address.port)) return True except Exception as e: print(e) return False def get_status(self): status_cmd = CommandStruct(cmd=ServerCommand.Com_Status) response = self.processor.check_response_ex(status_cmd) response_data = response[1].data if isinstance(response_data, dict): response_data = Serializable.get_deserialized(response_data) if not response[0]: raise ValueError("get status failed") elif response_data is not None and isinstance(response_data, ServerStatus): if isinstance(self.out_data, type([])): self.out_data.append(response_data) self.server.status = response_data def send_data(self): data_cmd = CommandStruct(cmd=ServerCommand.Com_Data, data=self.in_data) response = self.processor.check_response_ex(data_cmd) if not response[0]: raise ValueError("send data failed") def get_data(self): get_data_cmd = CommandStruct(cmd=ServerCommand.Com_Get_Data) response = self.processor.check_response_ex(get_data_cmd) outdata = response[1].data if not response[0]: raise ValueError("get data failed") else: if isinstance(self.out_data, type([])): self.out_data.append(outdata) def stop_slave(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Stop_Mining) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("stop slave failed") def setup_slave(self): setup_cmd = CommandStruct(cmd=ServerCommand.Com_Setup, data=self.in_data) response = self.processor.check_response_ex(setup_cmd) if not response[0]: raise ValueError("send data failed") def clear_cache(self): stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Clear_Cache) response = self.processor.check_response_ex(stop_mining_cmd) if not response[0]: raise ValueError("clear cache failed") def run(self): try: start_cmd = CommandStruct(ServerCommand.Com_Start) stop_cmd = CommandStruct(ServerCommand.Com_Stop) if self.is_connection_ok() and self.processor.check_response_ex( start_cmd)[0]: if self.cmd == ServerCommand.Com_Status: self.get_status() elif self.cmd == ServerCommand.Com_Data: self.send_data() elif self.cmd == ServerCommand.Com_Get_Data: self.get_data() elif self.cmd == ServerCommand.Com_Stop_Mining: self.stop_slave() elif self.cmd == ServerCommand.Com_Setup: self.setup_slave() elif self.cmd == ServerCommand.Com_Clear_Cache: self.clear_cache() else: pass try: # the last call may have exception due to network connection problem self.processor.check_response_ex(stop_cmd) except: pass except Exception as ex: print(ex) pass