63 |
open BTChooser |
open BTChooser |
64 |
open TcpMessages |
open TcpMessages |
65 |
|
|
66 |
|
(* prints a new logline with date, module and starts newline *) |
67 |
|
let lprintf_nl () = |
68 |
|
lprintf "%s[BT]: " |
69 |
|
(log_time ()); lprintf_nl2 |
70 |
|
|
71 |
|
(* prints a new logline with date, module and does not start newline *) |
72 |
|
let lprintf_n () = |
73 |
|
lprintf "%s[BT]: " |
74 |
|
(log_time ()); lprintf |
75 |
|
|
76 |
let http_ok = "HTTP 200 OK" |
let http_ok = "HTTP 200 OK" |
77 |
let http11_ok = "HTTP/1.1 200 OK" |
let http11_ok = "HTTP/1.1 200 OK" |
78 |
|
|
148 |
then |
then |
149 |
begin |
begin |
150 |
if !verbose_msg_servers then |
if !verbose_msg_servers then |
151 |
lprintf "get_sources_from_tracker: tracker_connected:%s last_clients:%i last_conn-last_time:%i file: %s\n" |
lprintf_nl () "get_sources_from_tracker: tracker_connected:%s last_clients:%i last_conn-last_time:%i file: %s" |
152 |
(string_of_bool file.file_tracker_connected) t.tracker_last_clients_num |
(string_of_bool file.file_tracker_connected) t.tracker_last_clients_num |
153 |
(t.tracker_last_conn - last_time()) file.file_name; |
(t.tracker_last_conn - last_time()) file.file_name; |
154 |
|
|
163 |
} in |
} in |
164 |
|
|
165 |
if !verbose_msg_servers then |
if !verbose_msg_servers then |
166 |
lprintf "Request sent to tracker %s for file: %s\n" |
lprintf_nl () "Request sent to tracker %s for file: %s" |
167 |
t.tracker_url file.file_name; |
t.tracker_url file.file_name; |
168 |
H.wget r |
H.wget r |
169 |
(fun fileres -> |
(fun fileres -> |
174 |
end |
end |
175 |
else |
else |
176 |
if !verbose_msg_servers then |
if !verbose_msg_servers then |
177 |
lprintf "Request NOT sent to tracker %s - remaning: %d file: %s\n" |
lprintf_nl () "Request NOT sent to tracker %s - remaning: %d file: %s" |
178 |
t.tracker_url (t.tracker_interval - (last_time () - t.tracker_last_conn)) file.file_name |
t.tracker_url (t.tracker_interval - (last_time () - t.tracker_last_conn)) file.file_name |
179 |
) file.file_trackers |
) file.file_trackers |
180 |
|
|
184 |
for clients that are in next list (and not in current) |
for clients that are in next list (and not in current) |
185 |
*) |
*) |
186 |
let recompute_uploaders () = |
let recompute_uploaders () = |
187 |
if !verbose_upload then lprintf "recompute_uploaders\n"; |
if !verbose_upload then lprintf_nl () "recompute_uploaders"; |
188 |
next_uploaders := choose_uploaders current_files; |
next_uploaders := choose_uploaders current_files; |
189 |
(*Send choke if a current_uploader is not in next_uploaders*) |
(*Send choke if a current_uploader is not in next_uploaders*) |
190 |
List.iter ( fun c -> if ((List.mem c !next_uploaders)==false) then |
List.iter ( fun c -> if ((List.mem c !next_uploaders)==false) then |
221 |
*) |
*) |
222 |
let disconnect_client c reason = |
let disconnect_client c reason = |
223 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
224 |
lprintf_nl "CLIENT %d: disconnected: %s" (client_num c) (string_of_reason reason); |
lprintf_nl () "CLIENT %d: disconnected: %s" (client_num c) (string_of_reason reason); |
225 |
begin |
begin |
226 |
match c.client_sock with |
match c.client_sock with |
227 |
NoConnection -> () |
NoConnection -> () |
294 |
if not ( !must_keep && (client_has_a_slot (as_client c) || c.client_interested)) then |
if not ( !must_keep && (client_has_a_slot (as_client c) || c.client_interested)) then |
295 |
begin |
begin |
296 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
297 |
lprintf_nl "disconnect since download is finished"; |
lprintf_nl () "disconnect since download is finished"; |
298 |
disconnect_client c Closed_by_user |
disconnect_client c Closed_by_user |
299 |
end |
end |
300 |
) file.file_clients |
) file.file_clients |
356 |
Int64Swarmer.verified_bitmap swarmer |
Int64Swarmer.verified_bitmap swarmer |
357 |
in |
in |
358 |
|
|
359 |
if !verbose then lprintf_nl "SENDING Verified bitmap: [%s]" bitmap; |
if !verbose then lprintf_nl () "SENDING Verified bitmap: [%s]" bitmap; |
360 |
|
|
361 |
send_client c (BitField |
send_client c (BitField |
362 |
( |
( |
395 |
try |
try |
396 |
set_lifetime sock 600.; |
set_lifetime sock 600.; |
397 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
398 |
lprintf_nl "client_parse_header %d" counter; |
lprintf_nl () "client_parse_header %d" counter; |
399 |
|
|
400 |
let file = Hashtbl.find files_by_uid file_id in |
let file = Hashtbl.find files_by_uid file_id in |
401 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
402 |
lprintf_nl "file found"; |
lprintf_nl () "file found"; |
403 |
let c = |
let c = |
404 |
match !cc with |
match !cc with |
405 |
None -> |
None -> |
406 |
let c = new_client file Sha1.null (TcpBufferedSocket.peer_addr sock) in |
let c = new_client file Sha1.null (TcpBufferedSocket.peer_addr sock) in |
407 |
if !verbose_connect then lprintf_nl "CLIENT %d: incoming CONNECTION" (client_num c); |
if !verbose_connect then lprintf_nl () "CLIENT %d: incoming CONNECTION" (client_num c); |
408 |
cc := Some c; |
cc := Some c; |
409 |
c |
c |
410 |
| Some c -> |
| Some c -> |
441 |
|
|
442 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then begin |
443 |
let (ip,port) = c.client_host in |
let (ip,port) = c.client_host in |
444 |
lprintf_nl "[BT]: CLIENT %d: Connected (%s:%d)" (client_num c) |
lprintf_nl () "[BT]: CLIENT %d: Connected (%s:%d)" (client_num c) |
445 |
(Ip.to_string ip) port; |
(Ip.to_string ip) port; |
446 |
end; |
end; |
447 |
|
|
448 |
(match c.client_sock with |
(match c.client_sock with |
449 |
NoConnection -> |
NoConnection -> |
450 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
451 |
lprintf_nl "[BT]: Can't connect to client !!!"; |
lprintf_nl () "[BT]: Can't connect to client !!!"; |
452 |
c.client_sock <- Connection sock |
c.client_sock <- Connection sock |
453 |
| ConnectionWaiting token -> |
| ConnectionWaiting token -> |
454 |
cancel_token token; |
cancel_token token; |
455 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
456 |
lprintf_nl "[BT]: Waiting for connection to client !!!"; |
lprintf_nl () "[BT]: Waiting for connection to client !!!"; |
457 |
c.client_sock <- Connection sock |
c.client_sock <- Connection sock |
458 |
| Connection s when s != sock -> |
| Connection s when s != sock -> |
459 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
460 |
lprintf_nl "[BT]: CLIENT %d: IMMEDIATE RECONNECTION" (client_num c); |
lprintf_nl () "[BT]: CLIENT %d: IMMEDIATE RECONNECTION" (client_num c); |
461 |
disconnect_client c (Closed_for_error "Reconnected"); |
disconnect_client c (Closed_for_error "Reconnected"); |
462 |
c.client_sock <- Connection sock; |
c.client_sock <- Connection sock; |
463 |
| Connection _ -> () |
| Connection _ -> () |
471 |
end; |
end; |
472 |
connection_ok c.client_connection_control; |
connection_ok c.client_connection_control; |
473 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
474 |
lprintf_nl "[BT]: file and client found"; |
lprintf_nl () "[BT]: file and client found"; |
475 |
(* if not c.client_incoming then *) |
(* if not c.client_incoming then *) |
476 |
send_bitfield c; |
send_bitfield c; |
477 |
c.client_blocks_sent <- file.file_blocks_downloaded; |
c.client_blocks_sent <- file.file_blocks_downloaded; |
496 |
| Not_found -> |
| Not_found -> |
497 |
let (ip,port) = (TcpBufferedSocket.peer_addr sock) in |
let (ip,port) = (TcpBufferedSocket.peer_addr sock) in |
498 |
if !verbose_unexpected_messages then |
if !verbose_unexpected_messages then |
499 |
lprintf_nl "[BT]: %s:%d requested a file that is not shared [%s]" |
lprintf_nl () "%s:%d requested a file that is not shared [%s]" |
500 |
(Ip.to_string ip) port (Sha1.to_hexa file_id) |
(Ip.to_string ip) port (Sha1.to_hexa file_id) |
501 |
| e -> |
| e -> |
502 |
lprintf_nl "[BT]: Exception %s in client_parse_header" (Printexc2.to_string e); |
lprintf_nl () "Exception %s in client_parse_header" (Printexc2.to_string e); |
503 |
close sock (Closed_for_exception e); |
close sock (Closed_for_exception e); |
504 |
raise e |
raise e |
505 |
|
|
561 |
try |
try |
562 |
let num, x,y, r = |
let num, x,y, r = |
563 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then begin |
564 |
lprintf_nl "[BT]: CLIENT %d: Finding new range to send" (client_num c); |
lprintf_nl () "CLIENT %d: Finding new range to send" (client_num c); |
565 |
end; |
end; |
566 |
|
|
567 |
if !verbose_swarming then begin |
if !verbose_swarming then begin |
568 |
lprintf_nl "[BT]: Current download:\n Current chunks: "; |
lprintf_n () "Current download:\n Current chunks: "; |
569 |
List.iter (fun (x,y) -> lprintf "%Ld-%Ld " x y) c.client_chunks; |
List.iter (fun (x,y) -> lprintf "%Ld-%Ld " x y) c.client_chunks; |
570 |
lprintf_nl "\n[BT]: Current ranges: "; |
lprint_newline (); |
571 |
|
lprintf_n () "Current ranges: "; |
572 |
List.iter (fun (p1,p2, r) -> |
List.iter (fun (p1,p2, r) -> |
573 |
let (x,y) = Int64Swarmer.range_range r |
let (x,y) = Int64Swarmer.range_range r |
574 |
in |
in |
575 |
lprintf "%Ld-%Ld[%Ld-%Ld] " p1 p2 x y) c.client_ranges_sent; |
lprintf "%Ld-%Ld[%Ld-%Ld] " p1 p2 x y) c.client_ranges_sent; |
576 |
(match c.client_range_waiting with |
(match c.client_range_waiting with |
577 |
None -> () |
None -> () |
578 |
| Some (x,y,r) -> lprintf "Waiting %Ld-%Ld\n" x y); |
| Some (x,y,r) -> lprintf "Waiting %Ld-%Ld" x y); |
579 |
lprintf_nl "\nBT: Current block: "; |
lprint_newline (); |
580 |
|
lprintf_n () "Current block: "; |
581 |
(match c.client_block with |
(match c.client_block with |
582 |
None -> lprintf_nl "none" |
None -> lprintf "none" |
583 |
| Some b -> Int64Swarmer.print_block b); |
| Some b -> Int64Swarmer.print_block b); |
584 |
lprintf_nl "\nBT: Finding Range:"; |
lprint_newline (); |
585 |
|
lprintf_nl () "Finding Range:"; |
586 |
end; |
end; |
587 |
try |
try |
588 |
(*We must find a block to request first, and then |
(*We must find a block to request first, and then |
592 |
match c.client_block with |
match c.client_block with |
593 |
None -> |
None -> |
594 |
if !verbose_swarming then |
if !verbose_swarming then |
595 |
lprintf_nl "No block"; |
lprintf_nl () "No block"; |
596 |
update_client_bitmap c; |
update_client_bitmap c; |
597 |
(try Int64Swarmer.verify_one_chunk swarmer with _ -> ()); |
(try Int64Swarmer.verify_one_chunk swarmer with _ -> ()); |
598 |
(*Find a free block in the swarmer*) |
(*Find a free block in the swarmer*) |
599 |
let b = Int64Swarmer.find_block up in |
let b = Int64Swarmer.find_block up in |
600 |
if !verbose_swarming then begin |
if !verbose_swarming then begin |
601 |
lprintf "[BT]: Block Found: "; Int64Swarmer.print_block b; |
lprintf_n () "Block Found: "; Int64Swarmer.print_block b; |
602 |
|
lprint_newline () |
603 |
end; |
end; |
604 |
c.client_block <- Some b; |
c.client_block <- Some b; |
605 |
(*We put the found block in client_block to |
(*We put the found block in client_block to |
609 |
iter () |
iter () |
610 |
| Some b -> |
| Some b -> |
611 |
if !verbose_swarming then begin |
if !verbose_swarming then begin |
612 |
lprintf "[BT]: Current Block: "; Int64Swarmer.print_block b; |
lprintf_n () "Current Block: "; Int64Swarmer.print_block b; |
613 |
|
lprint_newline () |
614 |
end; |
end; |
615 |
try |
try |
616 |
(*Given a block find a range inside*) |
(*Given a block find a range inside*) |
630 |
(* Int64Swarmer.alloc_range r; *) |
(* Int64Swarmer.alloc_range r; *) |
631 |
let num = Int64Swarmer.block_num swarmer b in |
let num = Int64Swarmer.block_num swarmer b in |
632 |
if !verbose_swarming then |
if !verbose_swarming then |
633 |
lprintf_nl "[BT]: Asking %d For Range %Ld-%Ld" num x y; |
lprintf_nl () "Asking %d For Range %Ld-%Ld" num x y; |
634 |
|
|
635 |
num, x -- file.file_piece_size ** Int64.of_int num, y -- x, r |
num, x -- file.file_piece_size ** Int64.of_int num, y -- x, r |
636 |
with Not_found -> |
with Not_found -> |
637 |
(*If we don't find a range to request inside the block, |
(*If we don't find a range to request inside the block, |
638 |
iter to choose another block*) |
iter to choose another block*) |
639 |
if !verbose_swarming then |
if !verbose_swarming then |
640 |
lprintf_nl "[BT]: Could not find range in current block"; |
lprintf_nl () "Could not find range in current block"; |
641 |
(* c.client_blocks <- List2.removeq b c.client_blocks; *) |
(* c.client_blocks <- List2.removeq b c.client_blocks; *) |
642 |
c.client_block <- None; |
c.client_block <- None; |
643 |
iter () |
iter () |
650 |
a block to ask) |
a block to ask) |
651 |
*) |
*) |
652 |
if !verbose_swarming then |
if !verbose_swarming then |
653 |
lprintf_nl "[BT]: Unable to get a block !!"; |
lprintf_nl () "Unable to get a block !!"; |
654 |
Int64Swarmer.compute_bitmap swarmer; |
Int64Swarmer.compute_bitmap swarmer; |
655 |
check_finished swarmer file; |
check_finished swarmer file; |
656 |
raise Not_found |
raise Not_found |
657 |
in |
in |
658 |
send_client c (Request (num,x,y)); |
send_client c (Request (num,x,y)); |
659 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
660 |
lprintf_nl "[BT]: CLIENT %d: Asking %s For Range %Ld-%Ld" |
lprintf_nl () "CLIENT %d: Asking %s For Range %Ld-%Ld" |
661 |
(client_num c) |
(client_num c) |
662 |
(Sha1.to_string c.client_uid) |
(Sha1.to_string c.client_uid) |
663 |
x y |
x y |
664 |
with Not_found -> |
with Not_found -> |
665 |
if not (Int64Swarmer.check_finished swarmer) && !verbose_hidden_errors then |
if not (Int64Swarmer.check_finished swarmer) && !verbose_hidden_errors then |
666 |
lprintf_nl "[BT]: BTClient.get_from_client ERROR: can't find a block to download and file is not yet finished..." |
lprintf_nl () "BTClient.get_from_client ERROR: can't find a block to download and file is not yet finished..." |
667 |
|
|
668 |
|
|
669 |
(** In this function we match a message sent by a client |
(** In this function we match a message sent by a client |
675 |
and client_to_client c sock msg = |
and client_to_client c sock msg = |
676 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then begin |
677 |
let (timeout, next) = get_rtimeout sock in |
let (timeout, next) = get_rtimeout sock in |
678 |
lprintf_nl "[BT]: CLIENT %d: (%d, %d,%d) Received %s" |
lprintf_nl () "CLIENT %d: (%d, %d,%d) Received %s" |
679 |
(client_num c) |
(client_num c) |
680 |
(last_time ()) |
(last_time ()) |
681 |
(int_of_float timeout) |
(int_of_float timeout) |
714 |
|
|
715 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
716 |
(match c.client_ranges_sent with |
(match c.client_ranges_sent with |
717 |
[] -> lprintf "EMPTY Ranges !!!\n" |
[] -> lprintf_nl () "EMPTY Ranges!" |
718 |
| (p1,p2,r) :: _ -> |
| (p1,p2,r) :: _ -> |
719 |
let (x,y) = Int64Swarmer.range_range r in |
let (x,y) = Int64Swarmer.range_range r in |
720 |
lprintf "Current range %Ld [%d] (asked %Ld-%Ld[%Ld-%Ld])\n" |
lprintf_nl () "Current range %Ld [%d] (asked %Ld-%Ld[%Ld-%Ld])" |
721 |
position len |
position len |
722 |
p1 p2 x y |
p1 p2 x y |
723 |
); |
); |
738 |
|
|
739 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
740 |
(match c.client_ranges_sent with |
(match c.client_ranges_sent with |
741 |
[] -> lprintf_nl "EMPTY Ranges !!!" |
[] -> lprintf_nl () "EMPTY Ranges!" |
742 |
| (p1,p2,r) :: _ -> |
| (p1,p2,r) :: _ -> |
743 |
let (x,y) = Int64Swarmer.range_range r in |
let (x,y) = Int64Swarmer.range_range r in |
744 |
lprintf_nl "Received %Ld [%d] %Ld-%Ld[%Ld-%Ld] -> %Ld" |
lprintf_nl () "Received %Ld [%d] %Ld-%Ld[%Ld-%Ld] -> %Ld" |
745 |
position len |
position len |
746 |
p1 p2 x y |
p1 p2 x y |
747 |
(new_downloaded -- old_downloaded) |
(new_downloaded -- old_downloaded) |
814 |
done; |
done; |
815 |
|
|
816 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
817 |
lprintf_nl "[BT]: BitField translated"; |
lprintf_nl () "BitField translated"; |
818 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
819 |
lprintf_nl "[BT]: Old BitField Unregistered"; |
lprintf_nl () "Old BitField Unregistered"; |
820 |
(match c.client_uploader with |
(match c.client_uploader with |
821 |
None -> assert false |
None -> assert false |
822 |
| Some up -> |
| Some up -> |
826 |
c.client_bitmap <- Some bitmap; |
c.client_bitmap <- Some bitmap; |
827 |
send_interested c; |
send_interested c; |
828 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
829 |
lprintf_nl "[BT]: New BitField Registered"; |
lprintf_nl () "New BitField Registered"; |
830 |
(* for i = 1 to max_range_requests - List.length c.client_ranges do |
(* for i = 1 to max_range_requests - List.length c.client_ranges do |
831 |
(try get_from_client sock c with _ -> ()) |
(try get_from_client sock c with _ -> ()) |
832 |
done*) |
done*) |
871 |
done*) |
done*) |
872 |
end |
end |
873 |
end |
end |
874 |
| None, Some _ -> lprintf "[BT]: no bitmap but client_uploader\n"; |
| None, Some _ -> lprintf_nl () "no bitmap but client_uploader"; |
875 |
| Some _ , None ->lprintf "[BT]: bitmap but no client_uploader\n"; |
| Some _ , None ->lprintf_nl () "bitmap but no client_uploader"; |
876 |
| None, None -> lprintf "[BT]: no bitmap no client_uploader\n"; |
| None, None -> lprintf_nl () "no bitmap no client_uploader"; |
877 |
end |
end |
878 |
*) |
*) |
879 |
end |
end |
891 |
(* Afaik this is no protocol violation and happens if the client |
(* Afaik this is no protocol violation and happens if the client |
892 |
didn't send a client bitmap after the handshake. *) |
didn't send a client bitmap after the handshake. *) |
893 |
let (ip,port) = c.client_host in |
let (ip,port) = c.client_host in |
894 |
if !verbose_msg_clients then lprintf_nl "[BT]: %s:%d with software %s : Choke send, but no client bitmap" |
if !verbose_msg_clients then lprintf_nl () "%s:%d with software %s : Choke send, but no client bitmap" |
895 |
(Ip.to_string ip) port (c.client_software) |
(Ip.to_string ip) port (c.client_software) |
896 |
| Some up -> |
| Some up -> |
897 |
Int64Swarmer.clear_uploader_ranges up |
Int64Swarmer.clear_uploader_ranges up |
953 |
|
|
954 |
| Cancel _ -> () |
| Cancel _ -> () |
955 |
with e -> |
with e -> |
956 |
lprintf_nl "[BT]: Error %s while handling MESSAGE: %s" (Printexc2.to_string e) (TcpMessages.to_string msg) |
lprintf_nl () "Error %s while handling MESSAGE: %s" (Printexc2.to_string e) (TcpMessages.to_string msg) |
957 |
|
|
958 |
|
|
959 |
(** The function used to connect to a client. |
(** The function used to connect to a client. |
969 |
None -> true |
None -> true |
970 |
| Some br -> |
| Some br -> |
971 |
if !verbose_connect then |
if !verbose_connect then |
972 |
lprintf_nl "[BT]: %s:%d blocked: %s" |
lprintf_nl () "%s:%d blocked: %s" |
973 |
(Ip.to_string ip) port br.blocking_description; |
(Ip.to_string ip) port br.blocking_description; |
974 |
false) |
false) |
975 |
then |
then |
979 |
let token = |
let token = |
980 |
add_pending_connection connection_manager (fun token -> |
add_pending_connection connection_manager (fun token -> |
981 |
try |
try |
982 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then |
983 |
lprintf_nl "[BT]: CLIENT %d: connect_client" (client_num c); |
lprintf_nl () "CLIENT %d: connect_client" (client_num c); |
|
end; |
|
984 |
let (ip,port) = c.client_host in |
let (ip,port) = c.client_host in |
985 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then |
986 |
lprintf_nl "[BT]: connecting %s:%d" (Ip.to_string ip) port; |
lprintf_nl () "connecting %s:%d" (Ip.to_string ip) port; |
|
end; |
|
987 |
connection_try c.client_connection_control; |
connection_try c.client_connection_control; |
988 |
begin |
begin |
989 |
let sock = connect token "bittorrent download" |
let sock = connect token "bittorrent download" |
992 |
match event with |
match event with |
993 |
BASIC_EVENT LTIMEOUT -> |
BASIC_EVENT LTIMEOUT -> |
994 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
995 |
lprintf_nl "[BT]: CLIENT %d: LIFETIME" (client_num c); |
lprintf_nl () "CLIENT %d: LIFETIME" (client_num c); |
996 |
close sock Closed_for_timeout |
close sock Closed_for_timeout |
997 |
| BASIC_EVENT RTIMEOUT -> |
| BASIC_EVENT RTIMEOUT -> |
998 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
999 |
lprintf_nl "[BT]: CLIENT %d: RTIMEOUT (%d)" (client_num c) |
lprintf_nl () "CLIENT %d: RTIMEOUT (%d)" (client_num c) |
1000 |
(last_time ()) |
(last_time ()) |
1001 |
; |
; |
1002 |
close sock Closed_for_timeout |
close sock Closed_for_timeout |
1017 |
TcpBufferedSocket.set_rtimeout sock 30.; |
TcpBufferedSocket.set_rtimeout sock 30.; |
1018 |
let file = c.client_file in |
let file = c.client_file in |
1019 |
|
|
1020 |
if !verbose_msg_clients then begin |
if !verbose_msg_clients then |
1021 |
lprintf_nl "[BT]: READY TO DOWNLOAD FILE"; |
lprintf_nl () "READY TO DOWNLOAD FILE"; |
|
end; |
|
1022 |
|
|
1023 |
send_init !!client_uid file.file_id sock; |
send_init !!client_uid file.file_id sock; |
1024 |
(* Fabrice: Initialize the client bitmap and uploader fields to <> None *) |
(* Fabrice: Initialize the client bitmap and uploader fields to <> None *) |
1033 |
(BTHeader (client_parse_header !counter (ref (Some c)) true)) |
(BTHeader (client_parse_header !counter (ref (Some c)) true)) |
1034 |
end |
end |
1035 |
with e -> |
with e -> |
1036 |
lprintf_nl "[BT]: Exception %s while connecting to client" |
lprintf_nl () "Exception %s while connecting to client" |
1037 |
(Printexc2.to_string e); |
(Printexc2.to_string e); |
1038 |
disconnect_client c (Closed_for_exception e) |
disconnect_client c (Closed_for_exception e) |
1039 |
); |
); |
1062 |
to connect to us |
to connect to us |
1063 |
*) |
*) |
1064 |
let ip = (Ip.of_inet_addr from_ip) in |
let ip = (Ip.of_inet_addr from_ip) in |
1065 |
if !verbose_sources > 1 then lprintf_nl "[BT]: CONNECTION RECEIVED FROM %s" |
if !verbose_sources > 1 then lprintf_nl () "CONNECTION RECEIVED FROM %s" |
1066 |
(Ip.to_string (Ip.of_inet_addr from_ip)) |
(Ip.to_string (Ip.of_inet_addr from_ip)) |
1067 |
; |
; |
1068 |
(*Reject this connection if we don't want |
(*Reject this connection if we don't want |
1073 |
None -> true |
None -> true |
1074 |
| Some br -> |
| Some br -> |
1075 |
if !verbose_connect then |
if !verbose_connect then |
1076 |
lprintf_nl "[BT]: %s:%d blocked: %s" |
lprintf_nl () "%s:%d blocked: %s" |
1077 |
(Ip.to_string ip) from_port br.blocking_description; |
(Ip.to_string ip) from_port br.blocking_description; |
1078 |
false) |
false) |
1079 |
then |
then |
1121 |
listen_sock := Some s; |
listen_sock := Some s; |
1122 |
() |
() |
1123 |
with e -> |
with e -> |
1124 |
lprintf_nl "[BT]: Exception %s while init bittorrent server" |
lprintf_nl () "Exception %s while init bittorrent server" |
1125 |
(Printexc2.to_string e) |
(Printexc2.to_string e) |
1126 |
|
|
1127 |
|
|
1191 |
String "interval", Int n -> |
String "interval", Int n -> |
1192 |
t.tracker_interval <- Int64.to_int n |
t.tracker_interval <- Int64.to_int n |
1193 |
| String "failure reason", String failure -> |
| String "failure reason", String failure -> |
1194 |
lprintf "Failure from BT-Tracker %s in file: %s Reason: %s\n" t.tracker_url file.file_name failure |
lprintf_nl () "Failure from Tracker %s in file: %s Reason: %s" t.tracker_url file.file_name failure |
1195 |
| String "complete", Int n -> () (*TODO we should put these two in some var. and perhaps display them in the gui*) |
| String "complete", Int n -> () (*TODO we should put these two in some var. and perhaps display them in the gui*) |
1196 |
| String "incomplete", Int n -> () |
| String "incomplete", Int n -> () |
1197 |
| String "peers", List list -> |
| String "peers", List list -> |
1223 |
None -> true |
None -> true |
1224 |
| Some br -> |
| Some br -> |
1225 |
if !verbose_connect then |
if !verbose_connect then |
1226 |
lprintf_nl "[BT]: %s:%d blocked: %s" |
lprintf_nl () "%s:%d blocked: %s" |
1227 |
(Ip.to_string !peer_ip) !port br.blocking_description; |
(Ip.to_string !peer_ip) !port br.blocking_description; |
1228 |
false) |
false) |
1229 |
then |
then |
1230 |
let c = new_client file !peer_id (!peer_ip,!port) |
let c = new_client file !peer_id (!peer_ip,!port) |
1231 |
in |
in |
1232 |
if !verbose_sources > 1 then lprintf_nl "[BT]: Received %s:%d" (Ip.to_string !peer_ip) |
if !verbose_sources > 1 then lprintf_nl () "Received %s:%d" (Ip.to_string !peer_ip) |
1233 |
!port; |
!port; |
1234 |
() |
() |
1235 |
| _ -> assert false |
| _ -> assert false |
1250 |
| String "private", Int n -> () |
| String "private", Int n -> () |
1251 |
(* TODO: if set to 1, disable peer exchange *) |
(* TODO: if set to 1, disable peer exchange *) |
1252 |
|
|
1253 |
| _ -> lprintf_nl "[BT]: received unknown entry in answer from tracker: %s : %s" (Bencode.print key) (Bencode.print value) |
| _ -> lprintf_nl () "received unknown entry in answer from tracker: %s : %s" (Bencode.print key) (Bencode.print value) |
1254 |
) list; |
) list; |
1255 |
(*Now, that we have added new clients to a file, it's time |
(*Now, that we have added new clients to a file, it's time |
1256 |
to connect to them*) |
to connect to them*) |
1257 |
if !verbose_sources > 0 then |
if !verbose_sources > 0 then |
1258 |
lprintf_nl "[BT]: get_sources_from_tracker: got %i source(s) for file %s" |
lprintf_nl () "get_sources_from_tracker: got %i source(s) for file %s" |
1259 |
t.tracker_last_clients_num file.file_name; |
t.tracker_last_clients_num file.file_name; |
1260 |
resume_clients file |
resume_clients file |
1261 |
|
|
1284 |
(try |
(try |
1285 |
connect_trackers file "" (fun _ _ -> ()) with _ -> ()) |
connect_trackers file "" (fun _ _ -> ()) with _ -> ()) |
1286 |
| FilePaused -> () (*when we are paused we do nothing, not even logging this vvvv*) |
| FilePaused -> () (*when we are paused we do nothing, not even logging this vvvv*) |
1287 |
| s -> lprintf_nl "[BT]: Other state %s!!" (string_of_state s) |
| s -> lprintf_nl () "Other state %s!!" (string_of_state s) |
1288 |
) !current_files |
) !current_files |
1289 |
|
|
1290 |
let upload_buffer = String.create 100000 |
let upload_buffer = String.create 100000 |
1375 |
if file.file_tracker_connected then |
if file.file_tracker_connected then |
1376 |
begin |
begin |
1377 |
connect_trackers file "stopped" (fun _ _ -> |
connect_trackers file "stopped" (fun _ _ -> |
1378 |
lprintf_nl "[BT]: BT-Tracker return: stopped %s" file.file_name; |
lprintf_nl () "Tracker return: stopped %s" file.file_name; |
1379 |
file.file_tracker_connected <- false) |
file.file_tracker_connected <- false) |
1380 |
end |
end |
1381 |
|
|
1397 |
); |
); |
1398 |
client_ops.op_client_enter_upload_queue <- (fun c -> |
client_ops.op_client_enter_upload_queue <- (fun c -> |
1399 |
if !verbose_msg_clients then |
if !verbose_msg_clients then |
1400 |
lprintf_nl "[BT]: CLIENT %d: client_enter_upload_queue" (client_num c); |
lprintf_nl () "CLIENT %d: client_enter_upload_queue" (client_num c); |
1401 |
ready_for_upload (as_client c)); |
ready_for_upload (as_client c)); |
1402 |
network.op_network_connected_servers <- (fun _ -> []); |
network.op_network_connected_servers <- (fun _ -> []); |