def users_list_request(sock, client_name): message = { ACTION: GET_ALL_USERS, TIME: time.time(), SENDER: client_name } log.debug(f'{client_name}: users_list_request created') send_message(sock, message) log.debug(f'{client_name}: users_list_request send')
def click_send(self): # отправка сообщений из поля ввода текста и его очищение contact = self.list_contacts.currentItem().text() text_message = self.text_new_msg.toPlainText() if text_message: create_message(self.sock, self.database, self.client_name, contact, text_message) self.text_new_msg.clear() # self.item_clicked_event(self.database.get_history(self.list_contacts.currentItem().text())) else: log.warning('Attempting to send an empty message')
def remove_contact_request(sock, client_name, remove_account): message = { ACTION: REMOVE_CONTACT, TIME: time.time(), SENDER: client_name, ACCOUNT_NAME: remove_account } log.debug(f'{client_name}: remove_contact_request created') send_message(sock, message) log.debug(f'{client_name}: remove_contact_request send')
def add_contact_request(sock, client_name, new_contact): message = { ACTION: ADD_CONTACT, TIME: time.time(), SENDER: client_name, ACCOUNT_NAME: new_contact } log.debug(f'{client_name}: add_contact_request created') send_message(sock, message) time.sleep(0.5) log.debug(f'{client_name}: add_contact_request send')
def accept(self, request): self.client_name = self.line_login.text() self.password = self.line_password.text() self.sock = socket(AF_INET, SOCK_STREAM) self.sock.connect((self.server_host, self.server_port)) # self.keys = get_keys(self.client_name) # print('keys', self.keys) database = ClientDB(self.client_name) # Отправляем приветствие и получаем ответ от сервера ans = presence_request(self.sock, self.client_name, self.password, request) log.debug(f'answer to PRESENCE: {ans}') if ans[ACTION] == PRESENCE: if ans[RESPONSE] == 200: # запрашиваем список контактов у сервера и добавляем их в свою базу данных contacts, msgs_history = contacts_list_request( self.sock, self.client_name, GET_USERS_PACKAGE) database.fill_contacts(contacts) # print(msgs_history) database.fill_messages_history(msgs_history) self.start_chat(database) else: self.label_error.setText(ans[ERROR]) elif ans[ACTION] == REGISTRATION: if ans[RESPONSE] == 200: # TODO win 'reg successful' print('reg++++') self.accept(PRESENCE) elif ans[RESPONSE] != 200: self.label_error.setText(ans[ERROR]) log.error(ans[ERROR])
def contacts_list_request(sock, client_name, request): message = {ACTION: request, TIME: time.time(), SENDER: client_name} log.debug(f'{client_name}: contacts_list_request created') send_message(sock, message) log.debug(f'{client_name}: contacts_list_request send') ans = get_message(sock) contacts = ans[CONTACTS] log.debug(f'For contacts_list_request get answer: {ans}') msgs_history = ans[MESSAGE_HISTORY] return contacts, msgs_history
def create_message(sock, database, acc_name, to_user, message): """ Отправка сообщения пользователю """ msg_id = database.save_message(to_user, 'out', message) log.debug(f'Save message to {to_user} to database') message = { ACTION: MESSAGE, TIME: time.time(), SENDER: acc_name, DESTINATION: to_user, MESSAGE_TEXT: message, MESSAGE_ID: msg_id } log.debug(f'Create message to {to_user}: {message}') send_message(sock, message) log.debug(f'Send message to {to_user}')
def presence_request(sock, client_name, password, request): """ сформировать presence-сообщение, отправить его и получить ответ от сервера """ presence = { ACTION: request, TIME: time.time(), USER: { SENDER: client_name, PASSWORD: password } } log.debug(f'{client_name}: Presence message created') send_message(sock, presence) log.debug(f'{client_name}: Presence message send') ans = get_message(sock) log.debug(f'For presence get answer: {ans}') return ans
def message_from_server(self): """ Обработка сообщений от сервера и пользователей """ while True: message = get_message(self.sock) try: # ------------------------ Разбор сообщений от сервера ------------------------ # if (RESPONSE and SENDER) in message \ and message[SENDER] == SERVER: if message[ACTION] == GET_ALL_USERS and message[RESPONSE] == 200: ClientApp.all_users = message[ALL_USERS] elif message[ACTION] == ADD_CONTACT and message[RESPONSE] == 200: self.database.add_contact(message[ACCOUNT_NAME]) self.fill_contacts() elif message[ACTION] == REMOVE_CONTACT and message[RESPONSE] == 200: self.database.del_contact(message[ACCOUNT_NAME]) self.fill_contacts() elif message[ACTION] == MESSAGE and message[RESPONSE] == 200: self.database.save_message_add_date(message[MESSAGE_ID], datetime.datetime.strptime(message[MESSAGE_DATETIME], '%Y-%m-%d %H:%M:%S.%f')) self.item_clicked_event(self.database.get_history(self.list_contacts.currentItem().text())) # ------------------------ Разбор сообщений от других пользователей ------------------------ # elif (ACTION and SENDER and DESTINATION and MESSAGE_TEXT) in message \ and message[ACTION] == MESSAGE \ and message[DESTINATION] == self.client_name: msg = f'\nGet message from user {message[SENDER]}: {message[MESSAGE_TEXT]}' self.database.save_message(message[SENDER], 'in', message[MESSAGE_TEXT], datetime.datetime.strptime(message[TIME], '%Y-%m-%d %H:%M:%S.%f')) log.info(msg) if self.list_contacts.currentItem() \ and self.list_contacts.currentItem().text() == message[SENDER]: self.item_clicked_event(self.database.get_history(self.list_contacts.currentItem().text())) else: log.error(f'Invalid message received from server: {message}') except KeyError: log.error(KeyError)