This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fnode.py
73 lines (51 loc) · 1.63 KB
/
fnode.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
#!/env/bin/python
import hashlib
import json
import random
import string
import sys
import time
import zmq
from termcolor import colored
def clear():
sys.stderr.write("\x1b[2J\x1b[H")
def node_data(data):
for i in data:
print colored(i, 'magenta')
def node_info(node):
print colored('#############', 'magenta')
print colored('My IP -->' + node['ip'] + ':' + node['port'], 'magenta')
print colored('My ID -->' + node['id'][0:7], 'magenta')
print colored('back IP -> ' + node['lower_bound_ip'], 'magenta')
print colored('back -> ' + node['lower_bound'][0:7], 'magenta')
print colored('FILES: ', 'magenta')
node_data(node['file'])
print colored('#############', 'magenta')
def sha256(toHash):
return str(hashlib.sha256(str(toHash)).hexdigest())
# Load JSON from a file
def load_json(path):
json_data = open(path, 'r')
d = json.load(json_data)
return d
def printJSON(varJSON):
print json.dumps(varJSON, indent=2, sort_keys=True)
# Create a JSON request
def create_req(req, who, to, msg):
data = json.dumps({'req': req, 'from': who, 'to': to, 'msg': msg})
return data
def check_rank(my_id, lower_id, target):
if lower_id > my_id:
if target > lower_id or (target >= 0 and target < my_id):
return 0
else:
return -1
else:
if (target <= my_id and target > lower_id) or (my_id == lower_id):
return 0
else:
return -1
def file_to_ring(node, filename, binary, fileid):
node['file'][fileid] = {'name': filename, 'data': binary}
def remove_file_ring(node, fileid):
del node['file'][fileid]