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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
open Core
open Async_kernel
open Async_rpc_kernel
module Fn = Babel_fn
module Description = struct
include Rpc.Description
include Comparable.Make_plain (Rpc.Description)
end
module Implementer = struct
type 'a t =
{ implement :
's. ?on_exception:Rpc.On_exception.t -> ('s -> 'a) -> 's Rpc.Implementation.t
; rpc : Generic_rpc.t
}
let map { implement; rpc } ~f =
let implement ?on_exception implementation =
implement ?on_exception (fun s -> f (implementation s))
in
{ implement; rpc }
;;
end
open Implementer
type 'a t = ('a Implementer.t, Shape.Set.t Lazy.t) Result.t Description.Map.t
module Validated = struct
type 'a t = (Rpc.Description.t * 'a Implementer.t) list
end
let validate (t : _ t) : _ Validated.t Or_error.t =
Map.to_alist t
|> List.map ~f:(fun (description, implementer) ->
match implementer with
| Ok implementer -> Ok (description, implementer)
| Error shapes ->
Or_error.error_s
[%message
"Duplicate rpcs"
(description : Rpc.Description.t)
(shapes : Shape.Set.t Lazy.t)])
|> Or_error.combine_errors
;;
let implement_multi ?on_exception t ~f =
let open Or_error.Let_syntax in
let%map validated = validate t in
List.map validated ~f:(fun (description, implementer) ->
implementer.implement ?on_exception (fun s -> f s description))
;;
let implement_multi_exn ?on_exception t ~f = ok_exn (implement_multi ?on_exception t ~f)
let shapes t =
let open Or_error.Let_syntax in
validate t
>>| List.map ~f:(fun (description, implementer) ->
description, Generic_rpc.shape implementer.rpc)
;;
let supported_rpcs t =
Or_error.map
(validate t)
~f:(List.map ~f:(fun ((_ : Description.t), implementer) -> implementer.rpc))
;;
let supported_rpcs_exn t = ok_exn (supported_rpcs t)
let shapes_exn t = ok_exn (shapes t)
let descriptions t = Or_error.map (validate t) ~f:(List.map ~f:fst)
let descriptions_exn t = List.map (shapes_exn t) ~f:fst
let print_shapes t =
Or_error.map (shapes t) ~f:(fun shapes ->
List.map shapes ~f:(fun (description, shape) ->
description.name, (description.version, shape))
|> String.Map.of_alist_multi)
|> [%sexp_of: (int * Shape.t) list String.Map.t Or_error.t]
|> print_s
;;
let map t =
Tilde_f.Let_syntax.(
Tilde_f.of_local (Map.map t) >>= Tilde_f.of_local_k Result.map >>= Implementer.map)
;;
let of_list ts =
let shape_set = function
| Ok { implement = _; rpc } -> lazy (Shape.Set.singleton (Generic_rpc.shape rpc))
| Error shapes -> shapes
in
List.reduce ts ~f:(fun t1 t2 ->
Map.merge_skewed t1 t2 ~combine:(fun ~key:_ a b ->
Error
(let%map.Lazy a = shape_set a
and b = shape_set b in
Set.union a b)))
|> Option.value ~default:Description.Map.empty
;;
let map_query t = Tilde_f.Let_syntax.(map t >>= Fn.map_input)
let map_response t = Tilde_f.Let_syntax.(map t >>= Fn.map)
let adder t ~rpc ~f = of_list [ t; f rpc ]
let singleton description implementer =
Description.Map.singleton description (Ok implementer)
;;
module Rpc = struct
let singleton rpc =
singleton
(Rpc.Rpc.description rpc)
{ implement = (fun ?on_exception f -> Rpc.Rpc.implement ?on_exception rpc f)
; rpc = Rpc rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_response t = Tilde_f.Let_syntax.(map_response t >>= Deferred.map)
end
module Rpc' = struct
open Async_rpc_kernel
let singleton rpc =
singleton
(Rpc.Rpc.description rpc)
{ implement = (fun ?on_exception f -> Rpc.Rpc.implement' ?on_exception rpc f)
; rpc = Rpc rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_response = map_response
end
module Pipe_rpc = struct
open Async_rpc_kernel
let singleton rpc =
singleton
(Rpc.Pipe_rpc.description rpc)
{ implement = (fun ?on_exception f -> Rpc.Pipe_rpc.implement ?on_exception rpc f)
; rpc = Pipe rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_error t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map_error)
;;
let filter_map_response t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Pipe.filter_map ?max_queue_length:None)
;;
let map_response t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map >>= Pipe.map)
;;
end
module Pipe_rpc_direct = struct
open Async_rpc_kernel
module Direct_stream_writer = Direct_stream_writer
let singleton rpc =
let description = Rpc.Pipe_rpc.description rpc in
let description_sexp = [%sexp_of: Rpc.Description.t] description in
let witness =
Type_equal.Id.create
~name:
[%string "[Babel.Callee.Pipe_rpc_direct] type id for %{description_sexp#Sexp}"]
[%sexp_of: _]
in
singleton
description
{ implement =
(fun ?on_exception f ->
Rpc.Pipe_rpc.implement_direct
?on_exception
rpc
(fun connection_state query writer ->
f
connection_state
query
(Direct_stream_writer.Expert.create_witnessed writer ~witness)))
; rpc = Pipe rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_error t =
Tilde_f.Let_syntax.(
map_response t >>= Fn.map >>= Deferred.map >>= Tilde_f.of_local_k Result.map_error)
;;
let filter_map_response t ~f =
let id = Direct_stream_writer.Expert.Transformation_id.create () in
Tilde_f.Let_syntax.(
map_response t
>>= Fn.map_input
>>= Direct_stream_writer.Expert.filter_map_input_with_id ~id)
~f
;;
let map_response t ~f =
let id = Direct_stream_writer.Expert.Transformation_id.create () in
Tilde_f.Let_syntax.(
map_response t
>>= Fn.map_input
>>= Direct_stream_writer.Expert.map_input_with_id ~id)
~f
;;
end
module State_rpc = struct
open Async_rpc_kernel
let singleton rpc =
singleton
(Rpc.State_rpc.description rpc)
{ implement = (fun ?on_exception f -> Rpc.State_rpc.implement ?on_exception rpc f)
; rpc = State rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_state t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map >>= Tuple2.map_fst)
;;
let map_update t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Tuple2.map_snd
>>= Pipe.map)
;;
let filter_map_update t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Tuple2.map_snd
>>= Pipe.filter_map ?max_queue_length:None)
;;
let map_error t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map_error)
;;
end
module One_way = struct
open Async_rpc_kernel
let singleton rpc =
singleton
(Rpc.One_way.description rpc)
{ implement = (fun ?on_exception f -> Rpc.One_way.implement ?on_exception rpc f)
; rpc = One_way rpc
}
;;
let add = adder ~f:singleton
let map_msg = map_query
end
module Streamable_plain_rpc = struct
let singleton rpc =
singleton
(Streamable.Plain_rpc.description rpc)
{ implement =
(fun ?on_exception f -> Streamable.Plain_rpc.implement ?on_exception rpc f)
; rpc = Streamable_plain rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_response t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Or_error.map)
;;
end
module Streamable_pipe_rpc = struct
let singleton rpc =
singleton
(Streamable.Pipe_rpc.description rpc)
{ implement =
(fun ?on_exception f -> Streamable.Pipe_rpc.implement ?on_exception rpc f)
; rpc = Streamable_pipe rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let filter_map_response t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Pipe.filter_map ?max_queue_length:None)
;;
let map_response t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map >>= Pipe.map)
;;
end
module Streamable_state_rpc = struct
let singleton rpc =
singleton
(Streamable.State_rpc.description rpc)
{ implement =
(fun ?on_exception f -> Streamable.State_rpc.implement ?on_exception rpc f)
; rpc = Streamable_state rpc
}
;;
let add = adder ~f:singleton
let map_query = map_query
let map_state t =
Tilde_f.Let_syntax.(
map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map >>= Tuple2.map_fst)
;;
let map_update t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Tuple2.map_snd
>>= Pipe.map)
;;
let filter_map_update t =
Tilde_f.Let_syntax.(
map_response t
>>= Deferred.map
>>= Tilde_f.of_local_k Result.map
>>= Tuple2.map_snd
>>= Pipe.filter_map ?max_queue_length:None)
;;
end