Skip to content

Commit c395270

Browse files
author
David Scott
committed
Update to the new message-switch.0.11.0 interface
Signed-off-by: David Scott <[email protected]>
1 parent 6d87ef5 commit c395270

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

main.ml

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -450,46 +450,60 @@ let process root_dir name x =
450450
(* Active servers, one per sub-directory of the root_dir *)
451451
let servers = String.Table.create () ~size:4
452452

453-
let create switch_port root_dir name =
453+
(* XXX: need a better error-handling strategy *)
454+
let get_ok = function
455+
| `Ok x -> x
456+
| `Error e ->
457+
let b = Buffer.create 16 in
458+
let fmt = Format.formatter_of_buffer b in
459+
Protocol_unix.Server.pp_error fmt e;
460+
Format.pp_print_flush fmt ();
461+
failwith (Buffer.contents b)
462+
463+
let create switch_path root_dir name =
454464
if Hashtbl.mem servers name
455465
then return ()
456466
else begin
457467
info "Adding %s" name
458468
>>= fun () ->
459-
Protocol_async.M.connect switch_port >>= fun c ->
460-
let server = Protocol_async.Server.listen (process root_dir name) c (Filename.basename name) in
469+
Protocol_async.Server.listen ~process:(process root_dir name) ~switch:switch_path ~queue:(Filename.basename name) ()
470+
>>= fun result ->
471+
let server = get_ok result in
461472
Hashtbl.add_exn servers name server;
462473
return ()
463474
end
464475

465-
let destroy switch_port name =
476+
let destroy switch_path name =
466477
info "Removing %s" name
467478
>>= fun () ->
468-
Protocol_async.M.connect switch_port >>= fun c ->
469-
Hashtbl.remove servers name;
470-
return ()
479+
if Hashtbl.mem servers name then begin
480+
let t = Hashtbl.find_exn servers name in
481+
Protocol_async.Server.shutdown ~t () >>= fun () ->
482+
Hashtbl.remove servers name;
483+
return ()
484+
end else return ()
471485

472486
let rec diff a b = match a with
473487
| [] -> []
474488
| a :: aa ->
475489
if List.mem b a then diff aa b else a :: (diff aa b)
476490

477491
(* Ensure the right servers are started *)
478-
let sync ~root_dir ~switch_port =
492+
let sync ~root_dir ~switch_path =
479493
Sys.readdir root_dir
480494
>>= fun names ->
481495
let needed : string list = Array.to_list names in
482496
let got_already : string list = Hashtbl.keys servers in
483-
Deferred.all_ignore (List.map ~f:(create switch_port root_dir) (diff needed got_already))
497+
Deferred.all_ignore (List.map ~f:(create switch_path root_dir) (diff needed got_already))
484498
>>= fun () ->
485-
Deferred.all_ignore (List.map ~f:(destroy switch_port) (diff got_already needed))
499+
Deferred.all_ignore (List.map ~f:(destroy switch_path) (diff got_already needed))
486500

487-
let main ~root_dir ~switch_port =
501+
let main ~root_dir ~switch_path =
488502
(* We watch and create queues for the Volume plugins only *)
489503
let root_dir = Filename.concat root_dir "volume" in
490504
Async_inotify.create ~recursive:false ~watch_new_dirs:false root_dir
491505
>>= fun (watch, _) ->
492-
sync ~root_dir ~switch_port
506+
sync ~root_dir ~switch_path
493507
>>= fun () ->
494508
let pipe = Async_inotify.pipe watch in
495509
let open Async_inotify.Event in
@@ -501,24 +515,24 @@ let main ~root_dir ~switch_port =
501515
Shutdown.exit 1
502516
| `Ok (Created path)
503517
| `Ok (Moved (Into path)) ->
504-
create switch_port root_dir (Filename.basename path)
518+
create switch_path root_dir (Filename.basename path)
505519
| `Ok (Unlinked path)
506520
| `Ok (Moved (Away path)) ->
507-
destroy switch_port (Filename.basename path)
521+
destroy switch_path (Filename.basename path)
508522
| `Ok (Modified _) ->
509523
return ()
510524
| `Ok (Moved (Move (path_a, path_b))) ->
511-
destroy switch_port (Filename.basename path_a)
525+
destroy switch_path (Filename.basename path_a)
512526
>>= fun () ->
513-
create switch_port root_dir (Filename.basename path_b)
527+
create switch_path root_dir (Filename.basename path_b)
514528
| `Ok Queue_overflow ->
515-
sync ~root_dir ~switch_port
529+
sync ~root_dir ~switch_path
516530
) >>= fun () ->
517531
loop () in
518532
loop ()
519533

520-
let main ~root_dir ~switch_port =
521-
let (_: unit Deferred.t) = main ~root_dir ~switch_port in
534+
let main ~root_dir ~switch_path =
535+
let (_: unit Deferred.t) = main ~root_dir ~switch_path in
522536
never_returns (Scheduler.go ())
523537

524538
open Xcp_service
@@ -558,5 +572,5 @@ let _ =
558572
use_syslog := true;
559573
Core.Syslog.openlog ~id:"xapi-storage-script" ~facility:Core.Syslog.Facility.DAEMON ();
560574
end;
561-
main ~root_dir:!root_dir ~switch_port:!Xcp_client.switch_port
575+
main ~root_dir:!root_dir ~switch_path:!Xcp_client.switch_path
562576

0 commit comments

Comments
 (0)