Index: src/daemon/common/commonClient.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/daemon/common/commonClient.ml,v
retrieving revision 1.5
diff -u -r1.5 commonClient.ml
--- src/daemon/common/commonClient.ml	28 Nov 2003 17:45:41 -0000	1.5
+++ src/daemon/common/commonClient.ml	2 Dec 2003 02:16:52 -0000
@@ -23,18 +23,23 @@
 open Options
 open CommonTypes
 open CommonGlobals
+open BasicSocket
 
 type 'a client_impl = {
     mutable impl_client_type : client_type;
     mutable impl_client_state : host_state;
     mutable impl_client_update : int;
+    mutable impl_client_downloaded : Int64.t * int;
+    mutable impl_client_downloaded_old : Int64.t * int;
+    mutable impl_client_uploaded : Int64.t * int;
+    mutable impl_client_uploaded_old : Int64.t * int;
     mutable impl_client_has_slot : bool;
     mutable impl_client_upload : shared option;
     mutable impl_client_num : int;
     mutable impl_client_val : 'a;
     mutable impl_client_ops : 'a client_ops;
   }
-  
+
 and 'a client_ops = {
     mutable op_client_network : network;
     
@@ -93,11 +98,16 @@
   let c = as_client_impl c in
   c.impl_client_num
   
