-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.py
439 lines (310 loc) · 14.9 KB
/
Client.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import itertools
import logging
import sys
import zmq
import ShoppingListCRDT
import json
import time
import sqlite3
import Item
REQUEST_TIMEOUT = 2500
REQUEST_RETRIES = 3
SERVER_ENDPOINT = "tcp://localhost:5555"
LRU_READY = "\x01"
class Client:
def __init__(self, port, username):
self.username = username
self.context = zmq.Context()
self.brokerPorts = ["5555"]
self.port = port
self.connected = True
self.shopping_lists = []
self.sls_offline = []
self.items_offline = {}
self.load_schema()
self.run()
def set_port(self, port):
self.port = port
def get_port(self):
return self.port
def run(self):
while True:
print("1) Create new shopping list")
print("2) Edit shopping list")
print("3) Join shopping list")
if self.connected:
print("4) Go offline")
else:
print("4) Go online")
print("5) Exit")
choice = input("Enter your choice: ")
if choice == "1":
url = input("Enter the url of the shopping list: ")
state = self.create_shopping_list(url)
if state == -1:
print("URL already in use")
elif choice == "2":
shopping_list = self.display_shopping_lists()
if shopping_list == -1:
continue
while True:
if self.connected:
sl = self.request_shopping_list(shopping_list.get_url())
if sl is not None:
shoppinglist = ShoppingListCRDT.ShoppingListCRDT(sl["url"], sl["additions"], sl["removals"])
shoppinglist.set_key(sl["key"])
shopping_list = shopping_list.merge(shoppinglist)
self.inspect_shopping_list(shopping_list)
print("1) Add item")
print("2) Remove item")
print("3) Exit")
choice2 = input("Enter your choice: ")
if choice2 == "1":
shopping_list = self.add_item_shopping_list(shopping_list)
elif choice2 == "2":
shopping_list = self.remove_item_shopping_list(shopping_list)
else:
break
for i in range(0, len(self.shopping_lists)):
if self.shopping_lists[i].get_url() == shopping_list.get_url():
self.shopping_lists[i] = shopping_list
break
if self.connected:
self.send_shopping_list(shopping_list)
elif choice == "3":
if self.connected:
url = input("Enter the url of the shopping list: ")
key_exists = any(item.get_url() == url for item in self.shopping_lists)
if not key_exists:
sl = self.request_shopping_list(url)
shopList = ShoppingListCRDT.ShoppingListCRDT(sl["url"], sl["additions"], sl["removals"])
shopList.set_key(sl["key"])
self.shopping_lists.append(shopList)
else:
print("You already have this shopping list")
continue
else:
print("You are not connected, cannot make requests to servers to join other shopping lists")
elif choice == "4":
if self.connected:
self.connected = False
print("You are now offline")
else:
self.connected = True
print("You are now online")
self.update_from_offline()
elif choice == "5":
print("exit")
break
else:
print("Invalid choice")
def display_shopping_lists(self):
counter = 1
dict = {}
if self.shopping_lists == []:
print("You have no shopping lists")
return -1
while True:
for shopping_list in self.shopping_lists:
dict[counter] = shopping_list
print(str(counter) + ") " + shopping_list.get_url())
counter += 1
choice = input("Enter your choice: ")
if int(choice) in dict:
return dict[int(choice)]
else:
print("Invalid choice")
counter = 1
def add_item_shopping_list(self, shopping_list):
item = input("Enter the item you want to add: ")
quantity = input("Enter the quantity: ")
for i in range(0, int(quantity)):
shopping_list.add_item(item)
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
cursor.execute("SELECT url FROM ShoppingLists WHERE url=?", (shopping_list.get_url(),))
shopping_list_url = cursor.fetchone()[0]
cursor.execute("UPDATE Items SET quantity = quantity + ? WHERE shopping_list_id = ? AND name = ?",
(int(quantity), shopping_list_url, item))
if cursor.rowcount == 0:
cursor.execute("INSERT INTO Items (shopping_list_id, name, quantity) VALUES (?, ?, ?)",
(shopping_list.url, item, int(quantity)))
connection.commit()
connection.close()
return shopping_list
def remove_item_shopping_list(self, shopping_list):
item = input("Enter the item you want to remove: ")
quantity = input("Enter the quantity: ")
for i in range(0, int(quantity)):
shopping_list.remove_item(item)
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
if shopping_list.get_items()[item] != 0:
cursor.execute("UPDATE Items SET quantity = ? WHERE shopping_list_id = ? AND name = ?",
(shopping_list.get_items()[item], shopping_list.get_url(), item))
else:
cursor.execute("DELETE FROM Items WHERE shopping_list_id = ? AND name = ?",
(shopping_list.get_url(), item))
connection.commit()
connection.close()
return shopping_list
def inspect_shopping_list(self, shoppinglist):
shoppinglist.print_list()
def pack_message(self, shoppinglist):
dictionary = {}
dictionary["url"] = shoppinglist.get_url()
dictionary["items"] = shoppinglist.get_items()
dictionary["key"] = shoppinglist.get_key()
dictionary["additions"] = shoppinglist.get_additions()
dictionary["removals"] = shoppinglist.get_removals()
return "sl:" + json.dumps(dictionary, sort_keys=True)
def unpack_message(self, msg):
return json.loads(msg[2])
def update_from_offline(self):
for shopping_list in self.sls_offline:
logging.info("Connecting to server…")
client = self.context.socket(zmq.DEALER)
client.setsockopt_string(zmq.IDENTITY, str(self.port), 'utf-8')
client.connect("tcp://localhost:" + self.brokerPorts[0])
client.send_multipart([b"sl", self.pack_message(shopping_list).encode('utf-8')])
poller = zmq.Poller()
poller.register(client, zmq.POLLIN)
while True:
events = dict(poller.poll())
if client in events and events[client] == zmq.POLLIN:
reply = client.recv_multipart()
print(reply)
if "already exists" in reply[0].decode('utf-8'):
self.shopping_lists.remove(shopping_list)
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
cursor.execute("DELETE FROM ShoppingLists WHERE url = ?", (shopping_list.get_url(),))
connection.commit()
connection.close()
else:
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
key_value = shopping_list.get_key() or ''
cursor.execute("SELECT COUNT(*) FROM ShoppingLists WHERE url = ?", (shopping_list.get_url(),))
result = cursor.fetchone()
if result[0] == 0:
cursor.execute("INSERT INTO ShoppingLists (client_username, url, key) VALUES (?, ?, ?)",
(self.username, shopping_list.get_url(), key_value))
connection.commit()
connection.close()
break
client.close()
def create_shopping_list(self, url):
shopping_list = ShoppingListCRDT.ShoppingListCRDT(url)
if self.connected:
self.shopping_lists.append(shopping_list)
logging.info("Connecting to server…")
client = self.context.socket(zmq.DEALER)
client.setsockopt_string(zmq.IDENTITY, str(self.port), 'utf-8')
client.connect("tcp://localhost:" + self.brokerPorts[0])
client.send_multipart([b"sl", self.pack_message(shopping_list).encode('utf-8')])
poller = zmq.Poller()
poller.register(client, zmq.POLLIN)
while True:
events = dict(poller.poll())
if client in events and events[client] == zmq.POLLIN:
reply = client.recv_multipart()
print(reply)
if "already exists" in reply[0].decode('utf-8'):
return -1
else:
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
key_value = shopping_list.get_key() or ''
cursor.execute("SELECT COUNT(*) FROM ShoppingLists WHERE url = ?", (shopping_list.get_url(),))
result = cursor.fetchone()
if result[0] == 0:
cursor.execute("INSERT INTO ShoppingLists (client_username, url, key) VALUES (?, ?, ?)",
(self.username, shopping_list.get_url(), key_value))
connection.commit()
connection.close()
break
else:
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
key_value = shopping_list.get_key() or ''
cursor.execute("SELECT COUNT(*) FROM ShoppingLists WHERE url = ?", (shopping_list.get_url(),))
result = cursor.fetchone()
if result[0] == 0:
cursor.execute("INSERT INTO ShoppingLists (client_username, url, key) VALUES (?, ?, ?)",
(self.username, shopping_list.get_url(), key_value))
connection.commit()
connection.close()
self.sls_offline.append(shopping_list)
self.shopping_lists.append(shopping_list)
def request_shopping_list(self, url):
logging.info("Connecting to server…")
client = self.context.socket(zmq.DEALER)
client.setsockopt_string(zmq.IDENTITY, str(self.port), 'utf-8')
client.connect("tcp://localhost:" + self.brokerPorts[0])
client.send_multipart([("requestSL:" + url).encode('utf-8')])
poller = zmq.Poller()
poller.register(client, zmq.POLLIN)
sl = None
while True:
events = dict(poller.poll())
if client in events and events[client] == zmq.POLLIN:
reply = client.recv_multipart()
reply = reply[0].decode('utf-8')
if reply == "Shopping list not found":
break
sl = json.loads(reply)
break
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
cursor.execute("SELECT id FROM Clients WHERE username=?", (self.username,))
cursor.execute("SELECT * FROM ShoppingLists WHERE client_username=? AND url=?", (self.username, url))
existing_list = cursor.fetchone()
if not existing_list:
key_value = sl["key"] or ''
cursor.execute("INSERT INTO ShoppingLists (client_username, url, key) VALUES (?, ?, ?)",
(self.username, url, str(key_value)))
connection.commit()
connection.close()
return sl
def send_shopping_list(self, shopping_list):
logging.info("Connecting to server…")
client = self.context.socket(zmq.DEALER)
client.setsockopt_string(zmq.IDENTITY, str(self.port), 'utf-8')
client.connect("tcp://localhost:" + self.brokerPorts[0])
client.send_multipart([b"sl", (self.pack_message(shopping_list)).encode('utf-8')])
poller = zmq.Poller()
poller.register(client, zmq.POLLIN)
while True:
events = dict(poller.poll())
if client in events and events[client] == zmq.POLLIN:
reply = client.recv_multipart()
print(reply)
break
return
def load_schema(self):
connection = sqlite3.connect(self.username + '.db')
cursor = connection.cursor()
with open('database/shopping_lists.sql', 'r') as sql_file:
sql_script = sql_file.read()
cursor.executescript(sql_script)
cursor.execute("SELECT id FROM Clients WHERE username=? AND port=?", (self.username, self.port))
existing_client = cursor.fetchone()
if existing_client:
print("Connecting to existing client...")
cursor.execute("SELECT * FROM ShoppingLists WHERE client_username=?", (self.username,))
sls = cursor.fetchall()
for sl in sls:
shopping_list = ShoppingListCRDT.ShoppingListCRDT(sl[2])
self.shopping_lists.append(shopping_list)
else:
cursor.execute("INSERT INTO Clients (username, port) VALUES (?, ?)", (self.username, self.port))
connection.commit()
connection.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python ./Client.py <port> <username>")
sys.exit(1)
port = sys.argv[1]
username = sys.argv[2]
Client(port, username)