mldonkey-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Mldonkey-commits] mldonkey distrib/ChangeLog src/networks/bittorr...


From: mldonkey-commits
Subject: [Mldonkey-commits] mldonkey distrib/ChangeLog src/networks/bittorr...
Date: Sun, 04 Apr 2010 09:16:01 +0000

CVSROOT:        /sources/mldonkey
Module name:    mldonkey
Changes by:     spiralvoice <spiralvoice>       10/04/04 09:16:01

Modified files:
        distrib        : ChangeLog 
        src/networks/bittorrent: bTClients.ml bTInteractive.ml 
                                 bTTorrent.ml bTTracker.ml bencode.ml 

Log message:
        patch #7153 (BT stuff)

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/mldonkey/distrib/ChangeLog?cvsroot=mldonkey&r1=1.1419&r2=1.1420
http://cvs.savannah.gnu.org/viewcvs/mldonkey/src/networks/bittorrent/bTClients.ml?cvsroot=mldonkey&r1=1.102&r2=1.103
http://cvs.savannah.gnu.org/viewcvs/mldonkey/src/networks/bittorrent/bTInteractive.ml?cvsroot=mldonkey&r1=1.151&r2=1.152
http://cvs.savannah.gnu.org/viewcvs/mldonkey/src/networks/bittorrent/bTTorrent.ml?cvsroot=mldonkey&r1=1.19&r2=1.20
http://cvs.savannah.gnu.org/viewcvs/mldonkey/src/networks/bittorrent/bTTracker.ml?cvsroot=mldonkey&r1=1.29&r2=1.30
http://cvs.savannah.gnu.org/viewcvs/mldonkey/src/networks/bittorrent/bencode.ml?cvsroot=mldonkey&r1=1.4&r2=1.5

Patches:
Index: distrib/ChangeLog
===================================================================
RCS file: /sources/mldonkey/mldonkey/distrib/ChangeLog,v
retrieving revision 1.1419
retrieving revision 1.1420
diff -u -b -r1.1419 -r1.1420
--- distrib/ChangeLog   4 Apr 2010 09:14:46 -0000       1.1419
+++ distrib/ChangeLog   4 Apr 2010 09:16:01 -0000       1.1420
@@ -15,6 +15,7 @@
 =========
 
 2010/04/04
+7153: BT: correctly handle failed tracker requests (ygrek)
 7155: DC: understand hublist.xml (ygrek)
 
 2010/04/02

Index: src/networks/bittorrent/bTClients.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/bittorrent/bTClients.ml,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -b -r1.102 -r1.103
--- src/networks/bittorrent/bTClients.ml        2 Apr 2010 09:46:06 -0000       
1.102
+++ src/networks/bittorrent/bTClients.ml        4 Apr 2010 09:16:01 -0000       
1.103
@@ -1348,93 +1348,87 @@
       lprintf_file_nl (as_file file) "Received %s:%d" (Ip.to_string ip) port;
     ()
 
+let exn_catch f x = try `Ok (f x) with exn -> `Exn exn
+
 (** In this function we interact with the tracker
   @param file The file for which we want some sources
   @param need_sources whether we need any sources
 *)
 let talk_to_tracker file need_sources =
+  (* This is the function which will be called by the http client for parsing 
the response *)
   let f t filename =
-    (*This is the function which will be called by the http client
-      for parsing the response
-     *)
-    let tracker_reply =
-      try
-        File.to_string filename
-      with e -> lprintf_file_nl (as_file file) "Empty reply from tracker"; ""
-    in
-    let v =
-       match tracker_reply with
-       | "" ->
-        if !verbose_connect then
-          lprintf_file_nl (as_file file) "Empty reply from tracker";
-        Bencode.decode ""
-       | _ -> Bencode.decode tracker_reply
+    let tracker_failed reason =
+      (* On failure, disable the tracker and count attempts (@see 
is_tracker_enabled) *)
+      let num = match t.tracker_status with | Disabled_failure (i,_) -> i + 1 
| _ -> 1 in
+      t.tracker_status <- Disabled_failure (num, intern reason);
+      lprintf_file_nl (as_file file) "Failure no. %d%s from Tracker %s for 
file: %s Reason: %s"
+        num
+        (if !!tracker_retries = 0 then "" else Printf.sprintf "/%d" 
!!tracker_retries)
+        t.tracker_url file.file_name (Charset.to_utf8 reason)
     in
