From 225e4ee1912a44275561216435dffadbc204e0b8 Mon Sep 17 00:00:00 2001 From: Romain Slootmaekers Date: Tue, 5 Jun 2012 10:44:18 +0200 Subject: [PATCH] re-adding client example --- _tags | 1 - examples/ocaml/client/_tags | 3 + examples/ocaml/client/demo.ml | 63 +++++++++++++++++++ examples/ocaml/plugin/README | 1 + examples/ocaml/{ => plugin}/_tags | 0 examples/ocaml/{ => plugin}/do_it.sh | 0 examples/ocaml/{ => plugin}/myocamlbuild.ml | 0 .../ocaml/{ => plugin}/plugin_update_max.ml | 0 8 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 examples/ocaml/client/_tags create mode 100644 examples/ocaml/client/demo.ml create mode 100644 examples/ocaml/plugin/README rename examples/ocaml/{ => plugin}/_tags (100%) rename examples/ocaml/{ => plugin}/do_it.sh (100%) rename examples/ocaml/{ => plugin}/myocamlbuild.ml (100%) rename examples/ocaml/{ => plugin}/plugin_update_max.ml (100%) diff --git a/_tags b/_tags index d28ee43c..a188862f 100644 --- a/_tags +++ b/_tags @@ -23,7 +23,6 @@ true: package(baardskeerder) "src/inifiles": include "src/nursery": include "src/plugins": include -"examples/ocaml": include "src/main" : include <**/nursery.*{byte,native}>: is_main,use_thread <**/profile.*{byte,native}>: is_main, package(ocamlviz) diff --git a/examples/ocaml/client/_tags b/examples/ocaml/client/_tags new file mode 100644 index 00000000..ffd9dd47 --- /dev/null +++ b/examples/ocaml/client/_tags @@ -0,0 +1,3 @@ +true: package(lwt) +true: package(arakoon) +true: thread diff --git a/examples/ocaml/client/demo.ml b/examples/ocaml/client/demo.ml new file mode 100644 index 00000000..0ee9561d --- /dev/null +++ b/examples/ocaml/client/demo.ml @@ -0,0 +1,63 @@ +open Lwt +open Arakoon_remote_client +open Arakoon_client + +let make_address ip port = + let ha = Unix.inet_addr_of_string ip in + Unix.ADDR_INET (ha,port) + +let with_client cluster_id (ip,port) f = + let sa = make_address ip port in + let do_it connection = + make_remote_client cluster_id connection >>= fun client -> + f client + in + Lwt_io.with_connection sa do_it + +let find_master cluster_id cfgs = + let rec loop = function + | [] -> Lwt.fail (Failure "too many nodes down") + | cfg :: rest -> + begin + let _,(ip, port) = cfg in + let sa = make_address ip port in + Lwt.catch + (fun () -> + Lwt_io.with_connection sa + (fun connection -> + make_remote_client cluster_id connection + >>= fun client -> + client # who_master ()) >>= function + | None -> Lwt.fail (Failure "No Master") + | Some m -> Lwt.return m) + (function + | Unix.Unix_error(Unix.ECONNREFUSED,_,_ ) -> loop rest + | exn -> Lwt.fail exn + ) + end + in loop cfgs + + +let with_master_client cluster_id cfgs f = + find_master cluster_id cfgs >>= fun master_name -> + let master_cfg = List.assoc master_name cfgs in + with_client cluster_id master_cfg f + + +let demo (client:Arakoon_client.client) = + client # set "foo" "bar" >>= fun () -> + client # get "foo" >>= fun v -> + Lwt_io.printlf "foo=%s" v >>= fun () -> + client # delete "foo" >>= fun () -> + client # exists "foo" >>= fun e -> + Lwt_io.printlf "have foo? %b" e + +let _ = + let cluster_id = "ricky" in + let cfgs = [ + ("arakoon_0",("127.0.0.1",4000)); + ("arakoon_1",("127.0.0.1",4001)); + ("arakoon_2",("127.0.0.1",4002)) + ] + in + Lwt_main.run (with_master_client cluster_id cfgs demo) diff --git a/examples/ocaml/plugin/README b/examples/ocaml/plugin/README new file mode 100644 index 00000000..72ea6504 --- /dev/null +++ b/examples/ocaml/plugin/README @@ -0,0 +1 @@ +you need an installed arakoon lib for this diff --git a/examples/ocaml/_tags b/examples/ocaml/plugin/_tags similarity index 100% rename from examples/ocaml/_tags rename to examples/ocaml/plugin/_tags diff --git a/examples/ocaml/do_it.sh b/examples/ocaml/plugin/do_it.sh similarity index 100% rename from examples/ocaml/do_it.sh rename to examples/ocaml/plugin/do_it.sh diff --git a/examples/ocaml/myocamlbuild.ml b/examples/ocaml/plugin/myocamlbuild.ml similarity index 100% rename from examples/ocaml/myocamlbuild.ml rename to examples/ocaml/plugin/myocamlbuild.ml diff --git a/examples/ocaml/plugin_update_max.ml b/examples/ocaml/plugin/plugin_update_max.ml similarity index 100% rename from examples/ocaml/plugin_update_max.ml rename to examples/ocaml/plugin/plugin_update_max.ml