patch #6160
[mldonkey.git] / src / networks / bittorrent / bTMain.ml
blobec0dbc4f10245ec347f573e15fa9fdfc683fce9a
1 (* Copyright 2001, 2002 b52_simon :), b8_bavard, b8_fee_carabine, INRIA *)
2 (*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 open CommonNetwork
22 open Printf2
23 open BTClients
24 open CommonOptions
25 open CommonFile
26 open CommonComplexOptions
27 open BasicSocket
28 open Options
29 open BTComplexOptions
30 open BTOptions
31 open BTGlobals
32 open BTTypes
33 open CommonTypes
35 let log_prefix = "[bTM]"
37 let lprintf_nl fmt =
38 lprintf_nl2 log_prefix fmt
40 let is_enabled = ref false
42 let disable enabler () =
43 if !enabler then begin
44 is_enabled := false;
45 enabler := false;
46 List.iter (fun file ->
47 Hashtbl2.safe_iter (fun c -> disconnect_client c Closed_by_user)
48 file.file_clients) !current_files;
49 (match !listen_sock with None -> ()
50 | Some sock ->
51 listen_sock := None;
52 TcpServerSocket.close sock Closed_by_user);
53 BTTracker.stop_tracker ();
54 if !!enable_bittorrent then enable_bittorrent =:= false
55 end
57 let enable () =
58 if not !is_enabled then
59 let enabler = ref true in
60 List.iter (fun dir ->
61 Unix2.safe_mkdir dir;
62 Unix2.can_write_to_directory dir;
63 ) [torrents_directory; seeded_directory;
64 tracked_directory; downloads_directory;
65 old_directory; new_torrents_directory];
66 is_enabled := true;
67 if !!BTTracker.tracker_port = !!client_port then
68 begin
69 lprint_newline();
70 lprintf_nl "BT-client_port and BT-tracker_port can not be the same.";
71 lprintf_nl "Change one of the settings and restart MLDonkey, exiting...\n";
72 Pervasives.exit 69
73 end;
74 if !!BTTracker.tracker_port > 0 then (
75 try BTTracker.start_tracker ()
76 with e ->
77 lprintf "Exception in BTTracker.start_tracker: %s\n"
78 (Printexc2.to_string e));
79 if !!share_scan_interval <> 0 then
80 add_session_timer enabler (float_of_int (max 300 (!!share_scan_interval * 60)))
81 (fun _ -> BTInteractive.share_files ();
83 if !!import_new_torrents_interval <> 0. then
84 add_session_timer enabler !!import_new_torrents_interval (fun _ ->
85 BTInteractive.scan_new_torrents_directory ();
87 add_timer 5. (fun _ -> BTInteractive.scan_new_torrents_directory ());
88 add_timer 10. BTInteractive.share_files;
89 add_session_timer enabler 600. BTInteractive.retry_all_ft;
90 network.op_network_disable <- disable enabler;
92 if not !!enable_bittorrent then enable_bittorrent =:= true;
94 BTClients.recover_files ();
95 add_session_timer enabler 60.0 (fun timer ->
96 BTClients.recover_files ();
99 add_session_timer enabler 120.0 (fun timer ->
100 BTClients.send_pings ();
103 add_session_timer enabler 10.0 (fun timer ->
104 BTClients.recompute_uploaders());
106 CommonGlobals.do_at_exit ( fun _ ->
107 List.iter (fun file ->
108 BTClients.file_stop file
109 ) !current_files;
112 BTClients.listen ();
115 let _ =
116 network.op_network_is_enabled <- (fun _ -> !!CommonOptions.enable_bittorrent);
117 option_hook enable_bittorrent (fun _ ->
118 if !CommonOptions.start_running_plugins then
119 if !!enable_bittorrent then network_enable network
120 else network_disable network);
122 network.op_network_save_simple_options <- BTComplexOptions.save_config;
123 network.op_network_load_simple_options <-
124 (fun _ ->
126 Options.load bittorrent_ini;
127 with Sys_error _ ->
128 BTComplexOptions.save_config ()
131 network.op_network_enable <- enable;
132 network.network_config_file <- [bittorrent_ini];
133 check_client_uid ();
134 network.op_network_info <- (fun n ->
136 network_netnum = network.network_num;
137 network_config_filename = (match network.network_config_file with
138 [] -> "" | opfile :: _ -> options_file_name opfile);
139 network_netflags = network.network_flags;
140 network_netname = network.network_name;
141 network_enabled = network_is_enabled network;
142 network_uploaded = !bt_upload_counter;
143 network_downloaded = !bt_download_counter;
144 network_connected_servers = 0;
146 CommonInteractive.register_gui_options_panel "BitTorrent"
147 gui_bittorrent_options_panel
149 let main (toto: int) = ()