+    match exn_catch File.to_string filename with
+    | `Exn _ | `Ok "" -> tracker_failed "empty reply"
+    | `Ok s ->
+    match exn_catch Bencode.decode s with
+    | `Exn exn -> tracker_failed (Printf.sprintf "wrong reply (%s)" 
(Printexc2.to_string exn))
+    | `Ok (Dictionary list) ->
     t.tracker_interval <- 600;
     t.tracker_min_interval <- 600;
     if need_sources then t.tracker_last_clients_num <- 0;
-    match v with
-      Dictionary list ->
-        List.iter (fun (key,value) ->
-            (match (key, value) with
-            | String "failure reason", _ -> ()
-            | _ -> (match t.tracker_status with
+        let chk_keyval key n = chk_keyval key n t.tracker_url file.file_name in
+        if not (List.mem_assoc "failure reason" list) then
+        begin
+          begin match t.tracker_status with
                    | Disabled_failure (i, _) ->
-                        lprintf_file_nl (as_file file) "Received good message 
from Tracker %s in file: %s after %d bad attempts"
-                          t.tracker_url file.file_name i
-                   | _ -> ());
+              lprintf_file_nl (as_file file) "Received good message from 
Tracker %s after %d bad attempts" 
+                t.tracker_url i
+          | _ -> () end;
                    (* Received good message from tracker after failures, 
re-enable tracker *)
-                   t.tracker_status <- Enabled);
-
+          t.tracker_status <- Enabled;
+        end;
+        List.iter (fun (key,value) ->
             match (key, value) with
-            | String "failure reason", String failure ->
-                (* On failure, disable the tracker, count the attempts and 
forbid re-enabling *)
-                t.tracker_status <- (match t.tracker_status with
-                  | Disabled_failure (i,_) -> Disabled_failure (i + 1, intern 
failure)
-                  | _ -> Disabled_failure (1, intern failure));
-                lprintf_file_nl (as_file file) "Failure no. %d%s from Tracker 
%s in file: %s Reason: %s"
-                  (match t.tracker_status with | Disabled_failure (i,_) -> i | 
_ -> 1)
-                  (if !!tracker_retries = 0 then "" else Printf.sprintf "/%d" 
!!tracker_retries)
-                  t.tracker_url file.file_name (Charset.to_utf8 failure)
-            | String "warning message", String warning ->
-                lprintf_file_nl (as_file file) "Warning from Tracker %s in 
file: %s Reason: %s" t.tracker_url file.file_name warning
-            | String "interval", Int n ->
-                t.tracker_interval <- chk_keyval (Bencode.print key) n 
t.tracker_url file.file_name;
+            | "failure reason", String failure -> tracker_failed failure
+            | "warning message", String warning ->
+                lprintf_file_nl (as_file file) "Warning from Tracker %s in 
file: %s Reason: %s" 
+                  t.tracker_url file.file_name warning
+            | "interval", Int n ->
+                t.tracker_interval <- chk_keyval key n;
                 (* in case we don't receive "min interval" *)
                 if t.tracker_min_interval > t.tracker_interval then
                   t.tracker_min_interval <- t.tracker_interval
-            | String "min interval", Int n ->
-                t.tracker_min_interval <- chk_keyval (Bencode.print key) n 
t.tracker_url file.file_name;
+            | "min interval", Int n ->
+                t.tracker_min_interval <- chk_keyval key n;
                 (* make sure "min interval" is always < or equal to "interval" 
*)
                 if t.tracker_min_interval > t.tracker_interval then
                   t.tracker_min_interval <- t.tracker_interval
-            | String "downloaded", Int n ->
-                t.tracker_torrent_downloaded <- chk_keyval (Bencode.print key) 
n t.tracker_url file.file_name
-            | String "complete", Int n
-            | String "done peers", Int n ->
-                t.tracker_torrent_complete <- chk_keyval (Bencode.print key) n 
t.tracker_url file.file_name
-            | String "incomplete", Int n ->
-                t.tracker_torrent_incomplete <- chk_keyval (Bencode.print key) 
n t.tracker_url file.file_name;
+            | "downloaded", Int n ->
+                t.tracker_torrent_downloaded <- chk_keyval key n
+            | "complete", Int n
+            | "done peers", Int n ->
+                t.tracker_torrent_complete <- chk_keyval key n
+            | "incomplete", Int n ->
+                t.tracker_torrent_incomplete <- chk_keyval key n;
                 (* if complete > 0 and we receive incomplete we probably won't 
receive num_peers so we simulate it below *)
                 if t.tracker_torrent_complete > 0 then
                   t.tracker_torrent_total_clients_count <- 
(t.tracker_torrent_complete + t.tracker_torrent_incomplete);
-            | String "num peers", Int n ->
-                t.tracker_torrent_total_clients_count <- chk_keyval 
(Bencode.print key) n t.tracker_url file.file_name;
+            | "num peers", Int n ->
+                t.tracker_torrent_total_clients_count <- chk_keyval key n;
                 (* if complete > 0 and we receive num_peers we probably won't 
receive incomplete so we simulate it below *)
                 if t.tracker_torrent_complete > 0 then
                   t.tracker_torrent_incomplete <- 
(t.tracker_torrent_total_clients_count - t.tracker_torrent_complete);
-            | String "last", Int n ->
-                t.tracker_torrent_last_dl_req <- chk_keyval (Bencode.print 
key) n t.tracker_url file.file_name
-            | String "key", String n ->
+            | "last", Int n ->
+                t.tracker_torrent_last_dl_req <- chk_keyval key n
+            | "key", String n ->
                 t.tracker_key <- n;
                 if !verbose_msg_clients then
                   lprintf_file_nl (as_file file) "%s in file: %s has key: %s" 
t.tracker_url file.file_name n
-            | String "tracker id", String n ->
+            | "tracker id", String n ->
                 t.tracker_id <- n;
                 if !verbose_msg_clients then
                   lprintf_file_nl (as_file file) "%s in file: %s has tracker 
id %s" t.tracker_url file.file_name n
 
-            | String "peers", List list ->
+            | "peers", List list ->
                 if need_sources then
                 List.iter (fun v ->
                     match v with
@@ -1445,11 +1439,11 @@
 
                         List.iter (fun v ->
                             match v with
-                              String "peer id", String id ->
+                              "peer id", String id ->
                                 peer_id := Sha1.direct_of_string id;
-                            | String "ip", String ip ->
+                            | "ip", String ip ->
                                 peer_ip := Ip.of_string ip
-                            | String "port", Int p ->
+                            | "port", Int p ->
                                 port := Int64.to_int p
                             | _ -> ()
                         ) list;
@@ -1459,7 +1453,7 @@
 
                     | _ -> assert false
                 ) list
-            | String "peers", String p ->
+            | "peers", String p ->
                 let rec iter_comp s pos l =
                   if pos < l then
                     let ip = Ip.of_ints (get_uint8 s pos,get_uint8 s (pos+1),
@@ -1473,10 +1467,10 @@
                 in
                 if need_sources then 
                   iter_comp p 0 (String.length p)
-            | String "private", Int n -> ()
+            | "private", Int n -> ()
               (* TODO: if set to 1, disable peer exchange *)
 
-            | _ -> lprintf_file_nl (as_file file) "received unknown entry in 
answer from tracker: %s : %s" (Bencode.print key) (Bencode.print value)
+            | key, _ -> lprintf_file_nl (as_file file) "received unknown entry 
in answer from tracker: %s : %s" key (Bencode.print value)
         ) list;
        (*Now, that we have added new clients to a file, it's time
          to connect to them*)
@@ -1485,7 +1479,7 @@
             t.tracker_last_clients_num file.file_name;
         if need_sources then resume_clients file
 
-    | _ -> assert false
+    | _ -> tracker_failed "wrong reply (value)" 
   in
   let event =
     if file.file_tracker_connected then ""
@@ -1566,8 +1560,9 @@
 (*          lprintf "sending piece\n"; *)
           send_client c (Piece (num, pos, upload_buffer, 0, len));
           iter_upload sock c
-  with e -> if !verbose then lprintf_nl
-                   "Exception %s in iter_upload" (Printexc2.to_string e)
+        with e -> 
+          if !verbose then 
+            lprintf_nl "Exception %s in iter_upload" (Printexc2.to_string e)
         end else
         begin
 (*          lprintf "client is waiting for another piece\n"; *)

Index: src/networks/bittorrent/bTInteractive.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/bittorrent/bTInteractive.ml,v
retrieving revision 1.151
retrieving revision 1.152
diff -u -b -r1.151 -r1.152
--- src/networks/bittorrent/bTInteractive.ml    2 Apr 2010 09:46:06 -0000       
1.151
+++ src/networks/bittorrent/bTInteractive.ml    4 Apr 2010 09:16:01 -0000       
1.152
@@ -62,11 +62,11 @@
     match value with
     | Dictionary alist ->
        (try
-          match List.assoc (String "result") alist with
+           match List.assoc "result" alist with
           | Int 1L -> "Port test OK!"
           | Int 0L ->
               (try
-                 match List.assoc (String "reason") alist with
+                  match List.assoc "reason" alist with
                  | String reason -> failure_message "%s" reason
                  | _ -> raise Not_found
                with Not_found ->

Index: src/networks/bittorrent/bTTorrent.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/bittorrent/bTTorrent.ml,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- src/networks/bittorrent/bTTorrent.ml        1 Apr 2010 16:03:53 -0000       
1.19
+++ src/networks/bittorrent/bTTorrent.ml        4 Apr 2010 09:16:01 -0000       
1.20
@@ -110,23 +110,21 @@
 
             List.iter (fun (key, value) ->
                 match key, value with
-                  String "path", List path ->
+                | "path", List path ->
                     current_file := path_list_to_string path;
                     if !verbose_msg_servers then
                       lprintf_nl "[BT] Parsed a new path: [%s]" !current_file
-                | String "path.utf-8", List path_utf8 -> 
+                | "path.utf-8", List path_utf8 -> 
                     current_file_utf8 := path_list_to_string path_utf8;
                     if !verbose_msg_servers then
                       lprintf_nl "[BT] Parsed path.utf-8: [%s]" !current_file
-                | String "length", Int n ->
+                | "length", Int n ->
                     length := !length ++ n;
                     current_length := n;
                     length_set := true
 
-                | String key, _ ->
+                | key, _ ->
                     if !verbose_msg_servers then lprintf_nl "[BT] other field 
[%s] with value [%s] in files" key (Bencode.print value)
-                | _ ->
-                    lprintf_nl "[BT] other field in files"
             ) list;
 
             assert (!length_set);
@@ -145,11 +143,11 @@
       Dictionary list ->
         List.iter (fun (key, value) ->
             match key, value with
-              String "announce", String tracker_url ->
+            | "announce", String tracker_url ->
                if !verbose_msg_servers then
                  lprintf_nl "[BT] New tracker added :%s" tracker_url;
                 announce := tracker_url
-            | String "announce-list", List list ->
+            | "announce-list", List list ->
                 List.iter (fun url_list ->
                     let next_urls = ref [] in
                     match url_list with
@@ -178,100 +176,94 @@
                       List.iter (fun url ->
                         lprintf_nl "[BT] New tracker added :%s" url
                         ) !announce_list
-            | String "info", ((Dictionary list) as info) ->
+            | "info", ((Dictionary list) as info) ->
 
                 file_info := info;
                 List.iter (fun (key, value) ->
                     match key, value with
-                    | String "files", List files ->
+                    | "files", List files ->
                         parse_files files
-                    | String "length", Int n ->
+                    | "length", Int n ->
                         length := n
-                    | String "name", String name ->
+                    | "name", String name ->
                         file_name := name
-                    | String "piece length", Int n ->
+                    | "piece length", Int n ->
                         file_piece_size := n
-                    | String "pieces", String pieces ->
+                    | "pieces", String pieces ->
                         file_pieces := pieces
-                    | String "ed2k", String string_ed2k ->
+                    | "ed2k", String string_ed2k ->
                         if !!enable_donkey then
                           file_ed2k_hash := string_ed2k;
                       (* TODO: Add new ed2k download if ed2k hash is available,
                                then merge it with current download *)
-                    | String "sha1", String string_sha1 -> ()
+                    | "sha1", String string_sha1 -> ()
                       (* TODO: Parse sha1 hash *)
 
-                    | String "publisher", String created_by ->
+                    | "publisher", String created_by ->
                       file_created_by := created_by
-                    | String "publisher-url", String publisher_url ->
+                    | "publisher-url", String publisher_url ->
                       file_created_by := !file_created_by ^ " @ " ^ 
publisher_url
 
-                    | String "name.utf-8", String name_utf8 ->
+                    | "name.utf-8", String name_utf8 ->
                         file_name_utf8 := Some name_utf8
 
-                    | String "publisher.utf-8", String publisher_utf8 -> ()
-                    | String "publisher-url.utf-8", String publisher_url_utf8 
-> ()
+                    | "publisher.utf-8", String publisher_utf8 -> ()
+                    | "publisher-url.utf-8", String publisher_url_utf8 -> ()
 
-                    | String "private", Int n ->
+                    | "private", Int n ->
                         (* TODO: if set to 1, only accept peers from tracker *)
                         file_is_private := n;
                         if !verbose_msg_servers &&
                           Int64.to_int !file_is_private = 1 then
                             lprintf_nl "[BT] torrent is private"
-                    | String key, _ ->
+                    | key, _ ->
                         if !verbose_msg_servers then
                           lprintf_nl "[BT] found other field [%s] with value 
[%s] in info" key (Bencode.print value)
-                    | _ ->
-                        lprintf_nl "[BT] other field in info"
                 ) list
 
-            | String "comment", String comment
-            | String "comment.utf-8", String comment ->
+            | "comment", String comment
+            | "comment.utf-8", String comment ->
               file_comment := comment
             (* Next 2 strings are after info sometimes  *)
-            | String "publisher", String created_by ->
+            | "publisher", String created_by ->
               file_created_by := created_by
-            | String "publisher-url", String publisher_url ->
+            | "publisher-url", String publisher_url ->
               file_created_by := !file_created_by ^ " @ " ^ publisher_url
 
-            | String "created by", String created_by ->
+            | "created by", String created_by ->
               file_created_by := created_by
-            | String "creation date", Int creation_date ->
+            | "creation date", Int creation_date ->
               file_creation_date := creation_date
-            | String "modified-by", String modified_by ->
+            | "modified-by", String modified_by ->
               file_modified_by := modified_by
-            | String "encoding", String encoding ->
+            | "encoding", String encoding ->
               file_encoding := encoding
-            | String "codepage", Int codepage ->
+            | "codepage", Int codepage ->
               file_codepage := codepage
-            | String "torrent filename", String torrent_filename ->
+            | "torrent filename", String torrent_filename ->
               file_torrent_filename := torrent_filename
-            | String "nodes", nodes -> ()
+            | "nodes", nodes -> ()
               (* TODO : nodes is a list of DHT Network nodes ,parse and use 
them *)
 
 (*
               file_nodes := nodes
 *)
 
-            | String "azureus_properties", ((Dictionary list) as 
azureus_properties) ->
+            | "azureus_properties", ((Dictionary list) as azureus_properties) 
->
                 file_aps := azureus_properties;
                 List.iter (fun (key, value) ->
                     match key, value with
-                    | String "dht_backup_enable", Int n ->
+                    | "dht_backup_enable", Int n ->
                         file_dht_backup_enable := n;
                         if !verbose_msg_servers &&
                           Int64.to_int !file_dht_backup_enable = 1 then
                             lprintf_nl "[BT] azureus properties : Torrent has 
dht backup"
-                    | String key, _ ->
+                    | key, _ ->
                         if !verbose_msg_servers then
                           lprintf_nl "[BT] found other field [%s] with value 
[%s] in azureus properties" key (Bencode.print value)
-                    | _ ->
-                        lprintf_nl "[BT] other field in azureus properties"
                 ) list
-            | String key, _ ->
+            | key, _ ->
                 if !verbose_msg_servers then lprintf_nl "[BT] found other 
field [%s] with value [%s] after info" key (Bencode.print value)
-            | _ ->
-                lprintf_nl "[BT] other field after info"
         ) list
     | _ -> assert false
   end;
@@ -351,8 +343,8 @@
 
   let encode_file (filename, size) =
     Dictionary [
-      String "length", Int size;
-      String "path", List (List.map
+      "length", Int size;
+      "path", List (List.map
           (fun s -> String s)(Filepath.string_to_path '/' filename));
     ]
   in
@@ -360,20 +352,20 @@
   let files =
     match torrent.torrent_files with
       [] ->
-        String "length", Int torrent.torrent_length
+        "length", Int torrent.torrent_length
     | _ ->
-        String "files",
+        "files",
         List (List.map encode_file torrent.torrent_files)
   in
 
   let info =
     Dictionary [
       files;
-      String "name", String torrent.torrent_name;
-      String "name.utf-8", String torrent.torrent_name_utf8;
-      String "piece length", Int torrent.torrent_piece_size;
-      String "pieces", String pieces;
-      String "private", Int torrent.torrent_private;
+      "name", String torrent.torrent_name;
+      "name.utf-8", String torrent.torrent_name_utf8;
+      "piece length", Int torrent.torrent_piece_size;
+      "pieces", String pieces;
+      "private", Int torrent.torrent_private;
     ]
   in
 
@@ -381,13 +373,13 @@
   let file_id = Sha1.string info_encoded in
   file_id,
   Dictionary [
-    String "announce", String torrent.torrent_announce;
-    String "comment", String torrent.torrent_comment;
-    String "created by", String torrent.torrent_created_by;
-    String "creation date", Int torrent.torrent_creation_date;
-    String "encoding", String torrent.torrent_encoding;
-    String "info", info;
-    String "modified-by", String torrent.torrent_modified_by;
+    "announce", String torrent.torrent_announce;
+    "comment", String torrent.torrent_comment;
+    "created by", String torrent.torrent_created_by;
+    "creation date", Int torrent.torrent_creation_date;
+    "encoding", String torrent.torrent_encoding;
+    "info", info;
+    "modified-by", String torrent.torrent_modified_by;
 (*
     String "nodes", String torrent.torrent_nodes;
 *)

Index: src/networks/bittorrent/bTTracker.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/bittorrent/bTTracker.ml,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- src/networks/bittorrent/bTTracker.ml        1 Apr 2010 16:03:53 -0000       
1.29
+++ src/networks/bittorrent/bTTracker.ml        4 Apr 2010 09:16:01 -0000       
1.30
@@ -144,8 +144,8 @@
 
 let void_message = Bencode.encode (
     Dictionary [
-      String "interval", Int 600L;
-      String "peers", List []
+      "interval", Int 600L;
+      "peers", List []
     ])
 
 let new_tracker info_hash =
@@ -263,27 +263,27 @@
 
           let message =
             Dictionary [
-              String "interval", Int 600L;
-              String "min interval", Int 600L;
-              String "peers", List
+              "interval", Int 600L;
+              "min interval", Int 600L;
+              "peers", List
                 (List.map (fun p ->
                     if no_peer_id = 1 then
                       Dictionary [
-                        String "ip", String (Ip.to_string p.peer_ip);
-                        String "port", Int (Int64.of_int p.peer_port);
+                        "ip", String (Ip.to_string p.peer_ip);
+                        "port", Int (Int64.of_int p.peer_port);
                       ]
                     else
                       Dictionary [
-                        String "peer id", String
+                        "peer id", String
                           (Sha1.direct_to_string p.peer_id);
-                        String "ip", String (Ip.to_string p.peer_ip);
-                        String "port", Int (Int64.of_int p.peer_port);
+                        "ip", String (Ip.to_string p.peer_ip);
+                        "port", Int (Int64.of_int p.peer_port);
                       ]
                 ) !list);
-              String "downloaded", Int (Int64.of_int 
tracker.tracker_downloaded);
-              String "complete", Int (Int64.of_int tracker.tracker_complete);
-              String "incomplete", Int (Int64.of_int 
tracker.tracker_incomplete);
-              String "last", Int (Int64.of_int ((int_of_float 
(Unix.gettimeofday ())) - tracker.tracker_last));
+              "downloaded", Int (Int64.of_int tracker.tracker_downloaded);
+              "complete", Int (Int64.of_int tracker.tracker_complete);
+              "incomplete", Int (Int64.of_int tracker.tracker_incomplete);
+              "last", Int (Int64.of_int ((int_of_float (Unix.gettimeofday ())) 
- tracker.tracker_last));
             ]
           in
           let m = Bencode.encode message in
@@ -389,11 +389,11 @@
         Hashtbl.iter (fun info_hash tracker ->
             files_tracked :=
                 (Dictionary [
-                   String (Sha1.direct_to_string info_hash),
+                   (Sha1.direct_to_string info_hash),
                      Dictionary [
-                       String "complete", Int (Int64.of_int 
tracker.tracker_complete);
-                       String "downloaded", Int (Int64.of_int 
tracker.tracker_downloaded);
-                       String "incomplete", Int (Int64.of_int 
tracker.tracker_incomplete);
+                       "complete", Int (Int64.of_int tracker.tracker_complete);
+                       "downloaded", Int (Int64.of_int 
tracker.tracker_downloaded);
+                       "incomplete", Int (Int64.of_int 
tracker.tracker_incomplete);
                      ];
                  ]) :: !files_tracked;
             if !verbose_msg_servers then begin
@@ -409,7 +409,7 @@
             lprintf_nl "f: (file hash) d: (downloaded) c: (complete) i: 
(incomplete)";
             lprint_string !log_tracked_files;
           end;
-        let message = Dictionary [ String "files", List !files_tracked ] in
+        let message = Dictionary [ "files", List !files_tracked ] in
         let m = Bencode.encode message in
         r.reply_content <- m
 
@@ -462,7 +462,7 @@
       | _ ->
           let message =
             Dictionary [
-              String "failure reason", String (Printexc2.to_string e);
+              "failure reason", String (Printexc2.to_string e);
             ]
           in
           let m = Bencode.encode message in

Index: src/networks/bittorrent/bencode.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/bittorrent/bencode.ml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- src/networks/bittorrent/bencode.ml  1 Apr 2010 16:03:53 -0000       1.4
+++ src/networks/bittorrent/bencode.ml  4 Apr 2010 09:16:01 -0000       1.5
@@ -60,10 +60,10 @@
 
 *)
 type value =
-  String of string
+  | String of string
   | Int of int64
   | List of value list
-  | Dictionary of (value * value) list
+  | Dictionary of (string * value) list
 
 let decode s =
   let len = String.length s in
@@ -108,21 +108,25 @@
           match s.[pos] with
             | 'e' -> Dictionary (List.rev list), (pos+1)
            | _ ->
-              let key, pos = decode s pos len in
+              match decode s pos len with
+              | (String key,pos) ->
               let v, pos = decode s pos len in
               iter s pos len ((key,v) :: list)
+              | _ -> assert false 
        in
        iter s (pos+1) len []
    | _ -> assert false
   in
+  assert (len > 0);
   let (v,pos) = decode s 0 len in
   v
 
 let encode v =
   let buf = Buffer.create 100 in
+  let encode_string s = Printf.bprintf buf "%d:%s" (String.length s) s in
   let rec encode v =
     match v with
-    | String s -> Printf.bprintf buf "%d:%s" (String.length s) s
+    | String s -> encode_string s
     | Int i -> Printf.bprintf buf "i%Lde" i
     | List list ->
        Buffer.add_char buf 'l';
@@ -130,7 +134,7 @@
        Buffer.add_char buf 'e'
     | Dictionary list ->
        Buffer.add_char buf 'd';
-       List.iter (fun (key,v) -> encode key; encode v) (List.sort (fun (String 
s1, _) (String s2, _) -> compare s1 s2) list);
+       List.iter (fun (key,v) -> encode_string key; encode v) (List.sort (fun 
(s1, _) (s2, _) -> compare s1 s2) list);
        Buffer.add_char buf 'e'
   in
   encode v;
@@ -138,13 +142,15 @@
 
 let print b =
   let buf = Buffer.create 100 in
-  let rec print v =
-    match v with
-    | String s ->
+  let print_string s =
        if String.length s > 200 then
           Printf.bprintf buf " \"%s...\"" (String.escaped (String.sub s 0 200))
        else
           Printf.bprintf buf "\"%s\"" (String.escaped s)
+  in
+  let rec print v =
+    match v with
+    | String s -> print_string s
     | Int i -> Printf.bprintf buf " %Ld" i
     | List list ->
        Printf.bprintf buf " [\n";
@@ -152,8 +158,9 @@
        Printf.bprintf buf " ]\n";
     | Dictionary list ->
        Printf.bprintf buf " {\n";
-       List.iter (fun (key,v) -> print key; Buffer.add_string buf " = "; print 
v; Printf.bprintf buf ";\n")  list;
+       List.iter (fun (key,v) -> print_string key; Buffer.add_string buf " = 
"; print v; Printf.bprintf buf ";\n")  list;
        Printf.bprintf buf " }\n";
   in
   print b;
   Buffer.contents buf
+




reply via email to

[Prev in Thread] Current Thread [Next in Thread]