Author: remi
Date: 2008-12-11 07:41:50 +0100 (Thu, 11 Dec 2008)
New Revision: 3051

Modified:
   
software_suite_v2/software/http_server_resources/chatter_tux/trunk/ChatterTux/Unit1.pas
Log:
* retrieve the correct network card.
* added a retry counter (10 retry)

Modified: 
software_suite_v2/software/http_server_resources/chatter_tux/trunk/ChatterTux/Unit1.pas
===================================================================
--- 
software_suite_v2/software/http_server_resources/chatter_tux/trunk/ChatterTux/Unit1.pas
     2008-12-09 21:47:16 UTC (rev 3050)
+++ 
software_suite_v2/software/http_server_resources/chatter_tux/trunk/ChatterTux/Unit1.pas
     2008-12-11 06:41:50 UTC (rev 3051)
@@ -88,20 +88,21 @@
 
 {*
  * \brief Check if the internet connection is available.
- * \return true or false.
+ * \return the working ip or None.
  *}
-function getInternetConnectionAvailable : boolean;
+function getInternetConnectionAvailable : string;
 var
   idHTTP : TIdHTTP;
 begin
   idHTTP := TIdHTTP.Create(nil);
-  idHTTP.ConnectTimeout := 6400;
+  idHTTP.ConnectTimeout := 30000;
   idHTTP.Request.CacheControl := 'no-cache';
   idHTTP.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 
5.1)';
-  result := false;
+  idHTTP.HandleRedirects := true;
+  result := 'None';
   try
-    idHTTP.Head('http://www.google.be/');
-    result := true;
+    idHTTP.Head('http://www.google.com/');
+    result := idHTTP.Socket.Binding.IP;
   except
   end;
   idHttp.Disconnect;
@@ -176,6 +177,9 @@
  * On frame creation.
  *}
 procedure TForm1.FormCreate(Sender: TObject);
+var
+  isMonitored : boolean;
+  retryCounter : integer;
 begin
   // Create the socket monitor
   MonitorSocket := TMonitorSocket.Create(self);
@@ -183,58 +187,79 @@
   // Create the splited frame TStringList
   splitedFrame := TStringList.Create;
 
-  // Wait a valid IP address
-  while true do
+  // Initialize the retry counter
+  retryCounter := 10;
+  isMonitored := false;
+
+  // 10 attempts to monitoring the network cards
+  while retryCounter > 0 do
   begin
-    // Need at least one IP address
-    if LocalIPList.Count = 0 then
-    // No IP address - failed
+
+    // Wait a valid IP address
+    while true do
     begin
-      sleep(2000);
-      application.ProcessMessages;
-    end else
-    // At least one IP address found
-    begin
-      // Check internet status
-      if not getInternetConnectionAvailable then
+      // Need at least one IP address
+      if LocalIPList.Count = 0 then
+      // No IP address - failed
       begin
-        continue;
+        sleep(2000);
+        application.ProcessMessages;
       end else
+      // At least one IP address found
       begin
-        sleep(1000);
-        break;
+        // Check internet status
+        myIP := getInternetConnectionAvailable;
+        if myIP = 'None' then
+        begin
+          continue;
+        end else
+        begin
+          sleep(1000);
+          break;
+        end;
       end;
     end;
-  end;
 
-  // Retrieve the local IP
-  if LocalIPList.Count > 0 then
-  begin
-    // Set my ip
-    myIP := LocalIPList[0];
-    sleep(1000);
-    application.ProcessMessages;
+    // Retrieve the local IP
+    if LocalIPList.Count > 0 then
+    begin
+      // Configure the socket monitor
+      isMonitored := false;
+      MonitorSocket.onPacketEvent := PacketEvent;
+      MonitorSocket.Addr := myIP;
+      MonitorSocket.AddrMask := '255.255.255.0';
+      MonitorSocket.IgnoreData := false;
+      MonitorSocket.IgnoreLAN := true;
+      MonitorSocket.ClearIgnoreIP;
 
-    // Configure the socket monitor
-    MonitorSocket.onPacketEvent := PacketEvent;
-    MonitorSocket.Addr := myIP;
-    MonitorSocket.AddrMask := '255.255.255.0';
-    MonitorSocket.IgnoreData := false;
-    MonitorSocket.IgnoreLAN := true;
-    MonitorSocket.ClearIgnoreIP;
-
-    // Start the monitoring
-    try
-      MonitorSocket.StartMonitor(true);
-    except
+      // Start the monitoring
       try
-        MonitorSocket.StartMonitor(false);
+        MonitorSocket.StartMonitor(true);
+        isMonitored := true;
       except
-        // Can't monitoring your network card ...
-        application.Terminate;
+        try
+          MonitorSocket.StartMonitor(false);
+          isMonitored := true;
+        except
+          // Can't monitoring your network card ...
+        end;
       end;
+      if isMonitored then
+      begin
+        break;
+      end;
+    end else
+    begin
+      sleep(1000);
     end;
-  end else
+    
+    // Decrease the retry counter
+    retryCounter := retryCounter - 1;
+
+  end; // Retry loop end (10 attempts)
+
+  // No monitoring found then exit the application
+  if not isMonitored then
   begin
     application.Terminate;
   end;


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to