1
1
open TreeAggregation
2
2
open TreeAggregationNames
3
3
4
- module TreeAggregationArrangement = struct
4
+ module type TreeAggregationParams = sig
5
+ val debug : bool
6
+ val send_aggregate_timeout : float
7
+ val broadcast_timeout : float
8
+ end
9
+
10
+ module DebugParams = struct
11
+ let debug = true
12
+ let send_aggregate_timeout = 3.0
13
+ let broadcast_timeout = 5.0
14
+ end
15
+
16
+ module ProductionParams = struct
17
+ let debug = false
18
+ let send_aggregate_timeout = 2.0
19
+ let broadcast_timeout = 3.0
20
+ end
21
+
22
+ module TreeAggregationArrangement (P : TreeAggregationParams ) = struct
5
23
type name = Names .name
6
24
type state = coq_Data
7
25
type input = coq_Input
@@ -18,66 +36,72 @@ module TreeAggregationArrangement = struct
18
36
19
37
let deserializeName : string -> name option = Serialization. deserializeName
20
38
21
- let init : name -> state = fun n ->
22
- Obj. magic (coq_TreeAggregation_MultiParams.init_handlers (Obj. magic n))
39
+ let init : name -> state =
40
+ fun n ->
41
+ Obj. magic (coq_TreeAggregation_MultiParams.init_handlers (Obj. magic n))
23
42
24
43
let handleIO : name -> input -> state -> res =
25
44
fun n i s ->
26
- Obj. magic (coq_TreeAggregation_MultiParams.input_handlers (Obj. magic n) (Obj. magic i) (Obj. magic s))
45
+ Obj. magic (coq_TreeAggregation_MultiParams.input_handlers (Obj. magic n) (Obj. magic i) (Obj. magic s))
27
46
28
47
let handleNet : name -> name -> msg -> state -> res =
29
48
fun dst src m s ->
30
- Obj. magic (coq_TreeAggregation_MultiParams.net_handlers (Obj. magic dst) (Obj. magic src) (Obj. magic m) (Obj. magic s))
49
+ Obj. magic (coq_TreeAggregation_MultiParams.net_handlers (Obj. magic dst) (Obj. magic src) (Obj. magic m) (Obj. magic s))
31
50
32
- let setTimeout : name -> state -> float = fun _ _ -> 1.0
51
+ let deserializeMsg : string -> msg = Serialization. deserializeMsg
33
52
34
- let deserializeMsg = Serialization. deserializeMsg
53
+ let serializeMsg : msg -> string = Serialization. serializeMsg
35
54
36
- let serializeMsg = Serialization. serializeMsg
55
+ let deserializeInput : string -> client_id -> input option = Serialization. deserializeInput
37
56
38
- let deserializeInput = Serialization. deserializeInput
57
+ let serializeOutput : output -> client_id * string = Serialization. serializeOutput
39
58
40
- let serializeOutput = Serialization. serializeOutput
59
+ let failMsg : msg option = Some Fail
41
60
42
- let failMsg = Some Fail
61
+ let newMsg : msg option = Some New
43
62
44
- let newMsg = Some New
63
+ let debug : bool = P. debug
45
64
46
- let debug : bool = true
65
+ let timestamp () =
66
+ let open Unix in
67
+ let tm = localtime (time () ) in
68
+ Printf. sprintf " %i/%i/%i %.2i:%.2i:%.2i"
69
+ tm.tm_mday (tm.tm_mon+ 1 ) (tm.tm_year+ 1900 ) tm.tm_hour tm.tm_min tm.tm_sec
47
70
48
- let debugInput : state -> input -> unit = fun _ inp ->
49
- Printf. printf " got input %s" (Serialization. debugSerializeInput inp);
50
- print_newline ()
71
+ let debugInput : state -> input -> unit =
72
+ fun _ inp ->
73
+ Printf. printf " %s: got input %s" (timestamp () ) (Serialization. debugSerializeInput inp);
74
+ print_newline ()
51
75
52
- let debugRecv : state -> (name * msg) -> unit = fun _ (nm , msg ) ->
53
- Printf. printf " receiving message %s from %s" (Serialization. debugSerializeMsg msg) (serializeName nm);
54
- print_newline ()
76
+ let debugRecv : state -> (name * msg) -> unit =
77
+ fun _ (nm , msg ) ->
78
+ Printf. printf " %s: receiving message %s from %s" (timestamp () ) (Serialization. debugSerializeMsg msg) (serializeName nm);
79
+ print_newline ()
55
80
56
- let debugSend : state -> (name * msg) -> unit = fun _ (nm , msg ) ->
57
- Printf. printf " sending message %s to %s" (Serialization. debugSerializeMsg msg) (serializeName nm);
58
- print_newline ()
81
+ let debugSend : state -> (name * msg) -> unit =
82
+ fun _ (nm , msg ) ->
83
+ Printf. printf " %s: sending message %s to %s" (timestamp () ) (Serialization. debugSerializeMsg msg) (serializeName nm);
84
+ print_newline ()
59
85
86
+ (* obsolete *)
60
87
let debugTimeout : state -> unit = fun _ -> ()
61
88
62
- let createClientId () = Uuidm. to_string (Uuidm. create `V4 )
89
+ let createClientId () : client_id = Uuidm. to_string (Uuidm. create `V4 )
63
90
64
- let serializeClientId (c : client_id ) = c
91
+ let serializeClientId (c : client_id ) : string = c
65
92
66
93
let deliverSendAggregateHandler : task_handler =
67
94
fun n s ->
68
95
Obj. magic (coq_TreeAggregation_MultiParams.input_handlers (Obj. magic n) (Obj. magic SendAggregate ) (Obj. magic s))
69
96
70
- let setSendAggregateTimeout : timeout_setter =
71
- fun n s ->
72
- 3.0
97
+ let setSendAggregateTimeout : timeout_setter = fun n s -> P. send_aggregate_timeout
73
98
74
99
let deliverBroadcastHandler : task_handler =
75
100
fun n s ->
76
101
Obj. magic (coq_TreeAggregation_MultiParams.input_handlers (Obj. magic n) (Obj. magic Broadcast ) (Obj. magic s))
77
102
78
- let setBroadcastTimeout : timeout_setter =
79
- fun n s ->
80
- 5.0
103
+ let setBroadcastTimeout : timeout_setter = fun n s -> P. broadcast_timeout
81
104
82
- let timeoutTasks = [(deliverSendAggregateHandler, setSendAggregateTimeout); (deliverBroadcastHandler, setBroadcastTimeout)]
105
+ let timeoutTasks : (task_handler * timeout_setter) list =
106
+ [(deliverSendAggregateHandler, setSendAggregateTimeout); (deliverBroadcastHandler, setBroadcastTimeout)]
83
107
end
0 commit comments