-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
59 lines (45 loc) · 1.56 KB
/
utils.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
import mmap
import socket
from random import randint
from typing import Tuple
MAX_DATA_SIZE = 65507
BUFFER_SIZE = 65536
TRACKER_ADDR = ('localhost', 12340)
open_ports = (1024, 49151) # available user ports
occupied_ports = []
def split_file(path: str, rng: Tuple[int, int],
chunk_size: int = MAX_DATA_SIZE - 20000) -> list:
assert chunk_size > 0, print("The chunk size should be bigger than 0.")
with open(path, "r+b") as f:
# getting the specified range
mm = mmap.mmap(f.fileno(), 0)[rng[0]: rng[1]]
# diving the bytes into chunks
ret = [mm[chunk: chunk + chunk_size] for chunk in
range(0, rng[1] - rng[0], chunk_size)]
return ret
def assemble_file(chunks: list, path: str) -> None:
f = open(path, "bw+")
for c in chunks:
f.write(c)
f.flush()
f.close()
def give_port() -> int:
rand_port = randint(open_ports[0], open_ports[1])
while rand_port in occupied_ports:
rand_port = randint(open_ports[0], open_ports[1])
return rand_port
def create_socket(port: int) -> socket.socket:
global occupied_ports
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('localhost', port))
occupied_ports.append(port)
return s
def free_socket(s: socket.socket):
global occupied_ports
port = port_number(s)
occupied_ports.remove(port)
s.close()
print(f"Port {port}'s socket is now closed.")
def port_number(s: socket.socket) -> int:
return s.getsockname()[1]