DCPlusPlus.ExternalIP Class Reference

A simple class to retrieve your own IP address (if you are using a router it may be differing from the network cards address in your computer, so we need a way to get the ip you are visible to other clients). More...

Collaboration diagram for DCPlusPlus.ExternalIP:

Collaboration graph
[legend]
List of all members.

Public Member Functions

void AbortFetch ()
 Manually abort the retrieval.
delegate void CompletedEventHandler (ExternalIP ex_ip)
 Prototype for the Completed Event Handler.
 ExternalIP ()
void FetchIP ()
 Start an async retrieval of our external ip address.
delegate void ProgressChangedEventHandler (ExternalIP ex_ip)
 Prototype for the Progress Changed Event Handler.
[Test] void TestResolve ()
 Test to check if we can successfully retrieve our ip address.
[Test] void TestResolveFailServiceOffine ()
 Test to see if a failed resolve throws exceptions we do not catch or our application may crash during the retrieval.
delegate void UnableToFetchEventHandler (ExternalIP ex_ip)
 Prototype for the Unable To Fetch Event Handler.

Public Attributes

event CompletedEventHandler Completed
 Event handler that gets called when an ip address was retrieved after calling FetchIP().
event ProgressChangedEventHandler ProgressChanged
 Event handler that gets called when the progress of the FetchIP() operation changed.
event UnableToFetchEventHandler UnableToFetch
 Event handler that gets called when the operation was unable to finish the ip retrieval during an error.

Protected Attributes

bool busy = false
Connection.ErrorCodes error_code = Connection.ErrorCodes.NoErrorYet
string my_ip
int percentage = 0

Properties

Connection.ErrorCodes ErrorCode [get]
 Get the error code if something went wrong and you want to tell the user the 'exact' cause.
bool IsBusy [get]
 Get the Status of the retrieval.
string MyIP [get, set]
 Get the ip address FetchIP() retrieved.
int Percentage [get]
 Get the progress percentage of the retrieval operation.

Private Member Functions

void DownloadFileCallback (object sender, DownloadDataCompletedEventArgs e)
 Async callback for webclients get file operation ,gets called if the file was retrieved successfully.
void DownloadProgressCallback (object sender, DownloadProgressChangedEventArgs e)
 Async callback for webclients get file operation ,gets called when the progress of the download changes.

Private Attributes

string url = "http://www.lawrencegoetz.com/programs/ipinfo/"
 the url of the ip fetching service we use
WebClient wc = new WebClient()
 our weblclient instance we use to get the ip

Detailed Description

A simple class to retrieve your own IP address (if you are using a router it may be differing from the network cards address in your computer, so we need a way to get the ip you are visible to other clients).

Definition at line 22 of file ExternalIP.cs.


Constructor & Destructor Documentation

DCPlusPlus.ExternalIP.ExternalIP (  ) 

Definition at line 111 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.DownloadFileCallback(), DCPlusPlus.ExternalIP.DownloadProgressCallback(), DCPlusPlus.ExternalIP.my_ip, and DCPlusPlus.ExternalIP.wc.

Referenced by DCPlusPlus.ExternalIP.TestResolve(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

00112         {
00113             my_ip = "";
00114             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
00115             wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadFileCallback);
00116 
00117         }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

void DCPlusPlus.ExternalIP.AbortFetch (  ) 

Manually abort the retrieval.

Definition at line 147 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.busy, and DCPlusPlus.ExternalIP.wc.

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         }

delegate void DCPlusPlus.ExternalIP.CompletedEventHandler ( ExternalIP  ex_ip  ) 

Prototype for the Completed Event Handler.

Parameters:
ex_ip the External IP object that fired the event

void DCPlusPlus.ExternalIP.DownloadFileCallback ( object  sender,
DownloadDataCompletedEventArgs  e 
) [private]

Async callback for webclients get file operation ,gets called if the file was retrieved successfully.

Parameters:
sender event sending webclient instance
e event arguments of the download operation

Definition at line 171 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.busy, DCPlusPlus.ExternalIP.Completed, DCPlusPlus.ExternalIP.error_code, DCPlusPlus.ExternalIP.my_ip, and DCPlusPlus.ExternalIP.UnableToFetch.

Referenced by DCPlusPlus.ExternalIP.ExternalIP().

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         }

Here is the caller graph for this function:

void DCPlusPlus.ExternalIP.DownloadProgressCallback ( object  sender,
DownloadProgressChangedEventArgs  e 
) [private]

Async callback for webclients get file operation ,gets called when the progress of the download changes.

Parameters:
sender event sending webclient instance
e event arguments of the download operation

Definition at line 233 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.percentage, and DCPlusPlus.ExternalIP.ProgressChanged.

Referenced by DCPlusPlus.ExternalIP.ExternalIP().

00234         {
00235             percentage = e.ProgressPercentage;
00236             if (ProgressChanged != null)
00237                 ProgressChanged(this);
00238         }

Here is the caller graph for this function:

void DCPlusPlus.ExternalIP.FetchIP (  ) 

Start an async retrieval of our external ip address.

Definition at line 125 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.busy, DCPlusPlus.ExternalIP.percentage, DCPlusPlus.ExternalIP.ProgressChanged, DCPlusPlus.ExternalIP.url, and DCPlusPlus.ExternalIP.wc.

