-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_functions.py
47 lines (38 loc) · 1.15 KB
/
common_functions.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
import requests, random
# random select, selects randomly an online node to send the request
# set the ip to the Bootstrap's ip
boot_ip='127.0.0.1:5000'
def exec_requests(requests):
for r in requests:
if r[0]=='insert':
insert(r[1],r[2],r[3])
elif r[0]=='query':
query(r[1],r[2])
return
def random_select():
r = requests.post('http://'+boot_ip+'/overlay')
nodes_list = r.json()['topology']
ip_list = []
for node in nodes_list:
temp_ip = (node['node_ip_port'])
ip_list.append(temp_ip)
return random.choice(ip_list)
def insert(key, value, node = None):
if node != None:
ip = node
else:
ip = random_select()
r = requests.post('http://'+ip+'/insert', data = { 'key':key, 'value':value })
print(r.text)
def query(key, node = None):
if node != None:
ip = node
else:
ip = random_select()
r = requests.post('http://'+ip+'/query', data = { 'key':key })
if (key == '*'):
print("Those are all key-value pairs in Chord:\n")
for node in r.json()['result']:
print(node, "\n")
else:
print(r.json()['result'])