Source file oBus_resolver.ml
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
let section = Lwt_log.Section.make "obus(resolver)"
open Lwt_react
module String_map = Map.Make(String)
let cache_size = 100
type resolver = {
mutable count : int;
owner : OBus_name.bus signal;
set_owner : OBus_name.bus -> unit;
}
and info = {
mutable resolvers : (resolver * Lwt_switch.t) Lwt.t String_map.t;
mutable exited : OBus_name.bus array;
mutable exited_index : int;
}
let finalise remove _ =
ignore (Lazy.force remove)
let has_exited peer_name info =
let rec loop index =
if index = cache_size then
false
else if info.exited.(index) = peer_name then
true
else
loop (index + 1)
in
loop 0
let key = OBus_connection.new_key ()
let get_name_owner connection name =
try%lwt
OBus_connection.method_call
~connection
~destination:OBus_protocol.bus_name
~path:OBus_protocol.bus_path
~interface:OBus_protocol.bus_interface
~member:"GetNameOwner"
~i_args:(OBus_value.C.seq1 OBus_value.C.basic_string)
~o_args:(OBus_value.C.seq1 OBus_value.C.basic_string)
name
with exn when OBus_error.name exn = "org.freedesktop.DBus.Error.NameHasNoOwner" ->
Lwt.return ""
let update_mapping info message =
let open OBus_message in
let open OBus_value in
match message with
| { sender = "org.freedesktop.DBus";
typ = Signal(["org"; "freedesktop"; "DBus"], "org.freedesktop.DBus", "NameOwnerChanged");
body = [V.Basic(V.String name); V.Basic(V.String old_owner); V.Basic(V.String new_owner)] } ->
if OBus_name.is_unique name && new_owner = "" && not (has_exited name info) then begin
info.exited.(info.exited_index) <- name;
info.exited_index <- (info.exited_index + 1) mod cache_size
end;
begin
match try Lwt.state (String_map.find name info.resolvers) with Not_found -> Sleep with
| Return(resolver, switch) ->
resolver.set_owner new_owner
| Fail _ | Sleep ->
()
end;
Some message
| _ ->
Some message
let make ?switch connection name =
Lwt_switch.check switch;
OBus_string.assert_validate OBus_name.validate_bus name;
let info =
match OBus_connection.get connection key with
| Some info ->
info
| None ->
let info = {
resolvers = String_map.empty;
exited = Array.make cache_size "";
exited_index = 0;
} in
OBus_connection.set connection key (Some info);
let _ = Lwt_sequence.add_l (update_mapping info) (OBus_connection.incoming_filters connection) in
info
in
if OBus_name.is_unique name && has_exited name info then
Lwt.return (S.const "")
else begin
let%lwt resolver, export_switch =
match try Some(String_map.find name info.resolvers) with Not_found -> None with
| Some thread ->
thread
| None ->
let waiter, wakener = Lwt.wait () in
info.resolvers <- String_map.add name waiter info.resolvers;
let export_switch = Lwt_switch.create () in
try%lwt
let%lwt () =
OBus_match.export
~switch:export_switch
connection
(OBus_match.rule
~typ:`Signal
~sender:OBus_protocol.bus_name
~interface:OBus_protocol.bus_interface
~member:"NameOwnerChanged"
~path:OBus_protocol.bus_path
~arguments:(OBus_match.make_arguments [(0, OBus_match.AF_string name)]) ())
in
let%lwt current_owner = get_name_owner connection name in
let owner, set_owner = S.create current_owner in
let resolver = { count = 0; owner; set_owner } in
Lwt.wakeup wakener (resolver, export_switch);
Lwt.return (resolver, export_switch)
with exn ->
info.resolvers <- String_map.remove name info.resolvers;
Lwt.wakeup_exn wakener exn;
let%lwt () = Lwt_switch.turn_off export_switch in
Lwt.fail exn
in
resolver.count <- resolver.count + 1;
let remove = lazy(
try%lwt
resolver.count <- resolver.count - 1;
if resolver.count = 0 then begin
info.resolvers <- String_map.remove name info.resolvers;
Lwt_switch.turn_off export_switch
end else
Lwt.return ()
with exn ->
let%lwt () = Lwt_log.warning_f ~section ~exn "failed to disable resolver for name %S" name in
Lwt.fail exn
) in
let owner = S.with_finaliser (finalise remove) resolver.owner in
let%lwt () =
Lwt_switch.add_hook_or_exec
switch
(fun () ->
S.stop owner;
Lazy.force remove)
in
Lwt.return owner
end