summaryrefslogtreecommitdiffstats
path: root/sources/thibaut/globals.ml
blob: f025108c532144c55759b29b60d59b993f815249 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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 | PULSE
type peer_id = int

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

module HashMap = Map2.Make(PeerId)

module RoundMap : sig
	type 'a t
	
	val empty : 'a t
	val add : int -> 'a -> 'a t -> 'a t
	val mem : int -> 'a t -> bool
	val find : int -> 'a t -> 'a
	val iter : (int -> 'a -> unit) -> 'a t -> unit
	val iter_limit : (int -> 'a -> unit) -> int -> 'a t -> unit
	val last_round : 'a t -> int
	val truncate : int -> 'a t -> 'a t
	
end = struct
	type 'a t = (int* 'a) list
	
	let add round data map = (round,data)::map
	
	let last_round map = match map with
		| [] -> 0
		| (a,b)::c -> a
	
	let empty = []
	
	let rec mem round map = match map with
		| [] -> false
		| (a,b)::c ->
			if a = round then true
			else if a < round then false
			else mem round c
		
	let rec find round map = match map with
		| [] -> raise Not_found
		| (a,b)::c ->
			if a = round then b
			else if a < round then raise Not_found
			else find round c
			
	let rec iter f map = match map with
		| [] -> ()
		| (a,b)::c -> f a b; iter f c
		
	let rec iter_limit f limit map = match map with
		| [] -> ()
		| (a,b)::c ->
			if a >= limit then begin
				f a b;
				iter_limit f limit c
			end
			
	let rec truncate limit map = match map with
		| [] -> []
		| (a,b)::c -> if a < limit then [] else (a,b)::(truncate limit c) 
		
		
end

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