@@ -30,18 +30,31 @@ def val_nonce(transacao, nonce, bits_zero):
3030
3131
3232def atender_cliente (conn , addr ):
33- name = conn .recv (10 ).decode .strip () # Recebe o nome do cliente
33+
34+ b_name = conn .recv (10 ) # Recebe o nome do cliente
35+ name = b_name .decode ('utf-8' )
3436 print (f'Cliente { name } conectado em { addr } ' )
3537
3638 # Registro do nome do cliente ao dicionário clientes e o registro da sua última atividade(tempo), monitorando clientes inativos
3739 clientes [name ] = {'conexao' : conn , 'ultima_atividade' : time .time ()}
3840 try :
3941 while True :
40- msg = conn .recv (4096 ). decode ()
42+ msg = conn .recv (4096 )
4143 if not msg :
4244 break
4345
44- if msg == 'G' : # Requisição de transação
46+ t_msg = msg [0 ]
47+ if t_msg == ord ('T' ): # Tipo da mensagem
48+ numTransacao = int .from_bytes (msg [1 :3 ], byteorder = 'big' )
49+ numCliente = int .from_bytes (msg [3 :5 ], byteorder = 'big' )
50+ tamJanela = int .fromm_bytes (msg [5 :9 ], byteorder = 'big' )
51+ bitZero = msg [9 ]
52+ tamTransacao = int .from_bytes (msg [10 :14 ], byteorder = 'big' )
53+ transacao = msg [14 :14 + tamTransacao ]
54+ break
55+
56+
57+ elif msg == b'G' : # Requisição de transação
4558 if clientes_pendentes :
4659 transacao , bits_zero = clientes_pendentes [0 ] # Extraindo a primeira transaçção e o valor de bits zero
4760 n_transacao = 1 # ID da transação
@@ -53,18 +66,18 @@ def atender_cliente(conn, addr):
5366 resposta .extend (n_clientes .to_bytes (2 , byteorder = 'big' )) # 2 bytes para quantidade de clientes
5467 resposta .extend (WINDOW_SIZE .to_bytes (4 , byteorder = 'big' )) # 4 bytes para o tamanho da janela
5568 resposta .extend (bits_zero .to_bytes (1 , byteorder = 'big' )) # 1 byte para o valor de bits zero
56- resposta .extend (len (transacao )) .to_bytes (4 , byteorder = 'big' ) # 4 bytes para o tamanho da transação
69+ resposta .extend (len (transacao ).to_bytes (4 , byteorder = 'big' ) ) # 4 bytes para o tamanho da transação
5770
5871 # transação codificada em bytearray
59- resposta .extend (transacao .encode )
72+ resposta .extend (transacao .encode () )
6073 conn .sendall (resposta )
6174
6275 else :
6376 conn .sendall (b'W' ) # Não existe transação disponível
6477
65- elif msg == 'S' : # Requisição de Nonce
66- n_transacao , nonce = msg . split ( )
67- nonce = int ( nonce )
78+ elif msg == b 'S' : # Requisição de Nonce
79+ n_transacao = int . from_bytes ( msg [ 1 : 3 ], byteorder = 'big' )
80+ nonce = int . from_bytes ( msg [ 3 : 7 ], byteorder = 'big' )
6881 transacao , bits_zero = clientes_pendentes [0 ]
6982
7083 if val_nonce (transacao , nonce , bits_zero ):
@@ -76,7 +89,7 @@ def atender_cliente(conn, addr):
7689 except Exception as e :
7790 print (f'Ocorreu um erro ao extrair a transação do cliente: { e } ' )
7891
79- print (f'Cliente { name } desconectado' )
92+ print (f'Cliente { name } desconectado' )
8093 conn .close ()
8194
8295def serv_config ():
@@ -87,7 +100,7 @@ def serv_config():
87100
88101 while True :
89102 conn , addr = server .accept ()
90- threading .Thread (targed = atender_cliente , args = (conn , addr )).start () # Inicia uma Thread ao cliente
103+ threading .Thread (target = atender_cliente , args = (conn , addr )).start () # Inicia uma Thread ao cliente
91104
92105def add_transacao ():
93106 try :
@@ -124,7 +137,7 @@ def mensagens_telegram_terminal():
124137
125138def enviar_mensagem_telegram (texto ):
126139 for chat_id in clientes_telegram :
127- requests .get (f'{ API_URL } sendMensage ' , params = {'chat_id' : chat_id , 'text' : texto })
140+ requests .get (f'{ API_URL } sendMessage ' , params = {'chat_id' : chat_id , 'text' : texto })
128141
129142def comandos_telegram (chat_id , comando ):
130143 if comando == '/validtrans' :
@@ -136,7 +149,7 @@ def comandos_telegram(chat_id, comando):
136149 else :
137150 resposta = ('O comando digitado está errado!' )
138151
139- comandos_telegram (resposta )
152+ enviar_mensagem_telegram (resposta )
140153
141154
142155t = threading .Thread (target = serv_config )
0 commit comments