Referenced by DCPlusPlus.PortCheck.TestCheck(), DCPlusPlus.PortCheck.TestCheckRunningLocalPeers(), DCPlusPlus.ExternalIP.TestResolve(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

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         }

Here is the caller graph for this function:

delegate void DCPlusPlus.ExternalIP.ProgressChangedEventHandler ( ExternalIP  ex_ip  ) 

Prototype for the Progress Changed Event Handler.

Parameters:
ex_ip the External IP object that fired the event

[Test] void DCPlusPlus.ExternalIP.TestResolve (  ) 

Test to check if we can successfully retrieve our ip address.

Definition at line 244 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.Completed, DCPlusPlus.ExternalIP.ExternalIP(), DCPlusPlus.ExternalIP.FetchIP(), and DCPlusPlus.ExternalIP.MyIP.

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         }

Here is the call graph for this function:

[Test] void DCPlusPlus.ExternalIP.TestResolveFailServiceOffine (  ) 

Test to see if a failed resolve throws exceptions we do not catch or our application may crash during the retrieval.

Definition at line 279 of file ExternalIP.cs.

References DCPlusPlus.ExternalIP.Completed, DCPlusPlus.ExternalIP.ExternalIP(), DCPlusPlus.ExternalIP.FetchIP(), DCPlusPlus.ExternalIP.MyIP, DCPlusPlus.ExternalIP.UnableToFetch, and DCPlusPlus.ExternalIP.url.

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         }

Here is the call graph for this function:

delegate void DCPlusPlus.ExternalIP.UnableToFetchEventHandler ( ExternalIP  ex_ip  ) 

Prototype for the Unable To Fetch Event Handler.

Parameters:
ex_ip the External IP object that fired the event


Member Data Documentation

bool DCPlusPlus.ExternalIP.busy = false [protected]

Definition at line 96 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.AbortFetch(), DCPlusPlus.ExternalIP.DownloadFileCallback(), and DCPlusPlus.ExternalIP.FetchIP().

event CompletedEventHandler DCPlusPlus.ExternalIP.Completed

Event handler that gets called when an ip address was retrieved after calling FetchIP().

Definition at line 29 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadFileCallback(), DCPlusPlus.PortCheck.TestCheck(), DCPlusPlus.PortCheck.TestCheckRunningLocalPeers(), DCPlusPlus.ExternalIP.TestResolve(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

Connection.ErrorCodes DCPlusPlus.ExternalIP.error_code = Connection.ErrorCodes.NoErrorYet [protected]

Definition at line 57 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadFileCallback().

string DCPlusPlus.ExternalIP.my_ip [protected]

Definition at line 80 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadFileCallback(), and DCPlusPlus.ExternalIP.ExternalIP().

int DCPlusPlus.ExternalIP.percentage = 0 [protected]

Definition at line 69 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadProgressCallback(), and DCPlusPlus.ExternalIP.FetchIP().

event ProgressChangedEventHandler DCPlusPlus.ExternalIP.ProgressChanged

Event handler that gets called when the progress of the FetchIP() operation changed.

Definition at line 35 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadProgressCallback(), and DCPlusPlus.ExternalIP.FetchIP().

event UnableToFetchEventHandler DCPlusPlus.ExternalIP.UnableToFetch

Event handler that gets called when the operation was unable to finish the ip retrieval during an error.

Definition at line 41 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.DownloadFileCallback(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

string DCPlusPlus.ExternalIP.url = "http://www.lawrencegoetz.com/programs/ipinfo/" [private]

the url of the ip fetching service we use

Definition at line 121 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.FetchIP(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

WebClient DCPlusPlus.ExternalIP.wc = new WebClient() [private]

our weblclient instance we use to get the ip

Definition at line 110 of file ExternalIP.cs.

Referenced by DCPlusPlus.ExternalIP.AbortFetch(), DCPlusPlus.ExternalIP.ExternalIP(), and DCPlusPlus.ExternalIP.FetchIP().


Property Documentation

Connection.ErrorCodes DCPlusPlus.ExternalIP.ErrorCode [get]

Get the error code if something went wrong and you want to tell the user the 'exact' cause.

Definition at line 63 of file ExternalIP.cs.

bool DCPlusPlus.ExternalIP.IsBusy [get]

Get the Status of the retrieval.

Definition at line 101 of file ExternalIP.cs.

string DCPlusPlus.ExternalIP.MyIP [get, set]

Get the ip address FetchIP() retrieved.

Definition at line 85 of file ExternalIP.cs.

Referenced by DCPlusPlus.PortCheck.TestCheck(), DCPlusPlus.PortCheck.TestCheckRunningLocalPeers(), DCPlusPlus.ExternalIP.TestResolve(), and DCPlusPlus.ExternalIP.TestResolveFailServiceOffine().

int DCPlusPlus.ExternalIP.Percentage [get]

Get the progress percentage of the retrieval operation.

Definition at line 74 of file ExternalIP.cs.


The documentation for this class was generated from the following file:
Generated on Wed Mar 7 19:09:28 2007 for DCPlusPlus by  doxygen 1.5.1-p1