1 (** standalone DHT node *)
5 let bracket res destroy k
=
6 let x = try k res
with exn
-> destroy res
; raise exn
in
10 let with_open_in_bin file
= bracket (open_in_bin file
) close_in_noerr
11 let with_open_out_bin file
= bracket (open_out_bin file
) close_out_noerr
13 let load file
: Kademlia.table
= with_open_in_bin file
Marshal.from_channel
15 let store file
(t
:Kademlia.table
) =
16 let temp = file ^
".tmp" in
18 with_open_out_bin temp (fun ch
-> Marshal.to_channel ch t
[]; Unix2.fsync
(Unix.descr_of_out_channel ch
));
21 lprintf_nl ~exn
"write to %S failed" file
; Sys.remove
temp
23 let init file
= try load file
with _
-> Kademlia.create
()
27 "FA959F240D5859CAC30F32ECD21BD89F576481F0";
28 "BDE98D04AB6BD6E8EA7440F82870E5191E130A84";
29 "857224361969AE12066166539538F07BD5EF48B4";
30 "81F643A195BBE3BB1DE1AC9184B9F84D74A37EFF";
31 "7CC9963D90B54DF1710469743C1B43E0E20489C0";
32 "C2C65A1AA5537406183F4D815C77A2A578B00BFB";
33 "72F5A608AFBDF6111E5A86B337E9FC27D6020663";
34 "FE73D74660695208F3ACD221B7A9A128A3D36D47";
37 let id = Kademlia.H.of_hexa
ids.(Random.int (Array.length
ids)) in
38 query_peers dht
id (fun node token peers
->
39 lprintf_nl
"run_queries : %s returned %d peers : %s"
40 (show_node node
) (List.length peers
) (strl
Kademlia.show_addr peers
))
47 let bw = UdpSocket.new_bandwidth_controler
48 (TcpBufferedSocket.create_write_bandwidth_controler
"UNLIMIT" 0) in
49 let dht = start
(init file
) (int_of_string port
) bw in
50 let finish () = store file
dht.M.rt
; stop
dht; exit
0 in
51 Sys.set_signal
Sys.sigint
(Sys.Signal_handle
(fun _
-> show
dht; finish ()));
52 Sys.set_signal
Sys.sigterm
(Sys.Signal_handle
(fun _
-> show
dht; finish ()));
53 Sys.set_signal
Sys.sighup
(Sys.Signal_handle
(fun _
-> show
dht));
54 BasicSocket.add_infinite_timer
1800. (fun () -> run_queries dht);
55 BasicSocket.add_infinite_timer
3600. (fun () -> store file
dht.M.rt
);
56 let routers = ["router.utorrent.com", 6881; "router.transmission.com",6881] in
57 bootstrap
dht ~
routers;
59 | _
-> Printf.eprintf
"Usage : %s <storage> <port>\n" Sys.argv
.(0)
61 exn
-> lprintf_nl
"main : %s" (Printexc.to_string exn
)