Index: src/networks/donkey/donkeyOvernet.ml
===================================================================
RCS file: /sources/mldonkey/mldonkey/src/networks/donkey/donkeyOvernet.ml,v
retrieving revision 1.75
diff -u -r1.75 donkeyOvernet.ml
--- src/networks/donkey/donkeyOvernet.ml	17 Feb 2007 23:47:49 -0000	1.75
+++ src/networks/donkey/donkeyOvernet.ml	24 Feb 2007 16:13:03 -0000
@@ -386,7 +386,7 @@
 
 let overnet_max_known_peers =
   define_option overnet_section [Proto.options_section_name; "max_known_peers"]
-  "maximal number of peers to keep overnet connected (should be >2048)"
+  "OBSOLETE:maximal number of peers to keep overnet connected (should be >2048)"
     int_option 8192
 
 let overnet_search_keyword =
@@ -395,17 +395,17 @@
 
 let overnet_search_timeout =
   define_option overnet_section [Proto.options_section_name; "search_timeout"]
-  "How long shoud a search on Overnet wait for the last answer before terminating"
+  "OBSOLETE:How long shoud a search on Overnet wait for the last answer before terminating"
     int_option 140
 
 let overnet_query_peer_period =
   define_option overnet_section [Proto.options_section_name; "query_peer_period"]
-  "Period between two queries in the overnet tree (should not be set under 5)"
+  "OBSOLETE:Period between two queries in the overnet tree (should not be set under 5)"
     float_option 5.
 
 let overnet_max_search_hits =
   define_option overnet_section [Proto.options_section_name; "max_search_hits"]
-  "Max number of hits in a search on Overnet"
+  "OBSOLETE:Max number of hits in a search on Overnet"
     int_option 200
 
 let overnet_republish =
@@ -415,7 +415,7 @@
 
 let overnet_exclude_peers =
   define_option overnet_section [Proto.options_section_name; "exclude_peers"]
-  "These IP addresses cannot be peers. Elements are separated by spaces, wildcard=255 ie: use 192.168.0.255 for 192.168.0.* "
+  "OBSOLETE:These IP addresses cannot be peers. Elements are separated by spaces, wildcard=255 ie: use 192.168.0.255 for 192.168.0.* "
     ip_list_option [Ip.of_string "1.0.0.0"]
 
 let overnet_md4 = define_option overnet_section
@@ -561,7 +561,7 @@
     "List of IP addresses to use to boot Kademlia networks"
     LimitedList.option (LimitedList.create max_boot_peers)
 
-(* peers we may ping *)
+(* peers we may ping: We got only an ip and port *)
 let unknown_peers = LimitedList.create 2000
 
 (* the total number of buckets used. We must fill a bucket before using the
@@ -574,6 +574,9 @@
   message, because we are not sure for other peers.
   *)
 
