Skip to content

Commit

Permalink
parsing of client_cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Slootmaekers committed Sep 13, 2011
1 parent c6baeec commit 1387513
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/all_test.ml
Expand Up @@ -59,7 +59,10 @@ let compression = "compression" >::: [Compression_test.suite;]
let collapser = "collapser" >::: [Collapser_test.suite]
let crc32c_tests = "crc32c" >::: [Crc32c_test.suite]

let nursery = "nursery" >::: [Routing_test.suite]
let nursery = "nursery" >::: [
Routing_test.suite;
Client_cfg_test.suite;
]

let system = "system" >::: [
Single.force_master;
Expand Down
19 changes: 19 additions & 0 deletions src/nursery/client_cfg.ml
Expand Up @@ -44,5 +44,24 @@ module ClientCfg = struct
let node_names (t:t) = Hashtbl.fold (fun k v acc -> k::acc) t []
let make () = Hashtbl.create 7
let add (t:t) name sa = Hashtbl.add t name sa
let get (t:t) name = Hashtbl.find t name


let from_file fn =
let inifile = new Inifiles.inifile fn in
let cfg = make () in
let _get s n p = Ini.get inifile s n p Ini.required in
let cluster_id = _get "global" "cluster_id" Ini.p_string in
let nodes = _get "global" "cluster" Ini.p_string_list in
let () = List.iter
(fun n ->
let ip = _get n "ip" Ini.p_string in
let port = _get n "client_port" Ini.p_int in
let sa = ip,port in
add cfg n sa
) nodes
in
cfg

end

36 changes: 36 additions & 0 deletions src/nursery/client_cfg_test.ml
@@ -0,0 +1,36 @@
open Client_cfg
open OUnit

let test_parsing () =
let lines = [
"[global]";
"cluster_id = sturdy";
"cluster = sturdy_0,sturdy_1, sturdy_2 ";
"\n";
"[sturdy_0]";
"client_port = 7080";
"name = sturdy_0";
"ip = 127.0.0.1";
"\n";
"[sturdy_1]";
"client_port = 7081";
"name = sturdy_1";
"ip = 127.0.0.1";
"\n";
"[sturdy_2]";
"ip = 127.0.0.1";
"client_port = 7082";
"name = sturdy_2"]
in
let contents = List.fold_left (fun acc v -> acc ^ "\n" ^ v) "" lines in
let fn = "/tmp/client_cfg_test.ml" in
let oc = open_out "/tmp/client_cfg_test.ml" in
let () = output_string oc contents in
let () = close_out oc in
let cfg = ClientCfg.from_file fn in
let sa0 = ClientCfg.get cfg "sturdy_0" in
OUnit.assert_equal sa0 ("127.0.0.1",7080)

let suite = "client_cfg" >:::[
"parsing" >:: test_parsing;
]
15 changes: 15 additions & 0 deletions src/tools/ini.ml
@@ -0,0 +1,15 @@
let p_string s = Scanf.sscanf s "%s" (fun s -> s)

let p_int s = Scanf.sscanf s "%i" (fun i -> i)

let p_string_list s = Str.split (Str.regexp "[, \t]+") s

let p_bool s = Scanf.sscanf s "%b" (fun b -> b)

let required section name = failwith (Printf.sprintf "missing: %s %s" section name)

let get inifile section name s2a not_found =
try
let v_s = inifile # getval section name in
s2a v_s
with (Inifiles.Invalid_element _) -> not_found section name

0 comments on commit 1387513

Please sign in to comment.