Skip to content
Browse files

re-adding client example

  • Loading branch information...
1 parent 3197e52 commit 225e4ee1912a44275561216435dffadbc204e0b8 Romain Slootmaekers committed
View
1 _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)
View
3 examples/ocaml/client/_tags
@@ -0,0 +1,3 @@
+true: package(lwt)
+true: package(arakoon)
+true: thread
View
63 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)
View
1 examples/ocaml/plugin/README
@@ -0,0 +1 @@
+you need an installed arakoon lib for this
View
0 examples/ocaml/_tags → examples/ocaml/plugin/_tags
File renamed without changes.
View
0 examples/ocaml/do_it.sh → examples/ocaml/plugin/do_it.sh
File renamed without changes.
View
0 examples/ocaml/myocamlbuild.ml → examples/ocaml/plugin/myocamlbuild.ml
File renamed without changes.
View
0 examples/ocaml/plugin_update_max.ml → examples/ocaml/plugin/plugin_update_max.ml
File renamed without changes.

0 comments on commit 225e4ee

Please sign in to comment.
Something went wrong with that request. Please try again.