Skip to content

Commit de738f1

Browse files
committed
Start work on TCP demo... wip
1 parent fc163dd commit de738f1

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

demos/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
from . import demo_philosophers
55
from . import demo_pi
66
from . import demo_primes
7-
from . import demo_readers_writers
7+
from . import demo_readers_writers
8+
from . import demo_tcp

demos/demo.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
demo_pi.main()
99
demo_primes.main()
1010
demo_readers_writers.main()
11+
demo_tcp.main()
1112

demos/demo_tcp.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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()

0 commit comments

Comments
 (0)