def accept_message(self): length = self._socket.recv(mp.length_size).decode() if length == '': length = 0 else: length = int(length) str_msg = self._socket.recv(length).decode() message = mp.parse_message(str_msg) logging.debug(f'{self._name} recieved - {message}') logging.debug(f'Raw: - {message.get_raw()}') return message
def kill_all(self): for client in self.__clients.values(): client.send_message(MessageParser.pack_kill_request()) client._disconnect() self.__clients = {}
def send_target(self, client_id: int, hash_target: str): self.__send_message_to_client( client_id, MessageParser.pack_target_setting(hash_target))
def assign_hash_range(self, client_id: int, hash_range: list): self.__send_message_to_client( client_id, MessageParser.pack_hash_assignment(hash_range))
def request_performance(self, client_id: int): self.__send_message_to_client(client_id, MessageParser.pack_performance_request())
def handle_client(self, client_id): client_name = self.__server_communicator.get_name(client_id) if self.__server_communicator.client_exists(client_id): self.__server_communicator.send_target(client_id, self.__hash_target) while self.__hash_result == '' and self.__server_communicator.client_exists( client_id): try: if self.__server_communicator.client_exists(client_id): self.__server_communicator.request_performance( client_id) msg = self.__server_communicator.get_client( client_id).accept_message() client_performance = MessageParser.cpu_performance_from_message( msg) else: break self.__cpu_performances.append(client_performance) capacity = self.get_client_capacity(client_performance) if capacity > 0: self.__queued_ranges[client_id] = [] for i in range(capacity): hash_range = self.__ranges_available.pop() if self.__server_communicator.client_exists( client_id): self.__server_communicator.assign_hash_range( client_id, hash_range) else: break logging.info( f'Gave range {hash_range} to {client_name}') self.__queued_ranges[client_id].append(hash_range) while len(self.__queued_ranges[client_id] ) > 0 and self.__hash_result == '': # time.sleep(self.__CLIENT_POLL_INTERVAL) if self.__server_communicator.client_exists( client_id): # self.__server_communicator.send_target(client_id, self.__hash_target) msg = self.__server_communicator.get_client( client_id).accept_message() else: break if msg.get_type() == MessageType.REPORT_RESULT: hash_range = range( int(msg.get_args()['range_start']), int(msg.get_args()['range_end']) + 1) hash_result = str( msg.get_args()['hash_result']) if hash_result != '': self.__hash_result = hash_result self.__server_communicator.kill_all() logging.info( f'Found key - {self.__hash_result}') break else: for qrange in self.__queued_ranges[ client_id]: if min(qrange) == min( hash_range) and max( qrange) == max(hash_range): self.__queued_ranges[ client_id].remove(qrange) logging.info( f'Range {qrange} is now done.') else: raise Exception('Scrambled sequence') except: break if self.__hash_result == '': logging.info(f'{client_name} crashed.') if client_id in self.__queued_ranges.keys(): for qrange in self.__queued_ranges[client_id]: self.__ranges_available.append(qrange) logging.info( f'Adding range back to available ranges - {qrange}') if client_id in self.__queued_ranges.keys(): self.__queued_ranges.pop(client_id) if client_id in range(len(self.__cpu_performances)): self.__cpu_performances.pop(client_id) if self.__server_communicator.client_exists(client_id): self.__server_communicator.remove_client(client_id)
def report_hash_result(self, hash_range, hash_result): self.send_message( MessageParser.pack_hash_result_report(hash_range, hash_result))
def report_performance(self, CPUPerformance): self.send_message( MessageParser.pack_performance_report(CPUPerformance))
print('Testing non-listed analysis:') pc = PerformanceCalculator(16, 4600, 0.0) non_listed_result = pc.analyze_cpu_performance(debug=True) assert non_listed_result.cores == 1.0 assert non_listed_result.frequency == 1.0 assert non_listed_result.utilization == 1.0 print('\nConclusion:') print(non_listed_result) print('Non-listed conclusion passed.\n\n') print('Testing listed analysis:') listed_result = pc.analyze_cpu_performance + analyze_cpu_performance_relative( [4, 5, 6, 16, 9, 7, 8], [2400, 2200, 4600, 4000], debug=True) assert listed_result.cores == 1.0 assert listed_result.frequency == 1.0 assert listed_result.utilization == 1.0 print('\nConclusion:') print(listed_result) print('Listed conclusion passed.') else: pc = PerformanceCalculator() non_listed_result = pc.analyze_cpu_performance() m = Message(MessageType.REPORT_PERFORMANCE, '', { 'cores': 4, 'utilization': 4, 'frequency': 4 }) print(f'\n\nTranslating :\n{m}\n\nto CPUPerformance:') print(MessageParser.cpu_performance_from_message(m))