def recieving_func(self) -> None: this_thread = thrdg.currentThread() while getattr(this_thread, 'run', True): try: data, addr = self.socket.recvfrom(1024) except: continue if data: msg = Header.parse_message(repr(data)) # Jeśli wiadomość to potwierdzenie drukujemy # informację i nie dodajemy jej do kolejki if msg.status == Status.RECIEVED: print('Server recieved request') else: if msg.operation == Operation.SORT_A or msg.operation == Operation.SORT_D: print(msg.a) else: self.print_result(msg) # Potwierdzamy odbiór komunikatu self.messages_to_send.put( Header.create_reply(msg.operation, Status.OK, self.SESSION_ID))
def init_connection(self) -> bool: print('Waiting for server connection') while True: # Wysyłamy prośbę o utworzenie sesji msg = Header.create_reply(operation=Operation.CONNECTING, status=Status.NONE, session_id=self.SESSION_ID) self.socket.sendto(msg, self.SERVER_ADDR) time.sleep(0.5) # Czekamy na potwierdzenie serwera lub informację o zajętości while True: try: msg, addr = self.socket.recvfrom(1024) msg = Header.parse_message(repr(msg)) if msg.status == Status.OK: self.SESSION_ID = msg.id print('Connected to server') return True elif msg.status == Status.BUSY: print( 'Server is busy, cannot connect right now\nExiting client app' ) return False except: break
def collect_sortD_request(self) -> None: print( "Enter next number to sort, if it's the last one, add '-' after the number (ex. '3-'):" ) while True: num = input() if not num: print('Submitted empty field, try again') continue try: if num[-1] == '-': int(num[:-1]) self.messages_to_send.put( Header.create_reply(Operation.SORT_D, Status.LAST, self.SESSION_ID, num[:-1])) return else: int(num) self.messages_to_send.put( Header.create_reply(Operation.SORT_D, Status.SENDING, self.SESSION_ID, num)) except: print('Not a number, try again') continue
def parse_request(self, message: str, addr: tuple) -> None: request = Header.parse_message(message) # Jeśli komunikat jest niezgodny z protokołem nie obsługujemy go if not request.operation == Status.ERROR: if request.status == Status.OK: print("Client of session {} recieved the message (op={})".format( request.id, request.operation)) return # Jeśli klient kończy sesję obsługujemy resztę jego żądań i usuwamy sesje elif request.operation == Operation.DISCONNECTING and self.session and self.session.session_id == request.id: while not self.session.request_queue.empty(): time.sleep(0.1) print('Client of session {} is disconnected'.format(request.id)) self.session = None self.sending_queue.put( (self.std_server_response(request), addr)) self.counter[1] += 1 return else: print('Recieved from: {}, through port: {} (id={})'.format( addr[0], addr[1], request.id)) # Potwierdzamy odbiór poprawnego komunikatu self.sending_queue.put( (self.std_server_response(request), addr)) # Nowym klientom tworzymy sesję if not self.session and request.operation == Operation.CONNECTING and request.id == Status.NONE: request.id = str(randint(0, 99999)).rjust(5, "0") new_session = Session(request.id, addr) new_session.request_queue.put(request) self.session = new_session # Żądania od obsługiwanego klienta umieszczcamy do przetworzenia elif self.session and self.session.session_id == request.id: self.session.request_queue.put(request) # Klientom próbującym dołączyć podczas trwania innej sesji odsyłamy błąd else: print( 'Another client (id={}) tried to connect, refusing'.format(request.id)) self.sending_queue.put((Header.create_reply( status=Status.BUSY, session_id=Status.ERROR), addr)) return # Odsyłamy informację o niepoprawnym komunikacie else: self.sending_queue.put((self.std_server_response(request), addr))
def disconnect(self) -> None: self.messages_to_send.put( Header.create_reply(operation=Operation.DISCONNECTING, status=Status.NONE, session_id=self.SESSION_ID)) print('Quitting session, please wait') time.sleep(1)
def std_server_response(self, request: Header) -> bytes: return Header.create_reply(operation=request.operation, status=Status.RECIEVED, session_id=request.id)
def process_arguments(self) -> None: # Operacje wywoływane jako argumenty programu realizujemy od razu if self.arguments.add: req = Header(o=Operation.ADD, s=Status.NONE, i=self.SESSION_ID, t=Header.create_timestamp(), a=str(self.arguments.add[0]), b=str(self.arguments.add[1])) self.requests.append(req) self.messages_to_send.put(req.to_send()) self.arguments.add = None if self.arguments.multiply: req = Header(o=Operation.MULTIPLY, s=Status.NONE, i=self.SESSION_ID, t=Header.create_timestamp(), a=str(self.arguments.multiply[0]), b=str(self.arguments.multiply[1])) self.requests.append(req) self.messages_to_send.put(req.to_send()) self.arguments.multiply = None if self.arguments.random: req = Header(o=Operation.RANDOM, s=Status.NONE, i=self.SESSION_ID, t=Header.create_timestamp(), a=str(self.arguments.random[0]), b=str(self.arguments.random[1])) self.requests.append(req) self.messages_to_send.put(req.to_send()) self.arguments.random = None if self.arguments.modulo: req = Header(o=Operation.MODULO, s=Status.NONE, i=self.SESSION_ID, t=Header.create_timestamp(), a=str(self.arguments.modulo[0]), b=str(self.arguments.modulo[1])) self.requests.append(req) self.messages_to_send.put(req.to_send()) self.arguments.modulo = None # Sortowanie przetwarzamy w pętli wewnątrz programu if self.arguments.sortA: self.collect_sortA_request() elif self.arguments.sortD: self.collect_sortD_request()