Eigene Dateien/Visual Studio 2005/Projects/DCPlusPlus/DCPlusPlus/PortCheck.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 {
00019     [TestFixture]
00020     public class PortCheck
00021     {
00026         public event CompletedEventHandler Completed;
00031         public event ProgressChangedEventHandler ProgressChanged;
00036         public event UnableToFetchEventHandler UnableToFetch;
00041         public delegate void CompletedEventHandler(PortCheck ex_ip);
00046         public delegate void ProgressChangedEventHandler(PortCheck ex_ip);
00051         public delegate void UnableToFetchEventHandler(PortCheck ex_ip);
00052         protected Connection.ErrorCodes error_code = Connection.ErrorCodes.NoErrorYet;
00056         public Connection.ErrorCodes ErrorCode
00057         {
00058             get
00059             {
00060                 return (error_code);
00061             }
00062         }
00063         protected int percentage = 0;
00067         public int Percentage
00068         {
00069             get
00070             {
00071                 return (percentage);
00072             }
00073         }
00078         public enum Ports
00079         {
00080             UdpAndTcp,Udp,Tcp,None
00081         };
00085         public Ports OpenPorts = Ports.None;
00086         protected string my_ip;
00090         public string MyIP
00091         {
00092             get
00093             {
00094                 return (my_ip);
00095             }
00096             set
00097             {
00098                 my_ip = value;
00099             }
00100 
00101         }
00102         protected string my_client_name="c#++";
00106         public string MyClientName
00107         {
00108             get
00109             {
00110                 return (my_client_name);
00111             }
00112             set
00113             {
00114                 my_client_name = value;
00115             }
00116 
00117         }
00118         protected int my_udp_port;
00122         public int MyUdpPort
00123         {
00124             get
00125             {
00126                 return (my_udp_port);
00127             }
00128             set
00129             {
00130                 my_udp_port = value;
00131             }
00132 
00133         }
00134         protected int my_tcp_port;
00138         public int MyTcpPort
00139         {
00140             get
00141             {
00142                 return (my_tcp_port);
00143             }
00144             set
00145             {
00146                 my_tcp_port = value;
00147             }
00148 
00149         }
00150         protected bool busy = false;
00154         public bool IsBusy
00155         {
00156             get
00157             {
00158                 return (busy);
00159             }
00160         }
00164         private WebClient wc = new WebClient();
00168         public PortCheck()
00169         {
00170             my_ip = "";
00171             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
00172             wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadFileCallback);
00173 
00174         }
00178         private string url = "http://connect.majestyc.net/";
00182         public void CheckPorts()
00183         {
00184             if (!busy)
00185             {
00186                 busy = true;
00187                 percentage = 0;
00188                 if (ProgressChanged != null)
00189                     ProgressChanged(this);
00190                 try
00191                 {
00192                     wc.DownloadDataAsync(new Uri(url+"?i="+my_ip+"&t="+my_tcp_port+"&u="+my_udp_port+"&c="+my_client_name));
00193                 }
00194                 catch (Exception ex)
00195                 {
00196                     Console.WriteLine("Exception occured during download: " + ex.Message);
00197                 }
00198             }
00199 
00200         }
00204         public void AbortCheck()
00205         {
00206             if (busy)
00207             {
00208                 try
00209                 {
00210                     wc.CancelAsync();
00211                 }
00212                 catch (Exception ex)
00213                 {
00214                     Console.WriteLine("Exception occured during abort: " + ex.Message);
00215                 }
00216                 System.Threading.Thread.Sleep(10);
00217                 //wc.IsBusy
00218                 //wc = new WebClient();
00219                 busy = false;
00220             }
00221         }
00228         private void DownloadFileCallback(object sender, DownloadDataCompletedEventArgs e)
00229         {
00230             try
00231             {
00232                 if (e.Cancelled) return;
00233                 if (e.Result.Length <= 0) return;
00234                 string page_string = "";
00235                 page_string = System.Text.Encoding.Default.GetString(e.Result);
00236                 //Console.WriteLine("port page: " + page_string);
00237                 int start = page_string.IndexOf("<strong class=\"");
00238                 if (start != -1)
00239                 {
00240                     string temp = page_string.Substring(start + "<strong class=\"".Length);
00241                     int end = temp.IndexOf("\"");
00242                     if (end != -1)
00243                     {
00244                         string tcp = temp.Substring(0, end);
00245                         if (tcp == "green small")
00246                             OpenPorts = Ports.Tcp;
00247                         else OpenPorts = Ports.None;
00248 
00249                         start = temp.IndexOf("<strong class=\"");
00250                         if (start != -1)
00251                         {
00252                             string temp2 = temp.Substring(start + "<strong class=\"".Length);
00253                             end = temp2.IndexOf("\"");
00254                             if (end != -1)
00255                             {
00256                                 string udp = temp2.Substring(0, end);
00257                                 if (udp == "green small" && OpenPorts == Ports.None)
00258                                     OpenPorts = Ports.Udp;
00259                                 if (udp == "green small" && OpenPorts == Ports.Tcp)
00260                                     OpenPorts = Ports.UdpAndTcp;
00261 
00262                                 busy = false;
00263                                 if (Completed != null)
00264                                     Completed(this);
00265                             }
00266                         }
00267                     }
00268                 }
00269             }
00270             catch (Exception out_ex)
00271             {
00272                 Console.WriteLine("Exception during download of port page: " + out_ex.Message);
00273                 error_code = Connection.ErrorCodes.Exception;
00274                 if (UnableToFetch != null)
00275                     UnableToFetch(this);
00276 
00277             }
00278         }
00285         private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
00286         {
00287             percentage = e.ProgressPercentage;
00288             if (ProgressChanged != null)
00289                 ProgressChanged(this);
00290         }
00291 #region Unit Testing
00295         [Test]
00296         public void TestCheck()
00297         {
00298             Console.WriteLine("Test to check open ports.");
00299             bool wait = true;
00300 
00301             //Assert.IsTrue(!string.IsNullOrEmpty(port_check_completed.MyIP), "no ip address fetched");
00302             ExternalIP ex_ip = new ExternalIP();
00303             ex_ip.Completed += delegate(ExternalIP ex_ip_completed)
00304             {
00305                 wait = true;
00306                 Console.WriteLine("Fetch Completed (ip found : " + ex_ip_completed.MyIP + ")");
00307                 PortCheck port_check = new PortCheck();
00308                 port_check.MyIP = ex_ip.MyIP;
00309                 port_check.MyTcpPort = 3412;
00310                 port_check.MyUdpPort = 3412;
00311 
00312                 port_check.Completed += delegate(PortCheck port_check_completed)
00313                 {
00314                     Console.WriteLine("");
00315                     Console.WriteLine("Check Completed (open ports : " + Enum.GetName(typeof(Ports), port_check_completed.OpenPorts) + ")");
00316                     wait = false;
00317                 };
00318                 port_check.CheckPorts();
00319 
00320             };
00321             ex_ip.FetchIP();
00322             Console.WriteLine("Waiting for data");
00323             DateTime ip_start = DateTime.Now;
00324             while (wait)
00325             {
00326                 if (DateTime.Now - ip_start > new TimeSpan(0, 0, 35))
00327                 {
00328                     Console.WriteLine("");
00329                     Console.WriteLine("Operation took too long");
00330                     wait = false;
00331                     Assert.Fail("Operation took too long");
00332                 }
00333                 Console.Write(".");
00334                 Thread.Sleep(250);
00335             }
00336             Console.WriteLine("PortCheck open ports Test successful.");
00337 
00338         }
00342         [Test]
00343         public void TestCheckRunningLocalPeers()
00344         {
00345             Console.WriteLine("Test to check running open ports.");
00346             bool wait = true;
00347             ListeningSockets ls = new ListeningSockets();
00348             ls.TcpPort = 3412;
00349             ls.UdpPort = 3412;
00350             ls.UpdateConnectionSettings();
00351 
00352             //Assert.IsTrue(!string.IsNullOrEmpty(port_check_completed.MyIP), "no ip address fetched");
00353             ExternalIP ex_ip = new ExternalIP();
00354             ex_ip.Completed += delegate(ExternalIP ex_ip_completed)
00355             {
00356                 wait = true;
00357                 Console.WriteLine("Fetch Completed (ip found : " + ex_ip_completed.MyIP + ")");
00358                 PortCheck port_check = new PortCheck();
00359                 port_check.MyIP = ex_ip.MyIP;
00360                 port_check.MyTcpPort = 3412;
00361                 port_check.MyUdpPort = 3412;
00362 
00363                 port_check.Completed += delegate(PortCheck port_check_completed)
00364                 {
00365                     Console.WriteLine("");
00366                     Console.WriteLine("Check Completed (open ports : " + Enum.GetName(typeof(Ports), port_check_completed.OpenPorts) + ")");
00367                     if(port_check_completed.OpenPorts == Ports.None || port_check_completed.OpenPorts == Ports.Udp)
00368                         Assert.Fail("Test failed: tcp port not open!");
00369                     wait = false;
00370                 };
00371                 port_check.CheckPorts();
00372 
00373             };
00374             ex_ip.FetchIP();
00375             Console.WriteLine("Waiting for data");
00376             DateTime ip_start = DateTime.Now;
00377             while (wait)
00378             {
00379                 if (DateTime.Now - ip_start > new TimeSpan(0, 0, 35))
00380                 {
00381                     Console.WriteLine("");
00382                     Console.WriteLine("Operation took too long");
00383                     wait = false;
00384                     Assert.Fail("Operation took too long");
00385                 }
00386                 Console.Write(".");
00387                 Thread.Sleep(250);
00388             }
00389             Console.WriteLine("PortCheck running open ports Test successful.");
00390 
00391         }
00396         [Test]
00397         public void TestCheckFailed()
00398         {
00399             Console.WriteLine("Test to fail checking open ports.");
00400             bool wait = true;
00401             PortCheck port_check = new PortCheck();
00402             port_check.url = "http://bogus.url";
00403             port_check.Completed += delegate(PortCheck port_check_completed)
00404             {
00405                 Console.WriteLine("");
00406                 Console.WriteLine("Check Completed.");
00407                 Assert.Fail("Failed at failing ;-(");
00408             };
00409             port_check.UnableToFetch += delegate(PortCheck port_check_unable)
00410             {
00411                 Console.WriteLine("");
00412                 Console.WriteLine("Failed to fetch check page.");
00413                 wait = false;
00414                 
00415             };
00416 
00417             port_check.CheckPorts();
00418             Console.WriteLine("Waiting for data");
00419             DateTime start = DateTime.Now;
00420             while (wait)
00421             {
00422                 if (DateTime.Now - start > new TimeSpan(0, 0, 5))
00423                 {
00424                     Console.WriteLine("");
00425                     Console.WriteLine("Operation took too long");
00426                     wait = false;
00427                     Assert.Fail("Operation took too long");
00428                 }
00429                 Console.Write(".");
00430                 Thread.Sleep(250);
00431             }
00432             Console.WriteLine("Failed PortCheck Test successful.");
00433         }
00434 #endregion
00435     }
00436 }

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