-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
95 lines (82 loc) · 3.34 KB
/
client.py
File metadata and controls
95 lines (82 loc) · 3.34 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
#!/usr/bin/env python
import sys
import glob
import hashlib as _hl
sys.path.append('gen-py')
sys.path.insert(0, glob.glob('/home/yaoliu/src_code/local/lib/lib/python2.7/site-packages')[0])
from chord import FileStore
import chord.ttypes as _tt
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def main():
transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2]))
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
test_hash = _hl.sha256('randi.txt').hexdigest()
print("\n********** "+str(test_hash)+"**********\n")
test_node = client.findSucc(test_hash)
print("\n\n-------- 1 ---"+str(test_node)+"------\n")
transport.close()
transport = TSocket.TSocket(test_node.ip, test_node.port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
meta = _tt.RFileMetadata()
meta.filename = 'randi.txt'
meta.version = 0
w_file = _tt.RFile()
w_file.meta = meta
w_file.content = "<h1>This is great, just it should work!<h1>"
w_file.meta.contentHash = _hl.sha256(w_file.content).hexdigest()
client.writeFile(w_file)
transport.close()
print("\n\n-------- 2 --- write ------\n")
transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2]))
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
test_hash = _hl.sha256('randi.txt').hexdigest()
test_node = client.findSucc(test_hash)
print(test_node)
transport.close()
transport = TSocket.TSocket(test_node.ip, test_node.port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
R_file = client.readFile('randi.txt')
assert w_file.content == R_file.content, "Content problem"
assert w_file.meta.filename == R_file.meta.filename, "filename problem"
assert w_file.meta.version == 0, "version problem"
assert w_file.meta.contentHash == R_file.meta.contentHash, "filename problem"
transport.close()
print("\n\n-------- 3 --- read correct ------\n")
transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2]))
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
test_hash = _hl.sha256('file.txt').hexdigest()
print("\n((((((((()))))))))"+str(test_hash)+"((((((()))))))\n")
test_node = client.findSucc(test_hash)
print(test_node)
transport.close()
transport = TSocket.TSocket(test_node.ip, test_node.port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = FileStore.Client(protocol)
transport.open()
print(client.readFile('file.txt'))
transport.close()
print("\n\n-------- 4 --- read correct ------\n")
if __name__ == '__main__':
try:
main()
except Thrift.TException as tx:
print('%s' % tx.message)