Skip to content
This repository
tag: v993
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 250 lines (209 sloc) 8.042 kb
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
open Cps.Ops

module Hashtbl = Base.Hashtbl

#<Debugvar:HLDIR_DEBUG>

(* ********************************************************)
(* DEFINE TYPE FOR HLNET PROTOCOL *************************)
type kind = Dir | Loc

type who = Me | Other of Unix.inet_addr

type iwho = IMe | IYou | IOther of Unix.inet_addr

(* Types for [kind=Dir] *)
type ('key, 'value) request =
  | FindOrReplace of ('key * 'value)
  | Add of ('key * 'value)
  | Find of 'key
  | All of 'key
  | Remove of 'key

type ('key, 'value) dir_request_channel =
    (('key, 'value) request, ('value * iwho) list) Hlnet.channel

type ('key, 'value) dir_response_channel =
    (('value * iwho) list, ('key, 'value) request) Hlnet.channel

(* Types for [kind=Loc] *)

type loc_channel = (Unix.inet_addr, Unix.inet_addr) Hlnet.channel

(* Type of a directory. *)

type ('key, 'value) sdirectory = {
  sched : Scheduler.t;
  mutable k_myaddr : (Unix.inet_addr -> unit) list;
  mutable myaddr : Unix.inet_addr option;
  htbl : ('key, 'value * who) Hashtbl.t;
}

type ('key, 'value) cdirectory = {
  mutable cmyaddr : Unix.inet_addr option;
  chan : ('key, 'value) dir_request_channel;
}

type ('key, 'value) t =
  | Server of ('key, 'value) sdirectory
  | Client of ('key, 'value) cdirectory



(* ********************************************************)
(* CONVERSION FUNCTIONS ***********************************)
let endpoint_to_inet = function
    Hlnet.Tcp (x, _) | Hlnet.Ssl (x, _, _) (* | Hlnet.Udp (x, _) *) -> x

let inet_of_channel c =
  endpoint_to_inet (Hlnet.remote_of_channel c)

let who_to_iwho (response_channel:('a, 'b)dir_response_channel) who =
  let response_addr = inet_of_channel response_channel in
  match who with
  | Me -> IMe
  | Other addr when addr = response_addr -> IYou
  | Other x -> IOther x

let iwho_to_who (request_channel:('a, 'b)dir_request_channel) iwho =
  match iwho with
  | IMe -> Other (inet_of_channel request_channel)
  | IYou -> Me
  | IOther x -> Other x

let list_to_opt chan = function
  | [] -> None
  | [(v,x)] -> Some (v, iwho_to_who chan x)
  | _ -> failwith("list_to_opt : Unexpected list")

let opt_to_list chan = function
  | None -> []
  | Some (v, x) -> ([v, who_to_iwho chan x] : ('a * iwho) list)

let list_to_ilist chan list =
  List.map
    (function (v,x) -> v, who_to_iwho chan x)
    list

let ilist_to_list chan list =
  List.map
    (function (v,x) -> v, iwho_to_who chan x)
    list

let who_to_string = function
  | Me -> "Me"
  | Other x -> Unix.string_of_inet_addr x



(* ********************************************************)
(* ********************************************************)
module ExtendHash = struct
  let find_or_replace htbl key value =
    match Hashtbl.find_opt htbl key with
    | None ->
        Hashtbl.add htbl key value;
        None
    | x -> x
end

let rec make ?(err_cont=fun _ ->
                 #<If> Logger.warning "[DIRECTORY] Make : Uncaught exn"
                 #<Else> ()
                 #<End>)
    sched endpoint path kind k =
  let dir_service = Hlnet.make_service_id ~name:(Printf.sprintf "dir/%s" path) ~version:1 in
  match kind with
  | `client ->
      #<If>
        Logger.debug "[DIRECTORY] Try to create a client directory (%s@ %s)"
        (Hlnet.print_service_id dir_service) (Hlnet.endpoint_to_string endpoint)
      #<End>;
      Hlnet.open_channel sched ~on_disconnect:(fun () -> err_cont Exit; `retry (Time.seconds 5))
        endpoint (Hlnet.Aux.magic_spec dir_service)
      @> fun chan -> Client {chan = chan; cmyaddr = None} |> k
  | `server ->
      #<If>
        Logger.debug "[DIRECTORY] Try to create a server directory (%s)"
        (Hlnet.endpoint_to_string endpoint)
      #<End>;
      let server = {
        sched = sched;
        k_myaddr = [];
        htbl = Hashtbl.create 1024;
        myaddr = None;
      } in
      (try
         let safe = true in
         Hlnet.accept ~safe sched endpoint (Hlnet.Aux.magic_spec dir_service)
           (fun (hlchan : ('key, 'value) dir_response_channel) ->
              Hlnet.setup_respond hlchan
                (fun request k ->
                   match request with
                   | FindOrReplace (key, value) ->
                       let addr = inet_of_channel hlchan in
                       let res =
                         ExtendHash.find_or_replace server.htbl key
                           (value, Other addr) in
                       opt_to_list hlchan res |> k
                   | Add (key, value) ->
                       let addr = inet_of_channel hlchan in
                       Hashtbl.add server.htbl key (value, Other addr)
                   | Find key ->
                       let res = Hashtbl.find_opt server.htbl key in
                       opt_to_list hlchan res |> k
                   | All key ->
                       list_to_ilist hlchan (Hashtbl.find_all server.htbl key) |> k
                   | Remove key ->
                       Hashtbl.remove server.htbl key
                )
           );
         Hlnet.accept ~safe sched endpoint
           (Hlnet.Aux.magic_spec (Hlnet.make_service_id ~name:"dir/localize" ~version:1))
           (fun (hlchan : loc_channel) ->
              Hlnet.setup_respond hlchan
                (fun myaddr k ->
                   server.myaddr <- Some myaddr;
                   List.iter
                     (fun k -> Scheduler.push server.sched (fun () -> k myaddr))
                     server.k_myaddr;
                   server.k_myaddr <- [];
                   k (inet_of_channel hlchan))
           );
         Server server |> k
       with e -> err_cont e)

let add t key value =
  match t with
  | Client r -> Hlnet.send r.chan (Add (key, value))
  | Server r -> Hashtbl.add r.htbl key (value, Me)

let find_or_replace t key value k =
  match t with
  | Client r ->
      Hlnet.sendreceive r.chan (FindOrReplace (key, value)) @>
        (function res -> list_to_opt r.chan res |> k)
  | Server r ->
      (ExtendHash.find_or_replace r.htbl key (value, Me)) |> k

let find_opt t key k =
  match t with
  | Client r ->
      Hlnet.sendreceive r.chan (Find key)
        (function res -> list_to_opt r.chan res |> k)
  | Server r ->
      Hashtbl.find_opt r.htbl key |> k

let find_all t key k =
  match t with
  | Client r ->
      Hlnet.sendreceive r.chan (All key)
        (function l -> k (ilist_to_list r.chan l))
  | Server r ->
      Hashtbl.find_all r.htbl key |> k

let remove t key =
  match t with
  | Client r -> Hlnet.send r.chan (Remove key)
  | Server r -> Hashtbl.remove r.htbl key

let my_public_addr_opt t k =
  match t with
  | Client r ->
      (match r.cmyaddr with
       | Some _ as a -> k a
       | None ->
           let endpoint = Hlnet.remote_of_channel r.chan in
           let addr = endpoint_to_inet endpoint in
           let sched = Hlnet.scheduler_of_channel r.chan in
           Hlnet.open_channel sched endpoint
             (Hlnet.Aux.magic_spec (Hlnet.make_service_id ~name:"dir/localize" ~version:1))
           @> (function (chan : loc_channel) ->
                Hlnet.sendreceive chan addr @> function a ->
                  r.cmyaddr <- Some a; r.cmyaddr |> k))
  | Server r -> r.myaddr |> k

let my_public_addr t k =
  match t with
  | Client _ -> my_public_addr_opt t @> (fun x -> Option.get x |> k)
  | Server r ->
      (match r.myaddr with
       | Some x -> x |> k
       | None -> r.k_myaddr <- k::r.k_myaddr)
Something went wrong with that request. Please try again.