-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol-processor.sml
63 lines (56 loc) · 1.83 KB
/
protocol-processor.sml
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
functor ProtocolProcessor (structure Proto : PROTOCOL
structure Mailbox : PROTOCOL_NODE_MAILBOX
sharing Mailbox.Msg = Proto.Msg)
: PROTOCOL_PROCESSOR =
struct
val debug = true
structure Param = Proto.Param
type param = Param.t
type state = Proto.state
structure Mailbox = Mailbox
type mailbox = Mailbox.t
datatype node = Node of {id : int, mbox : mailbox, state : state}
type t = node
fun create (p, mbox) =
let
val (init_msgs, init_state) = Proto.init p
val _ = app (Mailbox.send mbox) init_msgs
in
Node {id = Param.id p, mbox = mbox, state = init_state}
end
fun run (Node {id, mbox, state}) () =
let
val in_msg = Mailbox.recv mbox
val _ =
if debug then (
TextIO.print
(Int.toString id
^ ": state "
^ Proto.State.toString state ^ "\n");
TextIO.print
(Int.toString id
^ ": processing message "
^ Proto.Msg.toString in_msg ^ "\n")
)
else ()
val (out_msgs, state') = Proto.process (in_msg, state)
val _ =
if debug then (
TextIO.print
(Int.toString id
^ ": new state "
^ Proto.State.toString state' ^ "\n");
TextIO.print
(Int.toString id
^ ": sent messages "
^ StringUtil.print_join Proto.Msg.toString out_msgs ^ "\n")
)
else ()
val _ = app (Mailbox.send mbox) out_msgs
in
if Proto.terminate state' then
()
else
run (Node {id = id, mbox = mbox, state = state'}) ()
end
end (* ProtocolProcessor *)