Eigene Dateien/Visual Studio 2005/Projects/DCPlusPlus/DCPlusPlus/ExternalIP.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using NUnit.Framework;
00005 using System.Threading;
00006 using System.Net;
00007 using System.Net.Sockets;
00008 using System.Xml;
00009 using System.IO;
00010 
00011 
00012 namespace DCPlusPlus
00013 {
00021     [TestFixture]
00022     public class ExternalIP
00023     {
00029         public event CompletedEventHandler Completed;
00035         public event ProgressChangedEventHandler ProgressChanged;
00041         public event UnableToFetchEventHandler UnableToFetch;
00046         public delegate void CompletedEventHandler(ExternalIP ex_ip);
00051         public delegate void ProgressChangedEventHandler(ExternalIP ex_ip);
00056         public delegate void UnableToFetchEventHandler(ExternalIP ex_ip);
00057         protected Connection.ErrorCodes error_code = Connection.ErrorCodes.NoErrorYet;
00062         public Connection.ErrorCodes ErrorCode
00063         {
00064             get
00065             {
00066                 return (error_code);
00067             }
00068         }
00069         protected int percentage = 0;
00073         public int Percentage
00074         {
00075             get
00076             {
00077                 return (percentage);
00078             }
00079         }
00080         protected string my_ip;
00084         public string MyIP
00085         {
00086             get
00087             {
00088                 return (my_ip);
00089             }
00090             set
00091             {
00092                 my_ip = value;
00093             }
00094 
00095         }
00096         protected bool busy = false;
00100         public bool IsBusy
00101         {
00102             get
00103             {
00104                 return (busy);
00105             }
00106         }
00110         private WebClient wc = new WebClient();
00111         public ExternalIP()
00112         {
00113             my_ip = "";
00114             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
00115             wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadFileCallback);
00116 
00117         }
00121         private string url = "http://www.lawrencegoetz.com/programs/ipinfo/";
00125         public void FetchIP()
00126         {
00127             if (!busy)
00128             {
00129                 busy = true;
00130                 percentage = 0;
00131                 if (ProgressChanged != null)
00132                     ProgressChanged(this);
00133                 try
00134                 {
00135                     wc.DownloadDataAsync(new Uri(url));
00136                 }
00137                 catch (Exception ex)
00138                 {
00139                     Console.WriteLine("Exception occured during download: " + ex.Message);
00140                 }
00141             }
00142 
00143         }
00147         public void AbortFetch()
00148         {
00149             if (busy)
00150             {
00151                 try
00152                 {
00153                     wc.CancelAsync();
00154                 }
00155                 catch (Exception ex)
00156                 {
00157                     Console.WriteLine("Exception occured during abort: " + ex.Message);
00158                 }
00159                 System.Threading.Thread.Sleep(10);
00160                 //wc.IsBusy
00161                 //wc = new WebClient();
00162                 busy = false;
00163             }
00164         }
00171         private void DownloadFileCallback(object sender, DownloadDataCompletedEventArgs e)
00172         {
00173             try
00174             {
00175                 if (e.Cancelled) return;
00176                 //ASCIIEncoding ascii = new ASCIIEncoding();
00177                 //UTF8Encoding utf = new UTF8Encoding();
00178                 //string page_string = utf.GetString(e.Result);
00179                 if (e.Result.Length <= 0) return;
00180 
00181                 string page_string = "";
00182                 try
00183                 {
00184                     page_string = System.Text.Encoding.Default.GetString(e.Result);
00185                 }
00186                 catch (Exception ex)
00187                 {
00188                     Console.WriteLine("Exception after download: " + ex.Message);
00189                     return;
00190                 }
00191 
00192                 int start = page_string.IndexOf("<h1>Your IP address is<BR>\n");
00193                 if (start != -1)
00194                 {
00195                     string temp_ip = page_string.Substring(start + "<h1>Your IP address is<BR>".Length);
00196                     int end = temp_ip.IndexOf("</h1>");
00197                     if (end != -1)
00198                     {
00199                         temp_ip = temp_ip.Substring(0, end);
00200                         char[] trims = { '\n', ' ' };
00201                         temp_ip = temp_ip.Trim(trims);
00202 
00203                         //Console.WriteLine("temp_ip: '"+temp_ip+"'");
00204                         my_ip = temp_ip;
00205                         busy = false;
00206                         try
00207                         {
00208                             if (Completed != null)
00209                                 Completed(this);
00210                         }
00211                         catch (Exception ex)
00212                         {
00213                             Console.WriteLine("Exception during callback of own external ip resolve: " + ex.Message);
00214                         }
00215                     }
00216                 }
00217             }
00218             catch (Exception out_ex)
00219             {
00220                 Console.WriteLine("Exception during download of ip page: "+out_ex.Message);
00221                 error_code = Connection.ErrorCodes.Exception;
00222                 if (UnableToFetch != null)
00223                     UnableToFetch(this);
00224 
00225             }
00226         }
00233         private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
00234         {
00235             percentage = e.ProgressPercentage;
00236             if (ProgressChanged != null)
00237                 ProgressChanged(this);
00238         }
00239 #region Unit Testing
00243         [Test]
00244         public void TestResolve()
00245         {
00246             Console.WriteLine("Test to resolve own external ip.");
00247             bool wait = true;
00248             ExternalIP ex_ip = new ExternalIP();
00249             ex_ip.Completed += delegate(ExternalIP ex_ip_completed)
00250             {
00251                 Console.WriteLine("");
00252                 Console.WriteLine("Fetch Completed (ip found : " + ex_ip_completed.MyIP + ")");
00253                 Assert.IsTrue(!string.IsNullOrEmpty(ex_ip_completed.MyIP), "no ip address fetched");
00254                 wait = false;
00255             };
00256             ex_ip.FetchIP();
00257             Console.WriteLine("Waiting for data");
00258             DateTime start = DateTime.Now;
00259             while (wait)
00260             {
00261                 if (DateTime.Now - start > new TimeSpan(0, 0, 5))
00262                 {
00263                     Console.WriteLine("");
00264                     Console.WriteLine("Operation took too long");
00265                     wait = false;
00266                     Assert.Fail("Operation took too long");
00267                 }
00268                 Console.Write(".");
00269                 Thread.Sleep(250);
00270             }
00271             Console.WriteLine("External IP resolve Test successful.");
00272         }
00278         [Test]
00279         public void TestResolveFailServiceOffine()
00280         {
00281             Console.WriteLine("Test to fail resolve own external ip.");
00282             bool wait = true;
00283             ExternalIP ex_ip = new ExternalIP();
00284             ex_ip.url = "http://bogus.url";
00285             ex_ip.Completed += delegate(ExternalIP ex_ip_completed)
00286             {
00287                 Console.WriteLine("");
00288                 Console.WriteLine("Fetch Completed (ip found : " + ex_ip_completed.MyIP + ")");
00289                 Assert.Fail("Failed at failing ;-(");
00290             };
00291             ex_ip.UnableToFetch += delegate(ExternalIP ex_ip_unable)
00292             {
00293                 Console.WriteLine("");
00294                 Console.WriteLine("Failed to fetch ip page.");
00295                 wait = false;
00296                 
00297             };
00298 
00299             ex_ip.FetchIP();
00300             Console.WriteLine("Waiting for data");
00301             DateTime start = DateTime.Now;
00302             while (wait)
00303             {
00304                 if (DateTime.Now - start > new TimeSpan(0, 0, 5))
00305                 {
00306                     Console.WriteLine("");
00307                     Console.WriteLine("Operation took too long");
00308                     wait = false;
00309                     Assert.Fail("Operation took too long");
00310                 }
00311                 Console.Write(".");
00312                 Thread.Sleep(250);
00313             }
00314             Console.WriteLine("Failed External IP resolve Test successful.");
00315         }
00316  
00317 #endregion
00318     }
00319 }

Generated on Wed Mar 7 19:09:20 2007 for DCPlusPlus by  doxygen 1.5.1-p1