-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulator_new.py
371 lines (346 loc) · 11.8 KB
/
simulator_new.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/python
# -*- coding: latin-1 -*-
import os
import time
import math
import bcolors
import colorama
import argparse
from random import random
from norec4dna.Encoder import Encoder
from norec4dna.rules.FastDNARules import FastDNARules
from norec4dna.RU10Decoder import RU10Decoder
from norec4dna.OnlineEncoder import OnlineEncoder
from norec4dna.OnlineBPDecoder import OnlineBPDecoder
from norec4dna.LTEncoder import LTEncoder
from norec4dna.LTBPDecoder import LTBPDecoder
from norec4dna.LTDecoder import LTDecoder
from norec4dna.RU10Encoder import RU10Encoder
from norec4dna.distributions.RaptorDistribution import RaptorDistribution
from norec4dna.distributions.RobustSolitonDistribution import RobustSolitonDistribution
from norec4dna.distributions.OnlineDistribution import OnlineDistribution
from norec4dna.OnlineDecoder import OnlineDecoder
from norec4dna.helper import should_drop_packet
if os.name == "nt" and "PYCHARM_HOSTED" not in os.environ:
colorama.init()
useDNARules = True
DNARules = FastDNARules()
def blackbox(encoder, decoder):
encoder.encode_to_packets()
encoded_packets = encoder.get_encoded_packets() # list(itertools.permutations(..))
i = len(encoded_packets)
invalid_drop = 0
print(bcolors.BOLD + "[+] Created " + str(i) + " Packets" + bcolors.ENDC)
for packet in encoded_packets:
i -= 1
if isinstance(decoder, LTBPDecoder) or isinstance(decoder, OnlineBPDecoder):
if not should_drop_packet(DNARules, packet):
if decoder.input_new_packet(packet):
decoder.solve()
print(
bcolors.OK
+ "[!] Blackbox dropped "
+ str(i)
+ " random Packets, DNA-Simulator dropped "
+ str(invalid_drop)
+ " Packets and finished successful"
+ bcolors.ENDC
)
return decoder.is_decoded(), len(encoded_packets), i
else:
invalid_drop += 1
else:
# we are in the FAST_decoder part...
if not should_drop_packet(DNARules, packet):
decoder.input_new_packet(packet)
else:
invalid_drop += 1
# only try to solve if we
if (
packet.total_number_of_chunks <= len(encoded_packets) - i - invalid_drop
) and decoder.solve():
print(
bcolors.OK
+ "[!] Blackbox dropped "
+ str(i)
+ " random Packets, DNA-Simulator dropped "
+ str(invalid_drop)
+ " Packets and finished successful"
+ bcolors.ENDC
)
return decoder.is_decoded(), len(encoded_packets), i
if decoder.GEPP is not None:
decoder.solve()
j = 0 # Make it terminate hard if we doubled our # of encoded Packets...
while (not (decoder.is_decoded() and decoder.solve())) or j >= 2 * len(
encoded_packets
):
packet = encoder.create_and_add_new_packet()
if not should_drop_packet(DNARules, packet):
decoder.input_new_packet(packet)
else:
invalid_drop += 1
j += 1
print(
bcolors.ITALIC
+ "[!] Blackbox dropped "
+ str(i)
+ " random Packets."
+ bcolors.ENDC
)
print(
bcolors.ITALIC
+ "[!] DNA-Simulator dropped "
+ str(invalid_drop)
+ " Packets."
+ bcolors.ENDC
)
return decoder.is_decoded(), len(encoder.get_encoded_packets()), i
def __old_should_drop_packet(packet):
if not useDNARules:
return False
rand = random()
drop_chance = DNARules.apply_all_rules(packet)
# drop packet if rand bigger than the drop_chance for this Packet.
return drop_chance > rand
def blackboxOnlineTest(file, number_of_chunks=800, seed=2):
start = time.time()
epsilon = 0.05
quality = 7
dist = OnlineDistribution(epsilon, seed)
number_of_chunks = dist.get_size()
print("Starting Blackbox Test with " + str(number_of_chunks) + " Chunks")
pseudo = OnlineBPDecoder.pseudo_decoder(number_of_chunks)
encoder = OnlineEncoder(
file, number_of_chunks, dist, epsilon, quality, pseudo_decoder=pseudo
) # , chunk_size=chunk_size)
decoder = OnlineDecoder.pseudo_decoder(number_of_chunks)
decoder.set_read_all_before_decode(True)
result, numberOfEncodedPackets, droped_count = blackbox(encoder, decoder)
end = time.time() - start
print(
"Blackbox-Decode "
+ ("successful" if result else "NOT successful")
+ " after "
+ str(round(end, 4))
+ " sec."
)
return [
"Online_eps=" + str(epsilon) + "_quality=" + str(quality),
result,
numberOfEncodedPackets,
droped_count,
round(end, 4),
number_of_chunks,
]
def blackboxLTTest(file, number_of_chunks=800, seed=2, chunk_size=0):
print("Starting Blackbox Test with " + str(number_of_chunks) + " Chunks")
start = time.time()
dist = RobustSolitonDistribution(S=number_of_chunks, seed=seed)
pseudo = LTBPDecoder.pseudo_decoder(number_of_chunks)
encoder = LTEncoder(
file, number_of_chunks, dist, pseudo_decoder=pseudo, chunk_size=chunk_size
)
decoder = LTDecoder.pseudo_decoder(number_of_chunks)
result, numberOfEncodedPackets, dropedCount = blackbox(encoder, decoder)
end = time.time() - start
print(
"Blackbox-Decode "
+ ("successful" if result else "NOT successful")
+ " after "
+ str(round(end, 4))
+ " sec."
)
return [
dist.get_config_string(),
result,
numberOfEncodedPackets,
dropedCount,
round(end, 4),
number_of_chunks,
]
def blackboxRU10Test(file, number_of_chunks=800, seed=2, chunk_size=200):
print("Starting Blackbox Test with " + str(number_of_chunks) + " Chunks")
start = time.time()
dist = RaptorDistribution(number_of_chunks)
pseudo = RU10Decoder.pseudo_decoder()
encoder = RU10Encoder(
file, number_of_chunks, dist, pseudo_decoder=pseudo, chunk_size=chunk_size
)
decoder = RU10Decoder.pseudo_decoder(number_of_chunks)
result, numberOfEncodedPackets, droped_count = blackbox(encoder, decoder)
end = time.time() - start
print(
"Blackbox-Decode "
+ ("successful" if result else "NOT successful")
+ " after "
+ str(round(end, 4))
+ " sec."
)
return [
dist.get_config_string(),
result,
numberOfEncodedPackets,
droped_count,
round(end, 4),
number_of_chunks,
]
def get_random_int(max_int):
return int(random() * max_int)
def main(file="../../main.pdf", mode="LT", repreats=5):
csv = [
"filename, codecName, number_of_chunks, numberOfEncodedPackets, droprate, dropped_count, seed, result, time_needed"
]
# "OUT_raptor.pdf" #"b_lq.webm1" # "b_mq.webm" #
name = "ERROR"
# for droprate in np.arange(0.01, 0.02, 0.01):
for _ in range(repreats):
rnd = get_random_int(math.pow(2, 31) - 1)
chunk_size = 20
insert_header = True
if chunk_size != 0:
number_of_chunks = Encoder.get_number_of_chunks_for_file_with_chunk_size(
file, chunk_size=chunk_size, insert_header=insert_header
)
else:
number_of_chunks = 800
try:
if mode.lower() == "online":
(
name,
result,
numberOfEncodedPackets,
dropped_count,
time_needed,
number_of_chunks,
) = blackboxOnlineTest(
file, number_of_chunks=number_of_chunks, seed=rnd
)
# chunk_size=chunk_size)
elif mode.lower() == "lt":
(
name,
result,
numberOfEncodedPackets,
dropped_count,
time_needed,
number_of_chunks,
) = blackboxLTTest(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
chunk_size=chunk_size,
)
else:
(
name,
result,
numberOfEncodedPackets,
dropped_count,
time_needed,
number_of_chunks,
) = blackboxRU10Test(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
chunk_size=chunk_size,
)
line = (
str(file)
+ ","
+ str(name)
+ ","
+ str(number_of_chunks)
+ ","
+ str(numberOfEncodedPackets)
+ ","
+ str(dropped_count)
+ ","
+ str(rnd)
+ ","
+ str(result)
+ ","
+ str(time_needed)
)
except Exception as ex:
print("Error...", ex)
line = (
str(file)
+ ","
+ str(name)
+ ","
+ str(number_of_chunks)
+ ","
+ "ERROR"
+ ","
+ "ERROR"
+ ","
+ str(rnd)
+ ","
+ "ERROR"
+ ","
+ "ERROR"
)
print(line)
csv.append(line)
dtimeno = "sim" + str(time.strftime("%Y-%m-%d_%H-%M", time.localtime())) + ".csv"
with open(dtimeno, "w") as f:
for line in csv:
f.write(line + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Analyze network traffic")
parser.add_argument(
"-f", "--file", help="File to use for Simulation", required=True
)
parser.add_argument(
"-p",
"--profile",
help='If set, a profiling Graph "pycallgraph.png" will be created.',
action="store_true",
required=False,
)
parser.add_argument(
"-i",
"--repeats",
type=int,
help="Number of repeats the Simulator should run (default=5)",
default=5,
)
parser.add_argument(
"-m",
"--mode",
help="Mode in which the simulator should run. Choose from=[LT(default),Online,Raptor]",
default="LT",
)
parser.add_argument(
"-d",
"--dnarules",
help="If set, DNARules will be applied to all packets.",
action="store_true",
required=False,
)
args = parser.parse_args()
profile = bool(args.profile)
filename = str(args.file)
mode = str(args.mode)
useDNARules = bool(args.dnarules)
repreats = int(args.repeats)
if profile:
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
print(
bcolors.WARN
+ "[!] running with profiler - this might decrease performance"
+ bcolors.ENDC
)
with PyCallGraph(output=GraphvizOutput()):
main(filename, mode, repreats)
print(
bcolors.BLUE
+ '[*] profiling Graph saved as "pycallgraph.png"'
+ bcolors.ENDC
)
else:
main(filename, mode, repreats)
else:
print("[!] WARNING: RUN THIS SCRIPT DIRECTLY!")