-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
112 lines (91 loc) · 2.97 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
import sys
import rpyc
import os
from pprint import pprint
def usage():
print("./client.py IPADDRESS")
def parseCMD():
return raw_input('theFS> ').split()
def getFileList(ns):
file_table = ns.get_file_table()
for entry in file_table:
print("{}".format(entry))
print("")
def read_from_storage(block_uuid, storage):
addr, port = storage
con = rpyc.connect(addr, port=port)
ss = con.root.Storage()
return ss.pull(block_uuid)
def getFile(ns, fname):
file_table = ns.get_file_table_entry(fname)
if not file_table:
print("[-] File not found!")
return
with open(fname, "w") as f:
for block in file_table:
for storageServer in [ns.get_storage_list()[_] for _ in block[1]]:
data = read_from_storage(block[0], storageServer)
if data:
f.write(data)
break
else:
print("[-] No blocks found! Possible corruption!")
return
def send_to_storage(block_uuid, data, storageList):
print("sending: {} -> {}".format(str(block_uuid), str(storageList)))
storage = storageList[0]
storages = storageList[1:]
addr, port = storage
con = rpyc.connect(addr, port=port)
storage = con.root.Storage()
storage.push(data, storages, block_uuid)
def putFile(ns, fname):
size = os.path.getsize(fname)
blocks = ns.put(fname, size)
with open(fname) as f:
for b in blocks:
data = f.read(ns.get_block_size())
block_uuid=b[0]
storageList = [ns.get_storage_list()[_] for _ in b[1]]
send_to_storage(block_uuid, data, storageList)
def delete_from_storage(block_uuid, storage):
addr, port = storage
con = rpyc.connect(addr, port=port)
ss = con.root.Storage()
return ss.delete(block_uuid)
def rmFile(ns, fname):
file_table = ns.get_file_table_entry(fname)
if not file_table:
print("[-] File not found!")
return
for block in file_table:
for storageServer in [ns.get_storage_list()[_] for _ in block[1]]:
delete_from_storage(block[0], storageServer)
ns.delete_file_entry(fname)
def processCommand(ns, parameters):
if len(parameters) == 0:
return
elif parameters[0] == "quit":
exit()
elif parameters[0] == "list" or parameters[0] == "ls":
getFileList(ns)
elif parameters[0] == "get":
getFile(ns, parameters[1])
elif parameters[0] == "put":
putFile(ns, parameters[1])
elif parameters[0] == "delete" or parameters[0] == "rm":
rmFile(ns, parameters[1])
else:
return
def main(args):
addr, port = ("localhost", 5353)
if len(args) > 0:
addr = args[0]
if len(args) > 1:
port = int(args[1])
con = rpyc.connect(addr, port)
nameServer = con.root.NameServer()
while True:
processCommand(nameServer, parseCMD())
if __name__ == "__main__":
main(sys.argv[1:])