process_message.py
2.2 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
import ipdb
import os
from services import serviceFactory, Datatypes, Services
from database import DBconnection, Table, States
class Process:
def __init__(self,db):
self.db = db
self.conn = DBconnection(db)
# stores the message and returns its id
def store(self,query):
# service is wrong
if not Services.validate(query[Table.serv]):
return "No existe el servicio '" + query[Table.serv] + "'"
# message can't be sent by this service
if not serviceFactory(query[Table.serv]).validate(query[Table.type]):
return "El servicio '" + query[Table.serv] + "' no puede enviar el tipo '" + query[Table.type] + "'"
entities = {
Table.file : query[Table.file],
Table.dest : query[Table.dest],
Table.serv : query[Table.serv],
Table.type : query[Table.type],
Table.state : query[Table.state]
}
id = self.conn.insert("msg",entities)
return str(id)
# tries to send all messages available
def send(self):
rows = self.conn.query("SELECT * FROM msg WHERE state = ?",(States.queued,))
for query in DBconnection.parseToTable(rows):
# if file doesn't exist, erase the message request, it can't be read anyway
if not os.path.exists(query[Table.file]):
self.conn.query("DELETE FROM msg WHERE id = ?",(query[Table.id],))
serv = serviceFactory(query[Table.serv])
success = serv.send(query)
if success:
# save as delivered
self.conn.query("UPDATE msg SET state = ? WHERE id = ?",(States.delivered,query[Table.id]))
# returns the state of a message given its id
# stores the message to history if delivered
def lookup(self,id):
rows = self.conn.query("SELECT * FROM msg WHERE id = ?",(id,))
if rows == []:
return "El id " + str(id) + " no existe"
rows = DBconnection.parseToTable(rows)
row = rows[0]
if row[Table.state] == States.delivered:
self.conn.query("DELETE FROM msg WHERE id = ?",(id,))
entities = {
Table.file : row[Table.file],
Table.serv : row[Table.serv],
Table.dest : row[Table.dest],
Table.type : row[Table.type],
Table.state : row[Table.state],
}
self.conn.insert("history",entities)
return row[Table.state]