class Server(): def __init__(self): self.clients = [] self.jim = JimProtocol() self.clients = [] self.who_reads = [] self.who_writes = [] self.e = [] def presence_answer(self, presence_message): if 'action' in presence_message and \ presence_message['action'] == 'presence': return {'response': 200} else: return {'response': 400, 'error': 'Не верный запрос'} def main_loop_in_server(self): while True: try: sock, addr = serv_sock.accept() except OSError as e: pass else: self.clients.append(sock) presence = self.jim.recive_msg(sock) #print(presence.get('user')) repo.add_client(presence.get('user')) answer = self.presence_answer(presence) self.jim.send_msg(sock, answer) print('Client with ip {}'.format(addr)) finally: self.who_writes, self.who_reads, self.e = select.select( self.clients, self.clients, self.clients, 0) for who_write in self.who_writes: try: msg = who_write.recv(1024) for who_read in self.who_reads: try: who_read.send(msg) except: self.clients.remove(who_read) print(msg.decode()) message = self.jim.recive_msg(who_write) print(message) except: self.clients.remove(who_write)
class ClientWrite: def __init__(self, sock, name): self.jim = JimProtocol() self.name = name self.sock = sock def send_presence(self, name): presence = self.jim.create_presence(name) self.jim.send_msg(self.sock, presence) print(self.jim.recive_msg(self.sock)) self.main_loop_in_client() def main_loop_in_client(self): while True: msg = input('Введите текст: ') json_msg = self.jim.create_msg(self.name, msg) self.jim.send_msg(sock, json_msg)
class Client: def __init__(self, name): self.jim = JimProtocol() self.name = name self.request_queue = Queue() def action(self, msg): if 'action' in msg and \ msg['action'] == 'msg': msg = '{} {} send message to all: {}'\ .format(time.ctime(), msg.get('from'), msg.get('message')) return msg elif 'action' in msg and \ msg['action'] == 'presence': msg = 'console', msg return msg elif 'action' in msg and \ msg['action'] == 'setClients': clients = msg.get('clients') self.request_queue.put(clients) elif 'action' in msg and \ msg['action'] == 'setContacts': contacts = msg.get('contacts') self.request_queue.put(contacts) elif 'action' in msg and \ msg['action'] == 'msg_to_user': from_name = msg.get('from') message = msg.get('message') msg = '{}: {}'.format(from_name, message) return msg def connect_to_server(self): self.sock = socket.socket() self.sock.connect(('', 7778)) self.send_presence(self.name) def send_presence(self, name): presence = self.jim.create_presence(name) self.jim.send_msg(self.sock, presence) t = Thread(target=self.read_loop) t.daemon = True t.start() def send_msg(self, msg): json_msg = self.jim.create_msg(self.name, msg) self.jim.send_msg(self.sock, json_msg) def read_loop(self): while True: msg = self.jim.recive_msg(self.sock) msg = self.action(msg) print(msg) def add_contact(self, client_name): msg = self.jim.join_to_chat(self.name, client_name) self.jim.send_msg(self.sock, msg) def del_contact(self, client_name): msg = self.jim.del_contact(self.name, client_name) self.jim.send_msg(self.sock, msg) def remove_client_online(self, client_name): msg = self.jim.remove_client(client_name) self.jim.send_msg(self.sock, msg) def create_client(self, client_name): msg = self.jim.new_client(client_name) self.jim.send_msg(self.sock, msg) def get_clients(self): msg = self.jim.get_clients() self.jim.send_msg(self.sock, msg) msg = self.request_queue.get() return msg def get_contacts(self, name): msg = self.jim.get_contacts(name) self.jim.send_msg(self.sock, msg) msg = self.request_queue.get() return msg def send_private_msg(self, from_name, to_name, message): msg = self.jim.msg_private(from_name, to_name, message) self.jim.send_msg(self.sock, msg)
class Server(): def __init__(self): self.clients = [] self.jim = JimProtocol() self.clients = [] self.who_reads = [] self.who_writes = [] self.e = [] self.repo = Storage(session) self.from_name = '' self.user_and_sock = {} def action(self, msg): if 'action' in msg and \ msg['action'] == 'msg': print(msg) print('msg') # name = msg.get('from') # message = msg.get('message') answer_msg = '{} {} send message to all: {}'\ .format(time.ctime(), msg.get('from'), msg.get('message')) self.from_name = msg.get('from') return msg elif 'action' in msg and msg['action'] == 'join': client_name = msg.get('from') contact_name = msg.get('name') self.repo.add_contact(client_name, contact_name) print('user: {} add to {}'.format(client_name, contact_name)) return None elif 'action' in msg and msg['action'] == 'leave': client_name = msg.get('client') contact_name = msg.get('contact') self.repo.del_contact(client_name, contact_name) print('client: {} del {} from friend list'.format( client_name, contact_name)) return None elif 'action' in msg and msg['action'] == 'remove': client_name = msg.get('client') self.repo.remove_client(client_name) print('{} is removed'.format(client_name)) return None elif 'action' in msg and msg['action'] == 'create': client_name = msg.get('client') self.repo.add_client(client_name) print('New client: {}!'.format(client_name)) return None elif 'action' in msg and msg['action'] == 'getClients': # clients = self.user_and_sock.keys() # print(type(clients)) # clients = self.repo.get_clients() # clients_array = [] # for client in clients: # client_str = str(client) # client_str.join() # client_str = client_str[10:-3] # clients_array.append(client_str) # msg = self.jim.set_clients(clients) str = ' '.join(self.clients) print(str) msg = self.jim.set_clients(str) return msg # return clients_array elif 'action' in msg and msg['action'] == 'getContacts': contact = self.repo.get_contacts(msg.get('client_name')) print(contact) elif 'action' in msg and msg['action'] == 'msg_to_user': pass else: return None def reads_msg(self, w_clients, all_clients): for sock in w_clients: try: jmsg = self.jim.recive_msg(sock) msg = self.action(jmsg) print('1') if msg is not None: self.write_clients(msg, self.who_reads, self.clients) except: sock.cloce() all_clients.remove(sock) def write_clients(self, msgs, r_clients, all_clients): for sock in r_clients: try: print('3') msg = self.jim.create_answer_msg('msg', self.from_name, msgs) self.jim.send_msg(sock, msgs) print(msgs) except: sock.cloce() all_clients.remove(sock) # for msg in msgs: # try: # self.jim.send_msg(sock, msg) # print(msg) # except: # sock.cloce() # all_clients.remove(sock) def presence_answer(self, presence_message): if 'action' in presence_message and \ presence_message['action'] == 'presence': answer = self.jim.presence_answer({'response': 200}) return answer else: answer = self.jim.presence_answer({ 'response': 400, 'error': 'Не верный запрос' }) return answer def main_loop_in_server(self): while True: try: sock, addr = serv_sock.accept() presence = self.jim.recive_msg(sock) self.repo.add_client(presence.get('user')) self.user_and_sock[presence.get('user')] = sock self.clients.append(presence.get('user')) print(self.user_and_sock) answer = self.presence_answer(presence) self.jim.send_msg(sock, answer) except OSError as e: pass else: # self.clients.append(sock) # presence = self.jim.recive_msg(sock) # # #print(presence.get('user')) # repo.add_client(presence.get('user')) # answer = self.presence_answer(presence) # self.jim.send_msg(sock, answer) print('Client with ip {}'.format(addr)) finally: # wait = 0 # self.who_writes = [] # self.who_reads = [] try: self.who_writes, \ self.who_reads,\ self.e = \ select.select(self.user_and_sock.values(), self.user_and_sock.values(), self.user_and_sock.values(), 0) self.reads_msg(self.who_writes, self.clients) # self.write_clients(msg, self.who_reads, self.clients) except: pass
class Client: def __init__(self, name): self.jim = JimProtocol() self.name = name self.clients_online = [] # self.client_chat = ClientChat() #self.sock = sock def action(self, msg): if 'action' in msg and \ msg['action'] == 'msg': msg = '{} {} send message to all: {}'\ .format(time.ctime(), msg.get('from'), msg.get('message')) # print(msg) return msg elif 'action' in msg and \ msg['action'] == 'presence': msg = 'console', msg return msg elif 'action' in msg and \ msg['action'] == 'setClients': clients = msg.get('clients') # clients = clients.split(',') print(type(clients), clients) self.clients_online = clients list = clients.split(' ') self.clients_online = list return list def connect_to_server(self): self.sock = socket.socket() self.sock.connect(('', 7778)) self.send_presence(self.name) def send_presence(self, name): presence = self.jim.create_presence(name) self.jim.send_msg(self.sock, presence) t = Thread(target=self.read_loop) t.daemon = True t.start() def send_msg(self, msg): json_msg = self.jim.create_msg(self.name, msg) self.jim.send_msg(self.sock, json_msg) def read_loop(self): while True: # buf = self.sock.recv(1024) # msg = buf.decode() msg = self.jim.recive_msg(self.sock) msg = self.action(msg) print(msg) def add_contact(self, client_name): msg = self.jim.join_to_chat(self.name, client_name) self.jim.send_msg(self.sock, msg) def del_contact(self, client_name): msg = self.jim.del_contact(self.name, client_name) self.jim.send_msg(self.sock, msg) def remove_client_online(self, client_name): msg = self.jim.remove_client(client_name) self.jim.send_msg(self.sock, msg) def create_client(self, client_name): msg = self.jim.new_client(client_name) self.jim.send_msg(self.sock, msg) def get_clients(self): msg = self.jim.get_clients() self.jim.send_msg(self.sock, msg) def get_contact(self): msg = self.jim.get_contact(self.name) self.jim.send_msg(self.sock, msg) # client = Client(name='Kolya') # client.connect_to_server()