Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
126 lines (91 loc) · 3.3 KB
/
Copy pathclient.py
File metadata and controls
126 lines (91 loc) · 3.3 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
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
import zmq
import sys
import os
import random
from utilities import *
def establishConnections(IP, start_port, process_count):
temp_port_list = list(range(start_port,start_port+process_count))
master_ports_list = list()
#Randomizing connection sequence
while len(temp_port_list):
rnd = random.randint(0,5000) % len(temp_port_list)
master_ports_list.append(temp_port_list[rnd])
temp_port_list.remove(master_ports_list[-1])
context = zmq.Context()
socket_list = list()
for port in master_ports_list:
temp_socket = context.socket(zmq.REQ)
temp_socket.connect("tcp://"+IP+":"+str(port))
socket_list.append(temp_socket)
return context, socket_list, master_ports_list
'''
Attributes:
MASTER_IP
MASTER_START_PORT
MASTER_PROCESSES_COUNT
DOWNLOAD_DIR
USER_ID
'''
master_IP=sys.argv[1]
master_start_port=int(sys.argv[2])
master_processes_count=int(sys.argv[3])
download_dir = sys.argv[4]
user_id = sys.argv[5]
last_requested_server = 0
if os.path.isdir(download_dir) == False:
os.mkdir(download_dir)
context, master_socket_list, master_ports_list = establishConnections(
master_IP, master_start_port, master_processes_count)
context = zmq.Context()
while True:
print("Enter Process type: ", end='')
process = input()
print('')
if process == "exit":
break
if process == "download" or process == "upload":
print("==>Enter File Name: ",end='')
file_name = input()
print('')
#Preparing message sent to master
master_message = {"PROCESS" : process}
if process == "download":
master_message.update({"FILE_NAME" : file_name})
master_process = master_socket_list[last_requested_server]
master_process.send_pyobj(master_message)
master_received = master_process.recv_pyobj()
if ("FILE_NAME" in master_received):
print ("File name invalid\n")
continue
else:
DK_IP_port = master_received['IP'] + ":" + master_received['PORT']
last_requested_server += 1
last_requested_server %= master_processes_count
print("Client got ip:port %s\n"%(DK_IP_port))
#Establishing connection to a data keeper
DK_socket = context.socket(zmq.PAIR)
DK_socket.connect("tcp://"+DK_IP_port)
DK_message = dict()
if process == "upload":
DK_message = sendFile(file_name)
file_name=DK_message['FILE_NAME'].split("/")[-1]
DK_message.update({
"PROCESS_TYPE" : process,
"FILE_NAME" : file_name,
"USER_ID" : user_id
})
DK_socket.send_pyobj(DK_message)
if process == "download":
received_message = DK_socket.recv_pyobj()
received_message['FILE_NAME'] = download_dir + '/' + received_message['FILE_NAME'].split("/")[-1]
saveFile(received_message)
print("Downloading Done!\n")
else:
print("Uploading Done!\n")
DK_socket.close()
print("Want new process?[Y/n] ",end='')
choice = input()
if choice =='n' or choice =='N':
break
else:
print("==========> Wrong process type!\n")