+(* FIXME: The function that adds the peers into the buckets should increase 
+   n_used_buckets when needed, but I have the feeling it does not work as expected, 
+   so I set n_used_buckets to some value.*)
 let n_used_buckets = ref 42
 
 (* We distinguish between buckets and prebuckets: peers in buckets are peers
@@ -582,18 +585,19 @@
 let buckets = Array.init 129 (fun _ -> Fifo.create ())
 let prebuckets = Array.init 129 (fun _ -> Fifo.create ())
 
+(* The peers we want to ping in the next 60 seconds *)
 let to_ping = ref []
+
+(* Every peer we have in the buckets und prebuckets goes into known_peers *)
 let known_peers = KnownPeers.create 1023
 
 (*
 We keep the data in buckets depending on the number of bits they have
 in common with our identifier. When we exceed the desired storage,
 we start removing associations from the buckets with the fewest common
-bits. Once every 30 minutes, we remove the associations which are older
-than 1 hour.
+bits. 
 *)
 
-
 (* Argh, we MUST verify how MD4s are compared, from left to right, or from
 right to left ?? *)
 
@@ -626,9 +630,14 @@
 let bucket_number md4 =
   common_bits md4 !!overnet_md4
 
+(* Stats *)
 let search_hits = ref 0
 let source_hits = ref 0
 
+(* To know if we are can receive UDP packets *)
+let global_last_recv = ref 0
+let global_last_send = ref 0
+
 let udp_sock = ref None
 
 let overnet_searches = ref []
@@ -778,16 +787,20 @@
     None -> ()
   | Some sock ->
 (* Why check this? Because it may have been blocked since it was added *)    
-     if ip <> Ip.localhost && is_overnet_ip ip && port <> 0 then
-      Proto.udp_send sock ip port false msg
+     if ip <> Ip.localhost && is_overnet_ip ip && port <> 0 then begin
+      Proto.udp_send sock ip port false msg;
+      global_last_send := last_time ()
+     end
 
 let udp_send_ping ip port msg =
   match !udp_sock with
     None -> ()
   | Some sock ->
 (* Why check this? Because it may have been blocked since it was added *)    
-     if ip <> Ip.localhost && is_overnet_ip ip && port <> 0 then
-      Proto.udp_send sock ip port true msg
+     if ip <> Ip.localhost && is_overnet_ip ip && port <> 0 then begin
+      Proto.udp_send sock ip port true msg;
+      global_last_send := last_time ()
+     end
 
 let udp_send p msg =
   p.peer_last_send <- last_time ();
@@ -833,12 +846,12 @@
                 incr connected_peers;
             end 
             else if Fifo.length prebuckets.(bucket) >= max_peers_per_prebucket then
-	    begin
+            begin
               let pp = Fifo.take prebuckets.(bucket) in
               Fifo.put prebuckets.(bucket)
 (* If the head of the bucket is not dead we should keep it *)
                 (if pp.peer_kind < 4 || pp.peer_expire > last_time () then pp else p);
-	    end  
+            end  
             else begin
                Fifo.put prebuckets.(bucket) p;
                incr pre_connected_peers
@@ -848,7 +861,8 @@
                 p.peer_md4 <> Md4.null then begin
             Fifo.put prebuckets.(!n_used_buckets) p;
             incr pre_connected_peers;
-
+(* increase n_used_buckets if the buckets and prebuckets are full *)
+(* FIXME: Does it work correctly? Until sure, n_used_buckets is set to some value *)
             while !n_used_buckets < 128 &&
               Fifo.length prebuckets.(!n_used_buckets)
               = max_peers_per_bucket do
@@ -1087,6 +1101,7 @@
   (* Update known_peers by other_port:other_ip *)
   let sender = new_peer { dummy_peer with peer_port = other_port ; peer_ip = other_ip } in
   new_peer_message sender;
+  global_last_recv := last_time ();
   match t with
 
   | OvernetConnect p ->
@@ -1096,11 +1111,11 @@
         new_peer_message sender;
         udp_send sender (OvernetConnectReply (get_any_peers 20))
        else
-	 begin
-	   if !verbose_overnet then
+         begin
+           if !verbose_overnet then
        lprintf_nl "Connect: invalid IP %s:%d received from %s:%d"
-	          (Ip.to_string p.peer_ip) p.peer_port (Ip.to_string other_ip) other_port;
-	 end
+                  (Ip.to_string p.peer_ip) p.peer_port (Ip.to_string other_ip) other_port;
+         end
 
   | OvernetConnectReply ps ->
       UdpSocket.declare_pong other_ip;
@@ -1123,10 +1138,10 @@
       if is_overnet_ip sender.peer_ip && sender.peer_port <> 0 then
         udp_send sender (OvernetPublicized (Some (my_peer ())))
        else begin
-	      if !verbose_overnet then
+              if !verbose_overnet then
           lprintf_nl "Publicize: invalid IP %s:%d received from %s:%d"
-	          (Ip.to_string p.peer_ip) p.peer_port (Ip.to_string other_ip) other_port;
-	    end
+                  (Ip.to_string p.peer_ip) p.peer_port (Ip.to_string other_ip) other_port;
+            end
 
   | OvernetPublicized None ->
       ()
@@ -1138,6 +1153,7 @@
       ()
 
   | OvernetSearch (nresults, md4, from_who) ->
+      let nresults = min 10 nresults in
       let peers = get_closest_peers md4 nresults in
       udp_send sender (OvernetSearchReply (md4,peers))
 
@@ -1203,9 +1219,9 @@
                       if is_overnet_ip ip && port <> 0 then
                         let s = DonkeySources.find_source_by_uid
                             (Direct_address (ip, port))  in
-			if !verbose_overnet then
-			  lprintf_nl "added new source %s:%d for file %s"
-			    (Ip.to_string ip) port (Md4.to_string md4);
+                        if !verbose_overnet then
+                          lprintf_nl "added new source %s:%d for file %s"
+                            (Ip.to_string ip) port (Md4.to_string md4);
                         incr source_hits;
                         DonkeySources.set_request_result s
                            file.file_sources File_new_source;
@@ -1375,7 +1391,7 @@
             if p.peer_kind < 3 && Fifo.length b < max_peers_per_bucket then begin
                 Fifo.put b p;
                 incr connected_peers;
-		decr pre_connected_peers;
+                decr pre_connected_peers;
             (* bad peers are removed *)    
             (* Why check is_overnet_ip? Because it may have been blocked since it was added *)
             end else if (p.peer_kind = 4 && p.peer_expire <= last_time ()) || 
@@ -1452,20 +1468,20 @@
           (try
             while (List.length !overnet_searches) < 3 && !current_files <> [] do
               let (file, start, last) = List.hd !current_files in
-	      current_files := List.tl !current_files;
+              current_files := List.tl !current_files;
               if file_state file = FileDownloading &&
-	         (last + 900) < last_time () then begin
-	        ignore (create_search (FileSearch file) file.file_md4);
-   	        current_files2 := (file, start, last_time ()) :: !current_files2;
-	      end else
-   	        current_files2 := (file, start, last) :: !current_files2;
-	    done;
-	  with Failure "hd" -> ()
-	     | Failure "tl" -> (); );
+                 (last + 900) < last_time () then begin
+                ignore (create_search (FileSearch file) file.file_md4);
+                   current_files2 := (file, start, last_time ()) :: !current_files2;
+              end else
+                   current_files2 := (file, start, last) :: !current_files2;
+            done;
+          with Failure "hd" -> ()
+             | Failure "tl" -> (); );
           List.iter (fun (file, start, last) ->
             current_files := (file, start, last) :: !current_files
-	  ) !current_files2;
-       end	
+          ) !current_files2;
+       end        
 
 let enable () =
   if !!enable_overnet && not !is_enabled then begin
