-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
163 lines (135 loc) · 5.03 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
from concurrent import futures
import sys #pip3 install sys
sys.path.append('./generated')
sys.path.append('./proto')
sys.path.append('./utils')
import grpc
import fileService_pb2_grpc
import fileService_pb2
import heartbeat_pb2_grpc
import heartbeat_pb2
import sys
import time
import yaml
import threading
import os
def getFileData():
fileName = input("Enter filename:")
outfile = os.path.join('files', fileName)
file_data = open(outfile, 'rb').read()
fileData = fileService_pb2.FileData(fileName=fileName, data=file_data)
return fileData
def getFileChunks():
# Maximum chunk size that can be sent
CHUNK_SIZE=4000000
username = input("Enter Username: ")
fileName = input("Enter filename: ")
outfile = os.path.join('files', fileName)
sTime=time.time()
with open(outfile, 'rb') as infile:
while True:
chunk = infile.read(CHUNK_SIZE)
if not chunk: break
# Do what you want with each chunk (in dev, write line to file)
yield fileService_pb2.FileData(username=username, filename=fileName, data=chunk, seqNo=1)
print("Time for upload= ", time.time()-sTime)
def downloadTheFile(stub):
userName = input("Enter Username: ")
fileName = input("Enter file name: ")
data = bytes("",'utf-8')
sTime=time.time()
responses = stub.DownloadFile(fileService_pb2.FileInfo(username=userName, filename=fileName))
for response in responses:
fileName = response.filename
data += response.data
print("Time for Download = ", time.time()-sTime)
filePath=os.path.join('downloads', fileName)
saveFile = open(filePath, 'wb')
saveFile.write(data)
saveFile.close()
print("File Downloaded - ", fileName)
def uploadTheFileChunks(stub):
response = stub.UploadFile(getFileChunks())
if(response.success): print("File successfully Uploaded")
else:
print("Failed to upload. Message - ", response.message)
def deleteTheFile(stub):
userName = input("Enter Username: ")
fileName = input("Enter file name: ")
response = stub.FileDelete(fileService_pb2.FileInfo(username=userName, filename=fileName))
print(response.message)
def isFilePresent(stub):
userName = input("Enter Username: ")
fileName = input("Enter file name: ")
response = stub.FileSearch(fileService_pb2.FileInfo(username=userName, filename=fileName))
if(response.success==True):
print(response.message)
else:
print(response.message)
def sendFileInChunks(username, filename, i):
# Maximum chunk size that can be sent
CHUNK_SIZE=4000000
outfile = os.path.join('files', fileName)
with open(outfile, 'rb') as infile:
while True:
chunk = infile.read(CHUNK_SIZE)
if not chunk: break
yield fileService_pb2.FileData(username=username+"_"+str(i), filename=fileName, data=chunk, seqNo=1)
def sendFileMultipleTimes(stub):
userName = input("Enter Username: ")
fileName = input("Enter file name: ")
numberOfTimes = input("How many times you want to send this file?")
for i in range(1, numberOfTimes+1):
response = stub.UploadFile(sendFileInChunks(userName, fileName, i))
if(response.success):
print("File successfully Uploaded for sequence : ", str(i))
else:
print("Failed to upload for sequence : ", str(i))
def updateFile(stub):
response = stub.UpdateFile(getFileChunks())
if(response.success):
print("File successfully updated")
else:
print("Failed to update the file")
def getListOfAllTheFilesForTheUser(stub):
userName = input("Enter Username: ")
FileListResponse = stub.FileList(fileService_pb2.UserInfo(username=userName))
print(FileListResponse.Filenames)
def handleUserInputs(stub):
print("===================================")
print("1. Upload a file")
print("2. Download a file.")
print("3. Delete a file")
print("4. Check if a file is present")
print("5. Update a file.")
print("6. Get a list of all the files for an user")
print("7. Send a file 100 times")
print("===================================")
option = input("Please choose an option.")
if(option=='1'):
uploadTheFileChunks(stub)
elif(option=='2'):
downloadTheFile(stub)
elif(option=='3'):
deleteTheFile(stub)
elif(option=='4'):
isFilePresent(stub)
elif(option=='5'):
updateFile(stub)
elif(option=='6'):
getListOfAllTheFilesForTheUser(stub)
elif(option=='7'):
sendFileMultipleTimes(stub)
def run_client(serverAddress):
with grpc.insecure_channel(serverAddress) as channel:
try:
grpc.channel_ready_future(channel).result(timeout=1)
except grpc.FutureTimeoutError:
print("Connection timeout. Unable to connect to port ")
#exit()
else:
print("Connected")
stub = fileService_pb2_grpc.FileserviceStub(channel)
handleUserInputs(stub)
if __name__ == '__main__':
run_client('192.168.0.9:9000')