forked from consensus-oracle/coracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.ml
34 lines (26 loc) · 1.13 KB
/
states.ml
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
open Common
type ('s,'c) state = Server of 's | Client of 'c
type ('s,'c) t = (id * ('s,'c) state list) list
(* initial server and client state, servers will be numbers 1 to servers and clients servers+1 to servers+clients *)
let rec init ~server_init ~client_init servers clients =
let total = servers+clients in
let rec state = function
| n when n>total -> []
| n when n>servers -> (n, [Client (client_init n)]) :: state (n+1)
| n -> (n, [Server (server_init n)]) :: (state (n+1)) in
state 1
let get n t = List.hd (List.assoc n t)
let clients t = map_filter (function (_,(Client c)::_) -> Some c | _ -> None) t
let servers t = map_filter (function (_,(Server c)::_) -> Some c | _ -> None) t
let set_server n state_maybe t =
match state_maybe with
| None -> t
| Some new_state ->
let old_states = List.assoc n t in
(n, (Server new_state) :: old_states) :: (List.filter (fun (i,ss) -> not (i==n)) t)
let set_client n state_maybe t =
match state_maybe with
| None -> t
| Some new_state ->
let old_states = List.assoc n t in
(n, (Client new_state) :: old_states) :: (List.filter (fun (i,ss) -> not (i==n)) t)