summaryrefslogtreecommitdiffstats
path: root/sources/thibaut/globals.ml
blob: a2b22a819eee3e982172fec21aeea2c9fcb57038 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
type state = ON | OFF

let string_of_state state = match state with
    | ON -> "On"
    | OFF -> "Off"
    
let state_of_string s = match s with
    | "On" -> ON
    | "Off" -> OFF
    | _ -> failwith (Printf.sprintf "Wrong state:%s" s)
    
type phase = SEEDING | IDLE
type peer_id = int

module PeerId = struct
    type t = peer_id
    let compare = Pervasives.compare
end

module Int = struct
    type t = int
    let compare a b =  - (Pervasives.compare a b)
end

module HashMap = Map2.Make(PeerId)

module RoundMap = Map2.Make(Int)

type message_content = 
    | Seed of int * int (* seed, duration *)
    | SeedReply of int (* hash *)
    | Pulse of int * int HashMap.t list (* seed, branch *)

type message = {
    sender : peer_id;
    round : int;
    content : message_content
} 

type round_data = {
    mutable phase : phase;
    duration : int;
    seed : int;
    mutable hmap : int HashMap.t;
    mutable included : int;
    mutable replies : (int * int HashMap.t) list
}

module SlotArray : sig
    
    type 'a t

    val length : 'a t -> int
    val make : int -> 'a -> 'a t
    val full : 'a t -> bool
    val add : 'a t -> 'a -> unit
    val remove : 'a t -> int -> 'a -> unit
    val random_peer : 'a t -> 'a
    val clear : 'a t -> 'a -> unit
    val map : ('a -> 'a) -> 'a t -> unit
    val mapi : (int -> 'a -> 'a option) -> 'a t -> unit
    val iter : ('a -> unit) -> 'a t -> unit
    val iteri : (int -> 'a -> unit) -> 'a t -> unit
    val mem : ('a -> bool) -> 'a t -> bool
      
end = struct

    type 'a t = {
        mutable length : int; 
        array : 'a array;
        capacity : int
    }
      
    let full t = (t.length = t.capacity)
    
    let clear t newelem = 
        for i = 0 to t.capacity-1 do
            t.array.(i) <- newelem
        done;
        t.length <- 0
    
    let make n elem = {
        length = 0;
        array = Array.make n elem;
        capacity = n
    }
      
    let add t p = 
        let length = t.length in
        t.array.(length) <- p;
        t.length <- length + 1

    let remove t pos newelem =
        let tab = t.array in
        let length = (t.length-1) in
        tab.(pos) <- tab.(length);
        tab.(length) <- newelem;
        t.length <- length
    
    let length t = t.length
    
    let map f t =
        for i = 0 to t.capacity-1 do
            t.array.(i) <- f(t.array.(i))
        done

    let mapi f t =
        for i = 0 to t.capacity-1 do
            match f i t.array.(i) with
                | None -> ()
                | Some x -> t.array.(i) <- x
        done
        
    let iter f t =
        for i = 0 to t.capacity-1 do
            f(t.array.(i))
        done

    let iteri f t =
        for i = 0 to t.capacity-1 do
            f i (t.array.(i))
        done
        
    let mem pred sa = 
        let found = ref false in
        let i = ref 0 in
        while not !found && !i < (sa.capacity-1) do
            if pred sa.array.(!i) then begin
                found := true;
                incr i
            end
            else begin
                incr i
            end
        done;
        !found
    
    let random_peer t =
        let n = Random.int t.length in
        t.array.(n)     
end

type peer = {
    id : peer_id;
    mutable con_state : state;
    slots : slot SlotArray.t;
    mutable rounds_data : round_data RoundMap.t;
    messages : message Queue.t;
    mutable history : (int * (int HashMap.t list)) RoundMap.t (* seed, branch *)
}

and slot = 
    | Empty 
    | Peer of peer
    | AskRoot
    | AskPeer of peer