-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
175 lines (135 loc) · 3.97 KB
/
__main__.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
164
165
166
167
168
169
170
171
172
173
174
175
import pathlib
import random
import socket
import threading
import time
from src.peer.client import client
from src.peer.rfc import RFC
from src.peer.server import P2PCommands, server
from src.server.server import P2ServerCommands
from src.utils.utils import timethat
RFC_TOTAL = 500
HOSTNAME = socket.gethostname()
START_PORT = 1234
BASE_DIR = pathlib.Path("data/")
def create_peer(
hostname: str,
port: int,
commands: list[tuple[str, dict]] = None,
rfc_index: set[RFC] = None,
) -> tuple[threading.Thread, ...]:
server_thread = threading.Thread(
target=server, args=(hostname, port, rfc_index), daemon=True
)
client_thread = threading.Thread(target=client, args=(hostname, port, commands))
return server_thread, client_thread
def make_get_rfc(hostname: str, port: int, rfc_number: int):
return (
P2PCommands.getrfc,
{"hostname": hostname, "port": port, "rfc_number": rfc_number},
)
def make_rfc_index(
hostname: str, base_dir: pathlib.Path, count: int, randomize: bool = True
):
numbers = (
list(range(1, count + 1))
if not randomize
else random.sample(range(1, RFC_TOTAL + 1), count)
)
return set(
[RFC(i, f"rfc{i}", hostname, base_dir.joinpath(f"rfc{i}.txt")) for i in numbers]
)
def task_1():
threads: list[threading.Thread] = []
clients = 20
rfc_count = 60
rfc_index = make_rfc_index(HOSTNAME, BASE_DIR, rfc_count, False)
p0 = create_peer(
hostname=HOSTNAME,
port=START_PORT,
commands=None,
rfc_index=rfc_index,
)
threads.append(p0)
commands = [
(P2ServerCommands.pquery, {}),
(P2PCommands.rfcquery, {"hostname": HOSTNAME, "port": START_PORT}),
]
commands.extend(
(make_get_rfc(HOSTNAME, START_PORT, i) for i in range(1, rfc_count - 10 + 1))
)
for i in range(1, clients):
p_i = create_peer(
hostname=HOSTNAME,
port=START_PORT + i,
commands=commands,
rfc_index=None,
)
threads.append(p_i)
for server_thread, client_thread in threads:
server_thread.start()
client_thread.start()
def task_2():
threads: list[threading.Thread] = []
clients = 20
rfc_count = 10
rfc_indexes = {
i: make_rfc_index(HOSTNAME, BASE_DIR, rfc_count) for i in range(clients)
}
commands = [
(P2ServerCommands.pquery, {}),
(P2PCommands.rfcquery, {"hostname": HOSTNAME, "port": START_PORT}),
]
for i in range(clients):
rfc_index_i = rfc_indexes[i]
get_commands = []
for key, value in rfc_indexes.items():
if key != i:
get_commands.extend(
[
make_get_rfc(HOSTNAME, START_PORT + key, rfc.number)
for rfc in value
]
)
p_i = create_peer(
hostname=HOSTNAME,
port=START_PORT + i,
commands=[*commands, *get_commands],
rfc_index=rfc_index_i,
)
server_thread, _ = p_i
server_thread.start()
threads.append(p_i)
for server_thread, client_thread in threads:
client_thread.start()
def simple_test():
rfc_count = 2
rfc_index = make_rfc_index(HOSTNAME, BASE_DIR, rfc_count, False)
A_port = START_PORT
B_port = START_PORT + 1
A_commands = [
(P2ServerCommands.pquery, {}),
make_get_rfc(HOSTNAME, B_port, 1),
(P2ServerCommands.pquery, {}),
]
B_commands = [(P2ServerCommands.leave, {})]
A = create_peer(
hostname=HOSTNAME,
port=A_port,
commands=A_commands,
rfc_index=None,
)
A[0].start()
B = create_peer(
hostname=HOSTNAME,
port=B_port,
commands=B_commands,
rfc_index=rfc_index,
)
B[0].start()
A[1].start()
B[1].start()
if __name__ == "__main__":
simple_test()
# task_1()
# task_2()