-let dummy_client_impl = {
+let dummy_client_impl = 
+  {
     impl_client_type = NormalClient;
     impl_client_state = NewHost;
     impl_client_update = 1;
     impl_client_has_slot = false;
+    impl_client_downloaded = (Int64.zero, 0);
+    impl_client_downloaded_old = (Int64.zero, 0);
+    impl_client_uploaded = (Int64.zero, 0);
+    impl_client_uploaded_old = (Int64.zero, 0);
     impl_client_upload = None;
     impl_client_num = 0;
     impl_client_val = 0;
@@ -276,6 +286,72 @@
   (as_client_impl c).impl_client_upload <- sh;
   client_must_update c
   
+let client_downloaded c =
+  let impl = as_client_impl c in
+  match impl.impl_client_downloaded with
+      down, _ -> down
+
+let add_client_downloaded c v =
+  let impl = as_client_impl c in
+  match impl.impl_client_downloaded with down, time ->
+    if last_time () - time >= 20 then
+      impl.impl_client_downloaded_old <- impl.impl_client_downloaded;
+    impl.impl_client_downloaded <- (Int64.add down v, last_time ())
+
+let set_client_downloaded c v =
+  let impl = as_client_impl c in
+  match impl.impl_client_downloaded_old with olddown, oldtime ->
+  match impl.impl_client_downloaded with down, time ->
+    if oldtime = 0 then
+      impl.impl_client_downloaded_old <- 
+        if time <> 0 then
+	  impl.impl_client_downloaded
+	else
+	  (v, last_time ());
+  impl.impl_client_downloaded <- (v, last_time ())
+
+let client_downloaded_rate c =
+  let impl = as_client_impl c in
+  match impl.impl_client_downloaded_old with olddown, oldtime ->
+  match impl.impl_client_downloaded with down, time ->
+    if oldtime = last_time () then 0.
+    else
+      (Int64.to_float (Int64.sub olddown down)) /. 
+      (float_of_int (last_time () - oldtime))
+
+let client_uploaded c =
+  let impl = as_client_impl c in
+  match impl.impl_client_uploaded with
+      up, _ -> up
+
+let add_client_uploaded c v =
+  let impl = as_client_impl c in
+  match impl.impl_client_uploaded with up, time ->
+    if last_time () - time >= 20 then
+      impl.impl_client_uploaded_old <- impl.impl_client_uploaded;
+    impl.impl_client_uploaded <- (Int64.add up v, last_time ())
+
+let set_client_uploaded c v =
+  let impl = as_client_impl c in
+  match impl.impl_client_uploaded_old with oldup, oldtime ->
+  match impl.impl_client_uploaded with up, time ->
+    if oldtime = 0 then
+      impl.impl_client_uploaded_old <- 
+        if time <> 0 then
+	  impl.impl_client_uploaded
+	else
+	  (v, last_time ());
+  impl.impl_client_uploaded <- (v, last_time ())
+
+let client_uploaded_rate c =
+  let impl = as_client_impl c in
+  match impl.impl_client_uploaded_old with oldup, oldtime ->
+  match impl.impl_client_uploaded with up, time ->
+    if oldtime = last_time () then 0.
+    else
+      (Int64.to_float (Int64.sub oldup up)) /. 
+      (float_of_int (last_time () - oldtime))
+
 let set_client_has_a_slot c b = 
   let impl = as_client_impl c in
   if not b && impl.impl_client_has_slot then begin
Index: src/daemon/common/commonClient.mli
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/daemon/common/commonClient.mli,v
retrieving revision 1.2
diff -u -r1.2 commonClient.mli
--- src/daemon/common/commonClient.mli	23 May 2003 17:39:26 -0000	1.2
+++ src/daemon/common/commonClient.mli	2 Dec 2003 02:16:52 -0000
@@ -2,6 +2,10 @@
     mutable impl_client_type : CommonTypes.client_type;
     mutable impl_client_state : CommonTypes.host_state;
     mutable impl_client_update : int;
+    mutable impl_client_downloaded : Int64.t * int;
+    mutable impl_client_downloaded_old : Int64.t * int;
+    mutable impl_client_uploaded : Int64.t * int;
+    mutable impl_client_uploaded_old : Int64.t * int;
     mutable impl_client_has_slot : bool;
     mutable impl_client_upload : CommonTypes.shared option;
     mutable impl_client_num : int;
@@ -72,6 +76,14 @@
 val client_enter_upload_queue : CommonTypes.client -> unit
 val client_upload : CommonTypes.client -> CommonTypes.shared option
 val set_client_upload : CommonTypes.client -> CommonTypes.shared option -> unit
+val client_downloaded : CommonTypes.client -> Int64.t
+val client_downloaded_rate : CommonTypes.client -> float
+val add_client_downloaded : CommonTypes.client -> Int64.t -> unit
+val set_client_downloaded : CommonTypes.client -> Int64.t -> unit
+val client_uploaded : CommonTypes.client -> Int64.t
+val client_uploaded_rate : CommonTypes.client -> float
+val add_client_uploaded : CommonTypes.client -> Int64.t -> unit
+val set_client_uploaded : CommonTypes.client -> Int64.t -> unit
 val client_has_a_slot : CommonTypes.client -> bool
 val set_client_has_a_slot : CommonTypes.client -> bool -> unit
   
Index: src/daemon/common/commonUploads.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/daemon/common/commonUploads.ml,v
retrieving revision 1.9
diff -u -r1.9 commonUploads.ml
--- src/daemon/common/commonUploads.ml	27 Nov 2003 18:33:32 -0000	1.9
+++ src/daemon/common/commonUploads.ml	2 Dec 2003 02:16:52 -0000
@@ -603,7 +603,15 @@
 and find_pending_slot () =
   try
     let rec iter () =
-      let c = Intmap.top !pending_slots_map in
+      let score c = client_downloaded_rate c in
+      let top = Intmap.top !pending_slots_map in
+      let (c, sc) = 
+	Intmap.fold (fun _ c2 (c, sc) ->
+	  let sc2 = score c2 in
+	  if sc2 > sc then (c2, sc2) else (c, sc)
+        ) !pending_slots_map (top, score top) in
+      lprintf "give slot to client %d (rate: %.2f kB/s)" (client_num c) (sc /. 1024.);
+      lprint_newline ();
       give_a_slot c
     in
     iter ()
Index: src/networks/donkey/donkeyGlobals.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/networks/donkey/donkeyGlobals.ml,v
retrieving revision 1.12
diff -u -r1.12 donkeyGlobals.ml
--- src/networks/donkey/donkeyGlobals.ml	30 Nov 2003 01:25:19 -0000	1.12
+++ src/networks/donkey/donkeyGlobals.ml	2 Dec 2003 02:16:53 -0000
@@ -442,8 +442,6 @@
             port de chat du client ? *)
       client_connected = false;
       client_power = 0;
-      client_downloaded = Int64.zero;
-      client_uploaded = Int64.zero;
       client_banned = false;
       client_has_a_slot = false;
       client_overnet = false;
@@ -499,8 +497,6 @@
       client_chat_port = 0 ; (** A VOIR : où trouver le 
             port de chat du client ? *)
       client_connected = false;
-      client_downloaded = Int64.zero;
-      client_uploaded = Int64.zero;
       client_banned = false;
       client_has_a_slot = false;
       client_overnet = false;
Index: src/networks/donkey/donkeyInteractive.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/networks/donkey/donkeyInteractive.ml,v
retrieving revision 1.16
diff -u -r1.16 donkeyInteractive.ml
--- src/networks/donkey/donkeyInteractive.ml	30 Nov 2003 20:22:23 -0000	1.16
+++ src/networks/donkey/donkeyInteractive.ml	2 Dec 2003 02:16:54 -0000
@@ -865,6 +865,7 @@
       
 let _ =
   client_ops.op_client_info <- (fun c ->
+      let cc = as_client c.client_client in
       {
         P.client_network = network.network_num;
         P.client_kind = c.client_kind;
@@ -878,8 +879,8 @@
         P.client_chat_port = c.client_chat_port ;
         P.client_connect_time = c.client_connect_time;
         P.client_software = gbrand_to_string c.client_brand;
-        P.client_downloaded = c.client_downloaded;
-        P.client_uploaded = c.client_uploaded;
+        P.client_downloaded = client_downloaded cc;
+        P.client_uploaded = client_uploaded cc;
 (*        P.client_sock_addr =    (); *)
         P.client_upload = 
         (match c.client_upload with
@@ -1053,7 +1054,8 @@
       
       begin
         try 
-          let i = (client_info (as_client c.client_client)) in
+	  let cc = as_client c.client_client in
+          let i = client_info cc in
           
           Printf.bprintf buf "\\<td title=\\\"Add as Friend\\\" class=\\\"srb ar\\\"
 		onClick=\\\"parent.fstatus.location.href='submit?q=friend_add+%d'\\\"\\>%d\\</TD\\>"
@@ -1078,8 +1080,8 @@
             ("", "sr br", match c.client_kind with
                 Known_location (ip,port) -> Printf.sprintf "%s" (Ip.to_string ip)
               | Indirect_location _ -> (string_of_client_addr c));
-            ("", "sr ar", (size_of_int64 c.client_uploaded));
-            ("", "sr ar br", (size_of_int64 c.client_downloaded));
+            ("", "sr ar", (size_of_int64 (client_uploaded cc)));
+            ("", "sr ar br", (size_of_int64 (client_downloaded cc)));
             ("", "sr ar", Printf.sprintf "%d" c.client_rank);
             ("", "sr ar br", Printf.sprintf "%d" c.client_score);
             ("", "sr ar", (let last = c.client_connection_control.control_last_ok in
@@ -1127,7 +1129,8 @@
       
       try
         
-        (match c.client_block with
+	let cc = as_client c.client_client in
+        match c.client_block with
             None -> ()
           | Some b ->  ( 
                 let qfiles = c.client_file_queue in
@@ -1136,14 +1139,12 @@
                     client_print (as_client c.client_client) o;
                     Printf.bprintf buf "client: %s downloaded: %s uploaded: %s" 
                       (brand_to_string c.client_brand) 
-                    (Int64.to_string c.client_downloaded) 
-                    (Int64.to_string c.client_uploaded);
+                    (Int64.to_string (client_downloaded cc)) 
+                    (Int64.to_string (client_uploaded cc));
                     Printf.bprintf buf "\nfilename: %s\n\n" info.GuiTypes.file_name;
                   end;
               
               )
-        
-        )
       
       with _ -> ()
   
@@ -1153,7 +1154,8 @@
       let info = file_info file in
       let buf = o.conn_buf in
       try
-        (match c.client_block with
+	let cc = as_client c.client_client in
+        match c.client_block with
             None -> false 
           | Some b ->  ( 
                 let qfiles = c.client_file_queue in
@@ -1179,8 +1181,8 @@
                       ("", "sr", match c.client_kind with
                           Known_location (ip,port) -> Printf.sprintf "%s" (Ip.to_string ip)
                         | Indirect_location _ -> (string_of_client_addr c));
-                      ("", "sr ar", (size_of_int64 c.client_uploaded));
-                      ("", "sr ar", (size_of_int64 c.client_downloaded));
+                      ("", "sr ar", (size_of_int64 (client_uploaded cc)));
+                      ("", "sr ar", (size_of_int64 (client_downloaded cc)));
                       ("", "sr", info.GuiTypes.file_name) ];
                     
                     Printf.bprintf buf "\\</tr\\>";
@@ -1188,7 +1190,6 @@
                   end
                 else false;
               )
-        )
       with _ -> false;
   )
   
Index: src/networks/donkey/donkeyStats.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/networks/donkey/donkeyStats.ml,v
retrieving revision 1.3
diff -u -r1.3 donkeyStats.ml
--- src/networks/donkey/donkeyStats.ml	12 Oct 2003 18:29:37 -0000	1.3
+++ src/networks/donkey/donkeyStats.ml	2 Dec 2003 02:16:54 -0000
@@ -27,6 +27,7 @@
 open CommonTypes
 open CommonGlobals
 open CommonNetwork
+open CommonClient
 open CommonMessages
 open CommonInteractive
   
@@ -116,8 +117,9 @@
 	    !!gstats_by_brand.(brand_to_int b).brand_filerequest + 1
 
 let count_download c f v =
+  let cc = as_client c.client_client in
   download_counter := Int64.add !download_counter v;
-  c.client_downloaded <- Int64.add c.client_downloaded v;
+  add_client_downloaded cc v;
   stats_all.brand_download <- Int64.add stats_all.brand_download v;
   match c.client_brand with
       Brand_unknown -> ()
@@ -128,8 +130,9 @@
         Int64.add !!gstats_by_brand.(brand_to_int b).brand_download v
 
 let count_upload c f v =
+  let cc = as_client c.client_client in
   upload_counter := Int64.add !upload_counter v;
-  c.client_uploaded <- Int64.add c.client_uploaded v;
+  add_client_uploaded cc v;
   stats_all.brand_upload <- Int64.add stats_all.brand_upload v;
   match c.client_brand with
       Brand_unknown -> failwith "unknown client type"
Index: src/networks/donkey/donkeyTypes.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/networks/donkey/donkeyTypes.ml,v
retrieving revision 1.8
diff -u -r1.8 donkeyTypes.ml
--- src/networks/donkey/donkeyTypes.ml	29 Nov 2003 20:36:13 -0000	1.8
+++ src/networks/donkey/donkeyTypes.ml	2 Dec 2003 02:16:54 -0000
@@ -233,8 +233,6 @@
     mutable client_connected : bool;
 (* statistics *)
     mutable client_last_filereqs : int;    
-    mutable client_downloaded : Int64.t;
-    mutable client_uploaded : Int64.t;
     mutable client_brand : brand;
     mutable client_banned : bool;
     mutable client_has_a_slot : bool;
