-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP_comunication.py
More file actions
270 lines (238 loc) · 7.14 KB
/
UDP_comunication.py
File metadata and controls
270 lines (238 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import os, sys
import socket
import time
HOST = 'localhost'
PORT = 50022
TAM_MSG = 128
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST, PORT))
class mensagem:
#def __init__(self, *args):
# """ Create a vector, example: v = Vector(1,2) """
# if len(args)==0: self.values = (0,0)
# else: self.values = args
def __init__(self):
self.ID = '-'
self.ipo = '-'
self.ipd = '-'
self.porto = '-'
self.portd = '-'
self.MSG = '-'
self.status = '-'
self.nome = '-'
self.grupo = '-'
self.command = '-'
self.aux = '-'
def __set__(self, ID, IPO, IPD, PortO, PortD, MSG, status, nome, grupo, command, aux):
self.ID = ID
self.ipo = IPO
self.ipd = IPD
self.porto = PortO
self.portd = PortD
self.MSG = MSG
self.status = status
self.nome = nome
self.grupo = grupo
self.command = command
self.aux = aux
def setid(self, ID):
self.ID = ID
def setipo(self, IPO):
self.ipo = IPO
def setipd(self, IPD):
self.ipo = IPD
def setporto(self, PortO):
self.porto = PortO
def setportd(self, PortD):
self.portd = PortD
def setmsg(self, MSG):
self.MSG = MSG
def setstatus(self, status):
self.status = status
def setnome(self, nome):
self.nome = nome
def setgrupo(self, grupo):
self.grupo = grupo
def setcommand(self, command):
self.command = command
def setaux(self, aux):
self.aux = aux
def getget(self):
return self.ID, self.ipo, self.ipd, self.porto, self.portd, self.MSG, self.status, self.nome, self.grupo
def getid(self):
return self.ID
def getipo(self):
return self.ipo
def getipd(self):
return self.ipd
def getporto(self):
return self.porto
def getportd(self):
return self.portd
def getmsg(self):
return self.MSG
def getstatus(self):
return self.status
def getnome(self):
return self.nome
def getgrupo(self):
return self.grupo
def getcommand(self):
return self.command
def getaux(self):
return self.aux
def enviarMSG(self):
MSG = str(self.getid()) + '\t' + str(self.getipo()) + '\t' + str(self.getipd()) + '\t' + str(self.getporto()) + '\t' + str(self.getportd()) + '\t' + str(self.getmsg()) + '\t' + str(self.getstatus()) + '\t' + str(self.getnome()) + '\t' + str(self.getgrupo()) + '\t' + str(self.getcommand()) + '\t' + str(self.getaux())
s.sendto(bytes(MSG, 'UTF-8'), (HOST, PORT))
def setRECV(self, MSG):
parametros = MSG.split('\t')
self.ID = parametros[0]
self.ipo = parametros[1]
self.ipd = parametros[2]
self.porto = parametros[3]
self.Portd = parametros[4]
self.MSG = parametros[5]
self.status = parametros[6]
self.nome = parametros[7]
self.grupo = parametros[8]
self.command = parametros[9]
self.aux = parametros[10]
def printf(self):
print(self.getget())
# START FILE SYSTEM MANIPULATION
def saveMSG(self, File):
target = open(File, 'a')
linha = str(self.getid()) + '\t' + str(self.getipo()) + '\t' + str(self.getipd()) + '\t' + str(self.getporto()) + '\t' + str(self.getportd()) + '\t' + str(self.getmsg()) + '\t' + str(self.getstatus()) + '\t' + str(self.getnome()) + '\t' + str(self.getgrupo()) + '\t' + str(self.getcommand()) + '\t' + str(self.getaux() + '\n')
target.write(linha)
target.close()
def recoverMSG(self, File, ID):
with open(File) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[0] == ID:
self.ID = parametros[0]
self.ipo = parametros[1]
self.ipd = parametros[2]
self.porto = parametros[3]
self.Portd = parametros[4]
self.MSG = parametros[5]
self.status = parametros[6]
self.nome = parametros[7]
self.grupo = parametros[8]
self.command = parametros[9]
self.aux = parametros[10]
else:
pass
def setstatusARQ(self, File, ID, status):
with open(File) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[0] == ID:
self.status = status
# ACHA TRABALHO PARA FAZER & RETORNA A LINHA
def findWork(self, File, status):
with open(File) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[6] == status:
recoverMSG(File, parametros[0])
##
### TEM QUE VERIFICAR SE O NOME FOR CONTATO OU GRUPO OU MULTIPLOS GRUPOS
##
def listarMSG(self, FileContatos, FileMensagens, nome, UG):
auxID = ''
localizounome = 0
localizoumsg = 0
# VERIFICA SE EXISTE CONTATO ********
with open(FileContatos) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[7] == nome:
localizounome += 1
auxID = parametros[0]
##print("\n", parametros[7], " ", parametros, "\n")
##print("@@@@", localizounome, localizoumsg)
# MULTIPLOS NOME IGUAIS VAI DAR ERRO
if localizounome == 1:
with open(FileMensagens) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[0] == auxID:
localizoumsg += 1
# IMPRIME A MENSAGEM E STATUS DA MENSAGEM
print(parametros[5], " ", parametros[6])
return localizounome, localizoumsg
elif localizounome > 1:
return 2, -1
else:
return -1, -1
####$$$$$
####$$$$$ SE PRIMEIRA LINHA FOR IGUAL A ZERO ELE DA ERRO
####$$$$$
# PREVER QUE SE FOR GRUPO ADD UM SIMBOLO NO INICIO PARA IDENTIFICAR TAL #####
def listarContatos(self, FileContatos):
with open(FileContatos) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
# SE FOR IGUAL A '-' É PORQUE É UM CONTATO NAO GRUPO
if parametros[8] == '-':
sys.stdout.write(parametros[7])
sys.stdout.write("\n")
else:
sys.stdout.write(" # ")
sys.stdout.write(parametros[7])
sys.stdout.write("\n")
#********************************************************
def contatoexiste(self, FileContatos, nome):
localizounome = 0
with open(FileContatos) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[7] == nome:
localizounome += 1
auxID = parametros[0]
return localizounome
# x < 1 -> NAO EXISTE GRUPO
# x = 0 -> EXISTE GRUPO MAS NAO O CONTATO
# x = 1 -> JA EXISTE CONTATO NO GRUPO
# x > 1 -> EXISTE MULTIPLOS CONTATOS COM MESMO NOME NESTE GRUPO // NÃO TESTO O IP
def grupoexiste(self, FileContatos, nome, grupo):
localizounome = 0
with open(FileContatos) as f:
content = f.readlines()
f.close()
for line in content:
parametros = line.split('\t')
if parametros[7] == nome and parametros[8] == str(grupo):
localizounome += 1
return localizounome
# END FILE SYSTEM MANIPULATION
#######################################################################3
def enviarMSG(MSG):
s.sendto(bytes(MSG, 'UTF-8'), (HOST, PORT))
return 1
def receberMSG():
MSG = s.recv(TAM_MSG).decode('UTF-8')
return MSG
def saveMSG(File, linha):
target = open(File, 'a')
target.write(linha)
target.close()
def display():
print("Selecione uma das funcionalidades abaixo: \n")
print("i\t-> Inserir nome\ng\t-> Inserir grupo\nl\t-> Listar Mensagens\ns\t-> Enviar mensagem\nc\t-> Listar contatos\nd\t-> Deletar contato\nH\t-> HELP\nT\t-> EXIT PROGRAM !!!!")
return input()