forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_xmlrpc.ml
52 lines (47 loc) · 1.5 KB
/
plugin_xmlrpc.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
(*
* (c) 2005-2010 Anastasia Gornostaeva
*)
open Unix
open Xml
let proc ev event from (xml:element) (out:element -> unit) =
Event.sync (Event.send ev xml)
let sulci_rpc out in_chan out_chan () =
let xml = input_value in_chan in
match get_tagname xml with
| "iq" ->
let id = get_attr_s xml "id" in
let ev = Event.new_channel () in
Hooks.register_handle (Hooks.Id (id, proc ev));
out xml;
let reply = Event.sync (Event.receive ev) in
output_value out_chan reply;
flush out_chan;
close_out out_chan
| _ -> (* todo: figure our how to send *)
close_out out_chan
let _ =
let port = 5221 in
let inet_addr = inet_addr_any in
let sockaddr = ADDR_INET (inet_addr, port) in
let fd = socket PF_INET SOCK_STREAM 0 in
let () = setsockopt fd SO_REUSEADDR true in
let server out =
try
Unix.bind fd sockaddr;
Unix.listen fd 10;
while true do
let client, _ = Unix.accept fd in
let in_chan = Unix.in_channel_of_descr client
and out_chan = Unix.out_channel_of_descr client in
ignore (Thread.create (sulci_rpc out in_chan out_chan) ())
done
with Unix.Unix_error (code, syscall, _) ->
Logger.out
(Printf.sprintf "problem with %s: %s\n" syscall
(Unix.error_message code));
Thread.exit ()
in
let start out =
ignore (Thread.create server out)
in
Hooks.register_handle (Hooks.OnStart start)