forked from estefaniaguardado/ChatSocket
/
ActionHandler.py
executable file
·256 lines (212 loc) · 9.16 KB
/
ActionHandler.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import uuid
import FileHandler
import json
class ActionHandler(object):
def __init__(self):
super(ActionHandler, self).__init__()
self.usuarios = {}
self.mensajesPorUsuario = {}
self.usuarios["0"] = {"status" : "online", "usuario" : "ROOT"}
def procesaAccion(self, modelDeDatos):
if modelDeDatos["accion"] == "actualizar":
identificador = str(modelDeDatos["identificador"])
if identificador == "0" or identificador in self.usuarios:
informacion = modelDeDatos["informacion"]
retorno = {"status" : "ok"}
if identificador == "0":
identificador = str(uuid.uuid4())
llavePrivada = str(uuid.uuid4())
informacion["llavePrivada"] = llavePrivada
retorno["identificador"] = identificador
retorno["llavePrivada"] = llavePrivada
self.usuarios[identificador] = informacion
return retorno
return {"status" : "error", "mensaje" : "Identificador destinatario no valido"}
if modelDeDatos["accion"] == "enviar":
return self.mandarMensaje(modelDeDatos)
if modelDeDatos["accion"] == "listarMensajes":
return {"status" : "ok",
"enviadoMsj" : self.mensajesPorUsuario}
if modelDeDatos["accion"] == "recibir":
return self.recibirMensajes(modelDeDatos)
if modelDeDatos["accion"] == "backup":
return self.hazRespaldo()
if modelDeDatos["accion"] == "restaurar":
return self.restauraRespaldo()
if modelDeDatos["accion"] == "listar":
return self.listaUsuarios()
else:
return {"status" : "ok",
"informacion" : self.usuarios}
def agregaMensajeAContenedorDeUsuario(self, usuarioInteres, mensajeInteres):
if usuarioInteres in self.mensajesPorUsuario:
contenedor = self.mensajesPorUsuario[usuarioInteres]
else:
contenedor = []
self.mensajesPorUsuario[usuarioInteres] = contenedor
contenedor.append(mensajeInteres)
def mandarMensaje(self, modelDeDatos):
usuarioDestinatario = modelDeDatos["identificador"]
if usuarioDestinatario in self.usuarios:
mensaje = modelDeDatos["informacionMsj"]
mensaje["destinatario"] = usuarioDestinatario
self.agregaMensajeAContenedorDeUsuario(usuarioDestinatario, mensaje)
if "remitente" in mensaje and mensaje["remitente"] in self.usuarios and mensaje["remitente"] != usuarioDestinatario:
self.agregaMensajeAContenedorDeUsuario(mensaje["remitente"], mensaje)
return {"status" : "ok"}
else:
return {"status" : "error", "mensaje" : "Identificador destinatario no valido"}
def recibirMensajes(self, modelDeDatos):
usuario = str(modelDeDatos["identificador"])
usuarioValido = self.usuarios[usuario]["llavePrivada"] == modelDeDatos["llavePrivada"]
participante = modelDeDatos["participante"] if "participante" in modelDeDatos else ""
if participante == "0":
usuario = "0"
mensajesAlmacenados = []
hayMensajes = usuario in self.mensajesPorUsuario
if hayMensajes and usuarioValido:
mensajesAlmacenados = self.mensajesPorUsuario[usuario]
mensajesAlmacenados = [mensaje for mensaje in mensajesAlmacenados if self.filtraMensajes(mensaje, participante)]
return {"status" : "ok", "recibidoMsj" : mensajesAlmacenados}
def hazRespaldo(self):
FileHandler.stringAArchivo("Backup/usuarios", json.dumps(self.usuarios), toBase64=False)
FileHandler.stringAArchivo("Backup/mensajesPorUsuario", json.dumps(self.mensajesPorUsuario), toBase64=False)
return {"status" : "ok"}
def restauraRespaldo(self):
if FileHandler.existeArchivo("Backup/usuarios") and FileHandler.existeArchivo("Backup/mensajesPorUsuario"):
self.usuarios = json.loads(FileHandler.archivoAString("Backup/usuarios", toBase64=False))
self.mensajesPorUsuario = json.loads(FileHandler.archivoAString("Backup/mensajesPorUsuario", toBase64=False))
return {"status" : "ok"}
return {"status" : "error", "mensaje" : "No hay respaldo existente"}
def listaUsuarios(self):
usuarios = self.usuarios
salida = {}
for identificador in usuarios:
infoPrivada = dict(usuarios[identificador])
if "llavePrivada" in infoPrivada:
infoPrivada.pop("llavePrivada")
if identificador == "0" or infoPrivada["status"] == "online":
salida[identificador] = infoPrivada
return {"status" : "ok", "informacion" : salida}
def filtraMensajes(self, mensaje, usuario):
if usuario == "0":
return True
remitente = mensaje["remitente"] if "remitente" in mensaje else ""
destinatario = mensaje["destinatario"] if "destinatario" in mensaje else ""
return remitente == usuario or destinatario == usuario
if __name__ == "__main__":
modelDeDatosActualizar = {
"accion" : "actualizar",
"identificador" : "1",
"informacion" : {
"status" : "online",
"usuario" : "Fanny",
"IP" : "192.168.1.65",
"puerto" : 13375
}
}
modelDeDatosActualizar1 = {
"accion" : "actualizar",
"identificador" : "2",
"informacion" : {
"status" : "online",
"usuario" : "Luis",
"IP" : "192.168.1.65",
"puerto" : 13378
}
}
modelDeDatosListar = {
"accion" : "listar"
}
enviarMensajeF = {
"accion" : "enviar",
"identificador" : "1",
"informacionMsj" : {
"horaFecha" : datetime.datetime.now(),
"mensaje" : "hola, como estas"
}
}
enviarMensajeL = {
"accion" : "enviar",
"identificador" : "2",
"informacionMsj" : {
"horaFecha" : datetime.datetime.now(),
"mensaje" : "muy bien y tu?"
}
}
mensajesLista = {
"accion" : "listarMensajes"
}
mensajesObtenerLuis = {
"accion" : "recibir",
"identificador" : "2"
}
mensajesObtenerFanny = {
"accion" : "recibir",
"identificador" : "1"
}
actionHandler = ActionHandler()
# Ejemplo Actualizar
respuesta = actionHandler.procesaAccion(modelDeDatosActualizar)
print respuesta
respuesta1 = actionHandler.procesaAccion(modelDeDatosActualizar1)
print respuesta1
if respuesta["status"] == "ok":
print "Correcto almacenamiento"
else:
raise Exception("Incorrecto almacenamiento")
# Ejemplo Listar con un elemento
respuesta_listado = actionHandler.procesaAccion(modelDeDatosListar)
if respuesta_listado["status"] == "ok":
informacion_listado = respuesta_listado["informacion"]
print informacion_listado
if len(informacion_listado) == 2:
if informacion_listado["1"]["usuario"] == "Fanny":
print "Correcto listado Fanny"
else:
raise Exception("Incorrecto listado Fanny")
if informacion_listado["2"]["usuario"] == "Luis":
print "Correcto listado Luis"
else:
raise Exception("Incorrecto listado Luis")
else:
raise Exception("Incorrecto usuarios almacenados")
else:
raise Exception("Incorrecto listado")
#Ejemplo Enviado
enviado_Fanny = actionHandler.procesaAccion(enviarMensajeF)
if enviado_Fanny["status"] == "ok":
print "Enviado correctamente"
else:
raise Exception("Fallo en el envio")
#Ejemplo de lista de mensajes enviados
lista_enviados = actionHandler.procesaAccion(mensajesLista)
if lista_enviados["status"] == "ok" and len(lista_enviados["enviadoMsj"]) == 1:
print lista_enviados["enviadoMsj"]
print "Mensajes almacenados correctamente"
else:
raise Exception("Fallo listado de todos los mensajes")
#Ejemplo Recibido de Luis
respuesta_recibido = actionHandler.procesaAccion(mensajesObtenerLuis)
if respuesta_recibido["status"] == "ok" and len(respuesta_recibido["recibidoMsj"]) == 0:
print "Recibido para Luis"
print respuesta_recibido
else:
raise Exception("Error mensajes recibidos para Luis")
#Ejemplo Recibido de Fanny
respuesta_recibido = actionHandler.procesaAccion(mensajesObtenerFanny)
if respuesta_recibido["status"] == "ok" and len(respuesta_recibido["recibidoMsj"]) == 1:
print "Recibido para Fanny"
print respuesta_recibido
else:
raise Exception("Error mensajes recibidos para Fanny")
#Ejemplo Recibido de Fanny sin mensajes
respuesta_recibido = actionHandler.procesaAccion(mensajesObtenerFanny)
if respuesta_recibido["status"] == "ok" and len(respuesta_recibido["recibidoMsj"]) == 0:
print "Recibido para Fanny"
print respuesta_recibido
else:
raise Exception("Error mensajes recibidos para Fanny sin mensajes")