@@ -1476,9 +1492,9 @@
           (!!overnet_port) (Proto.udp_handler udp_client_handler)) in
       udp_sock := Some sock;
       if Proto.redirector_section = "DKKO" then
-	overnet_port_info := !!overnet_port;
+        overnet_port_info := !!overnet_port;
       if Proto.redirector_section = "DKKA" then
-	kademlia_port_info := !!overnet_port;
+        kademlia_port_info := !!overnet_port;
       UdpSocket.set_write_controler sock udp_write_controler;
 
 (* copy all boot_peers to unknown_peers *)
@@ -1603,7 +1619,7 @@
       add_session_timer enabler 900. (fun _ ->
           if !!enable_overnet then begin
               ignore(create_search FillBuckets !!overnet_md4);
-	      (* Remove all files not actuall downloading/pause/new etc *)
+              (* Remove all files not actuall downloading/pause/new etc *)
                 (* TODO: Are there states enough? There are:  FileDownloading
                 | FileQueued
                 | FilePaused
@@ -1635,8 +1651,8 @@
       
       add_infinite_timer 1800. (fun _ ->
           if !!enable_overnet then begin
-	      PublishedKeywords.refresh ();
-	      PublishedFiles.refresh ();
+              PublishedKeywords.refresh ();
+              PublishedFiles.refresh ();
             end            
       );
 
@@ -1652,9 +1668,9 @@
             udp_sock := None;
             UdpSocket.close sock Closed_by_user);
       if Proto.redirector_section = "DKKO" then
-	overnet_port_info := 0;
+        overnet_port_info := 0;
       if Proto.redirector_section = "DKKA" then
-	kademlia_port_info := 0;
+        kademlia_port_info := 0;
     end
 
 let _ =
@@ -1716,17 +1732,17 @@
                 | FileSearch _ -> "file"
                 | FillBuckets -> "fillbuckets" )
               (Md4.to_string s.search_md4) s.search_requests s.search_queries (last_time ()-s.search_start) s.search_lifetime;
-	   let pp p = print_peer buf p in
+           let pp p = print_peer buf p in
            Printf.bprintf buf "search_peers\n";
            Array.iter (fun a -> Fifo.iter (fun p -> pp p) a) s.search_peers;
-	   Printf.bprintf buf "search_asked_peers\n";
+           Printf.bprintf buf "search_asked_peers\n";
            KnownPeers.iter pp s.search_asked_peers;
-	   Printf.bprintf buf "search_ok_peers\n";
+           Printf.bprintf buf "search_ok_peers\n";
            KnownPeers.iter pp s.search_ok_peers;
