File: monitor.ml

package info (click to toggle)
obus 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,480 kB
  • sloc: ml: 14,675; lisp: 52; makefile: 11; xml: 8
file content (34 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(*
 * monitor.ml
 * ----------
 * Copyright : (c) 2008, Jeremie Dimino <jeremie@dimino.org>
 * Licence   : BSD3
 *
 * This file is a part of obus, an ocaml implementation of D-Bus.
 *)

(* This sample illustrate the use of threads in D-Bus + use of
   filters. Filters are part of the lowlevel api. *)

open Lwt
open OBus_bus
open OBus_message
open OBus_value

let filter what_bus message =
  Format.printf "@[<hv 2>message intercepted on %s bus:@\n%a@]@." what_bus OBus_message.print message;
  (* Drop the message so we do not respond to method call *)
  None

let add_filter what_bus get_bus =
  let%lwt bus = get_bus () in
  let _ = Lwt_sequence.add_r (filter what_bus) (OBus_connection.incoming_filters bus) in
  Lwt_list.iter_p
    (fun typ -> OBus_bus.add_match bus (OBus_match.rule ~typ ()))
    [ `Method_call; `Method_return; `Error; `Signal ]

let () = Lwt_main.run begin
  let%lwt () = add_filter "session" OBus_bus.session <&> add_filter "system" OBus_bus.system in
  let%lwt () = Lwt_io.printlf "type Ctrl+C to stop" in
  fst (wait ())
end