File tree 3 files changed +63
-1
lines changed
3 files changed +63
-1
lines changed Original file line number Diff line number Diff line change 4
4
from . import demo_philosophers
5
5
from . import demo_pi
6
6
from . import demo_primes
7
- from . import demo_readers_writers
7
+ from . import demo_readers_writers
8
+ from . import demo_tcp
Original file line number Diff line number Diff line change 8
8
demo_pi .main ()
9
9
demo_primes .main ()
10
10
demo_readers_writers .main ()
11
+ demo_tcp .main ()
11
12
Original file line number Diff line number Diff line change
1
+
2
+ from cpo import *
3
+
4
+ class Packet :
5
+ pass
6
+
7
+ class SYN (Packet ):
8
+ pass
9
+
10
+ class SYNACK (Packet ):
11
+ pass
12
+
13
+ def run_demo ():
14
+ RUNS = 1000
15
+ TIMEOUT = Nanoseconds .from_seconds (0.01 )
16
+ PROB_LOSS = 0.8
17
+
18
+ DEBUGGER ()
19
+
20
+ num_timeouts = 0
21
+ for _ in range (RUNS ):
22
+ cl_to_sv = FaultyOneOne (prob_loss = PROB_LOSS )
23
+ sv_to_cl = FaultyOneOne (prob_loss = PROB_LOSS )
24
+ # dont return whether the send was successful we have to figure
25
+ # that out ourselves through ACKs etc.
26
+ send_sv = lambda v : sv_to_cl .write_before (TIMEOUT , v ) or True
27
+ send_cl = lambda v : cl_to_sv .write_before (TIMEOUT , v ) or True
28
+ recv_sv = lambda : cl_to_sv .read_before (TIMEOUT )
29
+ recv_cl = lambda : sv_to_cl .read_before (TIMEOUT )
30
+
31
+ @proc
32
+ def client ():
33
+ nonlocal num_timeouts
34
+ send_cl (SYN ())
35
+ p = None
36
+ while p is None :
37
+ p = recv_cl ()
38
+ num_timeouts += p is None
39
+ assert isinstance (p , SYNACK )
40
+ @proc
41
+ def server ():
42
+ nonlocal num_timeouts
43
+ p = None
44
+ while p is None :
45
+ p = recv_sv ()
46
+ num_timeouts += p is None
47
+ assert isinstance (p , SYN )
48
+ send_sv (SYNACK ())
49
+ (client | server )()
50
+
51
+ return RUNS , num_timeouts
52
+
53
+ def main ():
54
+ num_runs , num_timeouts = run_demo ()
55
+ timeouts_per_run = num_timeouts / num_runs
56
+ print (f'Established { num_runs } TCP handshakes with '
57
+ f'{ timeouts_per_run } timeouts per run' )
58
+
59
+ if __name__ == '__main__' :
60
+ main ()
You can’t perform that action at this time.
0 commit comments