Skip to content

Commit

Permalink
re-adding client example
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Slootmaekers committed Jun 5, 2012
1 parent 3197e52 commit 225e4ee
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
1 change: 0 additions & 1 deletion _tags
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions examples/ocaml/client/_tags
@@ -0,0 +1,3 @@
true: package(lwt)
true: package(arakoon)
true: thread
63 changes: 63 additions & 0 deletions 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)
1 change: 1 addition & 0 deletions examples/ocaml/plugin/README
@@ -0,0 +1 @@
you need an installed arakoon lib for this
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 225e4ee

Please sign in to comment.