-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBaseAPI.py
More file actions
175 lines (160 loc) · 5.69 KB
/
DataBaseAPI.py
File metadata and controls
175 lines (160 loc) · 5.69 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
"""
DataBase object to control it's API
"""
import sqlite3
from sqlite3 import Error
import logging
import os
class DataBase:
"""DataBase class for managing the DB with easy API """
logging.basicConfig(filename="data_base_log.log", filemode='w', level=logging.INFO, format='%(name)s@%(levelname)s -> %(message)s')
def __init__(self):
"""
Initialize the connection to the DB
"""
try:
#os.remove("server.db")
self.conn = sqlite3.connect("server.db") # creating the DB
self.cur = self.conn.cursor()
logging.info("connected to DB")
success_clients = self._create_clients_db()
success_message = self._create_messages_db()
if not (success_clients and success_message):
self.__del__()
logging.error("Closing DB Connection")
logging.error("Closing Program")
exit(0)
except Error as e:
logging.error(e)
def __del__(self):
self.conn.close()
def _create_clients_db(self):
"""
creating a clients table
:return: success status
"""
try:
self.conn.executescript('''CREATE TABLE clients
(ID HEX TEXT PRIMARY KEY NOT NULL,
Name VARCHAR(255) NOT NULL,
PublicKey HEX TEXT,
LastSeen DATE);''') # create client DB
self.conn.commit()
logging.info("Created clients table")
return True
except Error as e:
logging.error(e)
return False
def _create_messages_db(self):
"""
creating a messages table
:return: success status
"""
try:
self.conn.executescript('''CREATE TABLE messages
(ID INT PRIMARY KEY NOT NULL,
ToClient HEX TEXT NOT NULL,
FromClient HEX TEXT NOT NULL,
Type INT NOT NULL,
Content Blob,
FOREIGN KEY(ToClient) REFERENCES clients(ID),
FOREIGN KEY(FromClient) REFERENCES clients(ID));''') # create messages DB
self.conn.commit()
logging.info("Created message table")
return True
except Error as e:
logging.error(e)
return False
def insert_messages_to_db(self, msg_id, dest_id, src_id, msg_type, msg_content):
"""
Inserting a message to the db
:param msg_id: unique id for message (4 bytes int)
:param dest_id: unique id for the sender client of the message (16 bytes int)
:param src_id: unique id for the receiver client of the message (16 bytes int)
:param msg_type: message type (byte int)
:param msg_content: the content of the message
:return:
"""
try:
self.conn.execute("INSERT INTO messages VALUES (?, ?, ?, ?, ?);", [msg_id, dest_id, src_id, msg_type, msg_content])
self.conn.commit()
except Error as e:
logging.error(e)
def insert_clients_to_db(self, client_id, client_name, client_public_key, client_last_seen):
"""
Inserting a client to DB
:param client_id:
:param client_name:
:param client_public_key:
:param client_last_seen:
:return:
"""
try:
self.conn.execute("INSERT INTO clients VALUES (?, ?, ?, ?);", [client_id, client_name, client_public_key, client_last_seen])
self.conn.commit()
except Error as e:
logging.error(e)
def get_messages_for_client(self, dest_client_uuid):
"""
pull all waiting message
:param dest_client_uuid: a unique id for the destination of the message
:return: messages array
"""
try:
self.cur.execute("SELECT FROM messages WHERE ToClient = ?;", [dest_client_uuid])
messages_array = self.cur.fetchall()
return messages_array
except Error as e:
logging.error(e)
return None
def get_public_key(self, ID):
"""
send the public key
:param ID: Client ID
:return: Public key (HEX)
"""
try:
self.cur.execute("SELECT PublicKey FROM clients WHERE ID = ?;", [id])
public_key = self.cur.fetchall()
return public_key
except Error as e:
logging.error(e)
return 0
def get_name(self, ID):
"""
send the public key
:param ID: Client ID
:return: Name
"""
try:
self.cur.execute("SELECT Name FROM clients WHERE ID = ?;", [id])
name = self.cur.fetchall()
return name
except Error as e:
logging.error(e)
return ""
def get_last_seen(self, ID):
"""
send the public key
:param ID: Client ID
:return: LastSeen
"""
try:
self.cur.execute("SELECT LastSeen FROM clients WHERE ID = ?;", [id])
last_seen = self.cur.fetchall()
return last_seen
except Error as e:
logging.error(e)
return 0
def get_client_list(self, client_id):
"""
return the client list
:return: Name , ID
"""
try:
self.cur.execute("SELECT ID, Name FROM clients WHERE ID != [?];", client_id)
client_list = self.cur.fetchall()
return client_list
except Error as e:
logging.error(e)
return ""