-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_fast.py
More file actions
126 lines (95 loc) · 3.7 KB
/
Copy pathclient_fast.py
File metadata and controls
126 lines (95 loc) · 3.7 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
from file import File
from packet import *
from progressbar import *
import const
import file
import multiprocessing
from multiprocessing import Process, Value, Lock
import pickle
import socket
import time
import sys
import hashlib
from math import ceil
import logging
logging.basicConfig(level=logging.INFO)
def send_thread(filename, server_address, progress, lock):
stop = False
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSocket.settimeout(1)
counter = 0
retry_counter = 0
path = '{}/' + filename
file_path = path.format(file.get_source_directory())
try:
file_obj = File(file_path)
except FileNotFoundError:
print('\n\n<<< FILE NOT FOUND >>>\n')
chunk_generator = file_obj.get_chunks_generator()
num_of_chunk = file_obj.calculate_chunks_number()
packet_class = Packet(counter)
# Send file name
clientSocket.sendto(filename.encode('utf-8').strip(), server_address)
server_port = clientSocket.recv(2)
receiver_port = int.from_bytes(server_port, byteorder='little')
next_packet = packet_class.create_packet(next(chunk_generator))
cur_packet = None
for chunk in chunk_generator:
counter += 1
success = False
cur_packet = next_packet
while not(success):
if (retry_counter > 9):
print("\n\nServer not responding")
stop = True
break
try:
clientSocket.sendto(cur_packet, (server_address[0], receiver_port))
if (counter == num_of_chunk-1 and retry_counter==0):
next_packet = packet_class.create_last_packet(chunk)
elif (counter < num_of_chunk-1 and retry_counter==0):
next_packet = packet_class.create_packet(chunk)
acknowledgement = int.from_bytes(clientSocket.recv(1024), byteorder='little')
success = True
retry_counter = 0
except Exception as e:
print('\n\n<<< RETRYING >>>', end='\r')
retry_counter += 1
if (stop):
break
progress_percent = ceil(counter/num_of_chunk*100.0)
with lock:
progress.value = progress_percent
clientSocket.sendto(next_packet, (server_address[0], receiver_port))
acknowledgement = int.from_bytes(clientSocket.recv(1024), byteorder='little')
def main():
file_list = []
if (len(sys.argv) < 2):
print ("You must specify file name in argument")
sys.exit()
elif (len(sys.argv) < 7):
for i in range(1, len(sys.argv)):
file_list.append(sys.argv[i])
else:
print ("You cannot send more than 5 file")
sys.exit()
SERVER_IP = input("Server IP Address: ")
SERVER_PORT = int(input("Server Port: "))
server_addr = (SERVER_IP, SERVER_PORT)
jumlah_file = len(sys.argv)-1
progress_poll = [Value('i', 1) for x in range(jumlah_file)]
lock = Lock()
processes = [Process(target=send_thread, args=(file_list[i], (SERVER_IP, SERVER_PORT), progress_poll[i], lock)) for i in range(jumlah_file)]
for process in processes : process.start()
max_length = len(max(file_list, key=len))
progress_bar_class = ProgressBar(max_length)
all_start_time = time.time()
while processes:
processes = [process for process in processes if process.is_alive()]
for idx, process in enumerate(processes):
progress_bar_class.print_progress_bar(progress_poll[idx].value, file_list[idx])
sys.stdout.write(u"\u001b[2J")
time.sleep(0.1)
print('Transfer complete! {}s elapsed'.format(time.time()-all_start_time))
if __name__ == '__main__':
main()