clean tcp shizzle werkt nu, heeft aangepaste versie van gast nodig!
[tt2015.git] / a3 / code / tcpmodel.icl
1 module tcpmodel
2
3 import gast, TCPIP
4
5 derive bimap []
6 derive gEq State
7 derive gLess State
8 derive genShow State
9 derive ggen Input
10 derive genShow Input
11 derive gEq Output
12 derive genShow Output
13
14 /* connection settings */
15 hostIP = "127.0.0.1"
16 hostPort = 1203
17
18 /* states */
19 :: State = Listen | ReceivedSYN | Established | Waiting | Closed
20
21 /* input (received from client) */
22 :: Input = InSYN | InACK | InRST | InFIN | InUserData
23
24 instance toString Input where
25 toString InSYN = "SYN"
26 toString InACK = "ACK"
27 toString InRST = "RST"
28 toString InFIN = "FIN"
29 toString InUserData = "DATA <some data here>"
30
31 /* output (sent to client) */
32 :: Output = OutSYN | OutACK | OutFIN
33
34 toOutput :: String -> [Output]
35 toOutput "SYN-ACK" = [OutSYN, OutACK]
36 toOutput _ = []
37
38 /* state transitions */
39 stateTrans :: State Input -> [Trans Output State]
40 stateTrans Listen InSYN = [Pt [OutSYN, OutACK] ReceivedSYN]
41 stateTrans Listen _ = [Pt [] Listen]
42
43 stateTrans ReceivedSYN InRST = [Pt [] Listen]
44 stateTrans ReceivedSYN InACK = [Pt [] Established]
45 stateTrans ReceivedSYN _ = [Pt [] ReceivedSYN]
46
47 stateTrans Established InACK = [Pt [OutACK] Established]
48 stateTrans Established InUserData = [Pt [OutACK] Waiting]
49 stateTrans Established InFIN = [Pt [OutACK] Closed]
50 stateTrans Established _ = [Pt [] Established]
51
52 stateTrans Waiting InACK = [Pt [] Established]
53 stateTrans Waiting _ = [Pt [] Waiting]
54
55 /* sut state */
56 :: *SutState :== (StringSChannel, StringRChannel, *World)
57
58 /* adapter for SUT communication */
59 adapter :: *SutState Input -> ([Output], *SutState)
60 adapter (s, r, world) i
61 # (s, world) = send (toString i) s world
62 # (cmd, r, world) = receive r world
63 = (toOutput cmd, (s, r, world))
64
65 /* reset SUT */
66 reset :: *SutState -> *SutState
67 reset (s, r, world)
68 # (s, world) = send "SUT-RESET" s world
69 = (s, r, world)
70
71 Start world
72 /* open console */
73 # (console, world) = stdio world
74 /* setup connection with adapter */
75 # (Just adr, world) = lookupIPAddress hostIP world
76 # (to, mcon, world) = connectTCP_MT Nothing (adr, hostPort) world
77 | to <> TR_Success
78 # console = console <<< "ERROR: can't connect to SUT!\n"
79 # (_, world) = fclose console world
80 = world
81 # (Just con) = mcon
82 /* open files */
83 # (_, file, world) = fopen "testOut.txt" FWriteText world
84 /* perform testing */
85 # st = (toStringSChannel con.sChannel, toStringRChannel con.rChannel, world)
86 # ((_, _, world), console, file) = testConfSM [] stateTrans Listen adapter st reset console file
87 /* close connection */
88 /* close files */
89 # (_, world) = fclose file world
90 # (_, world) = fclose console world
91 /* done */
92 = world
93