-	   Printf.bprintf buf "search_result_asked_peers\n";
+           Printf.bprintf buf "search_result_asked_peers\n";
            KnownPeers.iter pp s.search_result_asked_peers;
-	   Printf.bprintf buf "\n";
-	 ) !overnet_searches;
+           Printf.bprintf buf "\n";
+         ) !overnet_searches;
          ""
     ), ("<bucket_nr> :\t\tdumps a search (Devel)");
     "dump_bucket", Arg_one (fun i o ->
@@ -1805,18 +1821,46 @@
             html_mods_table_header buf "ovstatsTable" "sources" [];
             Printf.bprintf buf "\\<tr\\>";
             html_mods_td buf [
+              ("", "srh", Printf.sprintf "%s %s\n" (command_prefix_to_net)
+                  (if !!enable_overnet then "is enabled" else "is DISABLED"));
+              ("", "srh", Printf.sprintf "%s Connectivity: %s%s" (command_prefix_to_net)
+                   (if (!connected_peers + !pre_connected_peers) > 20 then "Got enough online peers" else "NOT enough online peers")
+                   (if (abs(!global_last_recv - !global_last_send)) > 60 then " ,there maybe a problem with incoming upd packets" else "" ));
+
+            ];
+            Printf.bprintf buf "\\</tr\\>\\</table\\>\\</div\\>\n";
+
+            Printf.bprintf buf "\\<div class=results\\>";
+            html_mods_table_header buf "ovstatsTable" "sources" [];
+            Printf.bprintf buf "\\<tr\\>";
+            html_mods_td buf [
               ("", "srh", Printf.sprintf "%s statistics" command_prefix_to_net);
               ("", "srh", Printf.sprintf "Search hits: %d\n" !search_hits);
               ("", "srh", Printf.sprintf "Source hits: %d\n" !source_hits); 
               ("", "srh", Printf.sprintf "Current files: %d\n" (List.length !current_files)); ];
+            if !verbose_overnet then begin
+              Printf.bprintf buf "\\</tr\\>\\<tr\\>\n";
+              html_mods_td buf [
+                ("", "srh", Printf.sprintf "Verbose:");
+                ("", "srh", Printf.sprintf "last upd send: %d\n" !global_last_send);
+                ("", "srh", Printf.sprintf "last upd recv: %d\n" !global_last_recv);
+                ("", "srh", Printf.sprintf "diff: %d\n" (abs(!global_last_recv - !global_last_send)));
+              ];
+            end;
             Printf.bprintf buf "\\</tr\\>\\</table\\>\\</div\\>\n";
-          end
+         end
         else
           begin
+            Printf.bprintf buf "%s %s\n" (command_prefix_to_net) 
+               (if !!enable_overnet then "is enabled" else "is DISABLED");
+            Printf.bprintf buf "%s Connectivity: %s%s\n" (command_prefix_to_net)
+                   (if (!connected_peers + !pre_connected_peers) > 20 then "Got enough online peers" else "NOT enough online peers")
+                   (if (abs(!global_last_recv - !global_last_send)) > 60 then " ,there maybe a problem with incoming upd packets" else "");
             Printf.bprintf buf "%s statistics:\n"
-	    (command_prefix_to_net);
+            (command_prefix_to_net);
             Printf.bprintf buf "  Search hits: %d\n" !search_hits;
             Printf.bprintf buf "  Source hits: %d\n" !source_hits;
+            Printf.bprintf buf "  Current files: %d\n" (List.length !current_files);
           end;
 (* Only for debuging current_file
           List.iter (fun (file,start,last) ->
@@ -1872,8 +1916,8 @@
                           i npeers nasked nok nres); ];
                       Printf.bprintf buf "\\</tr\\>";
 
-		    end
-		  else
+                    end
+                  else
                   Printf.bprintf buf
                   "   nbits[%d] = %d peer(s) total, %d peer(s) asked, %d peer(s) ok, %d peer(s) result asked\n"
                     i npeers nasked nok nres
@@ -2082,10 +2126,10 @@
         if !!enable_overnet && !!overnet_update_nodes then
           let n = load_contact_dat filename in
             lprintf_nl "contact.dat loaded from %s, added %d peers" url n;
-	else
-	  if not !!enable_overnet then
+        else
+          if not !!enable_overnet then
       lprintf_nl "Overnet module is disabled, ignoring..."
-	  else
+          else
       lprintf_nl "Overnet_update_nodes is disabled, ignoring..."
         );
 
