Author: alanmc
Date: 2007-05-19 20:18:49 -0400 (Sat, 19 May 2007)
New Revision: 77706

Modified:
   trunk/Mono.Nat/Examples/Program.cs
Log:
Updated the example code

Modified: trunk/Mono.Nat/Examples/Program.cs
===================================================================
--- trunk/Mono.Nat/Examples/Program.cs  2007-05-20 00:18:34 UTC (rev 77705)
+++ trunk/Mono.Nat/Examples/Program.cs  2007-05-20 00:18:49 UTC (rev 77706)
@@ -16,9 +16,9 @@
             int i = 0;
             NatController c = new NatController();
 
-            c.StartSearching();
+            c.Start();
             c.DeviceFound += new EventHandler<DeviceEventArgs>(c_DeviceFound);
-            c.DeviceLost += new EventHandler<DeviceEventArgs>(c_DeviceLost);
+            //c.DeviceLost += new EventHandler<DeviceEventArgs>(c_DeviceLost);
             while (i++ < 15)
             {
                 System.Threading.Thread.Sleep(1000);
@@ -37,6 +37,7 @@
         }
 
         private static INatDevice d;
+            
         static void c_DeviceFound(object sender, DeviceEventArgs e)
         {
             Console.WriteLine("Found a uPnP device...");
@@ -52,83 +53,114 @@
 
         private static void Mapper()
         {
-            IAsyncResult result;
+            GetExternalIP();
+            MapPort();
+            GetAllMappings();
+                       GetSpecificMapping();
+
+                       Console.WriteLine("Finished!");
+        }
+
+        private static void MapPort()
+        {
             Mapping m;
+            m = new Mapping(15661, Protocol.Tcp);
 
-            m = new Mapping(15661, Protocol.TCP);
             Console.WriteLine();
+            
             Console.WriteLine("Trying to map port: " + m.Port + " " + 
m.Protocol.ToString());
-            result = d.BeginCreatePortMap(m, "MonoTorrent", null, m);
-            result.AsyncWaitHandle.WaitOne();
             try
             {
-                d.EndCreatePortMap(result);
-                m = (Mapping)result.AsyncState;
+                d.CreatePortMap(m, "MonoTorrent");
+                               System.Threading.Thread.Sleep(100);
+
                 Console.WriteLine("Mapping created: " + m.Port + " " + 
m.Protocol.ToString());
+                Console.WriteLine();
+                Console.WriteLine("Trying to delete mapping: " + m.Port + " " 
+ m.Protocol.ToString());
+                d.DeletePortMap(m);
+                               System.Threading.Thread.Sleep(100);
+
+                Console.WriteLine("Mapping deleted: " + m.Port + " " + 
m.Protocol.ToString());
+
             }
             catch (MappingException ex)
             {
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Failed to create map: " + m.Port + " " + 
m.Protocol.ToString());
+                Console.WriteLine("Failed to create/delete map: " + m.Port + " 
" + m.Protocol.ToString());
                 Console.WriteLine(ex.ErrorCode.ToString() + " " + 
ex.ErrorText);
             }
 
-            Console.WriteLine();
-            m = new Mapping(15661, Protocol.TCP);
-            Console.WriteLine("Trying to delete mapping: " + m.Port + " " + 
m.Protocol.ToString());
-            result = d.BeginDeletePortMap(m, null, m);
-            result.AsyncWaitHandle.WaitOne();
+        }
 
+        private static void GetExternalIP()
+        {
             try
             {
-                d.EndDeletePortMap(result);
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Mapping deleted: " + m.Port + " " + 
m.Protocol.ToString());
+                Console.WriteLine();
+                Console.WriteLine("Trying to get external IP address");
+                IPAddress address = d.GetExternalIP();
+                               System.Threading.Thread.Sleep(100);
+                Console.WriteLine("External IP address is: " + 
address.ToString());
             }
-            catch(MappingException ex)
+            catch (MappingException ex)
             {
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Failed to delete map: " + m.Port + " " + 
m.Protocol.ToString());
+                Console.WriteLine("Failed to get external IP address");
                 Console.WriteLine(ex.ErrorCode.ToString() + " " + 
ex.ErrorText);
             }
+        }
 
+        private static void GetAllMappings()
+        {
             Console.WriteLine();
-            m = new Mapping(15661, Protocol.UDP);
-            Console.WriteLine("Trying to map port: " + m.Port + " " + 
m.Protocol.ToString());
-            result = d.BeginCreatePortMap(m, "MonoTorrent", null, m);
-            result.AsyncWaitHandle.WaitOne();
-            try
+            Console.WriteLine("Trying to get all the mappings");
+
+            Mapping[] mappings = d.GetAllMappings();
+                       System.Threading.Thread.Sleep(100);
+
+            Console.WriteLine("Got all the mappings...");
+            for (int i = 0; i < mappings.Length; i++)
             {
-                d.EndCreatePortMap(result);
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Mapping created: " + m.Port + " " + 
m.Protocol.ToString());
+                Console.Write("Port: " + mappings[i].Port.ToString());
+                Console.WriteLine(" Protocol: " + 
mappings[i].Protocol.ToString());
             }
-            catch (MappingException ex)
-            {
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Failed to create map: " + m.Port + " " + 
m.Protocol.ToString());
-                Console.WriteLine(ex.ErrorCode.ToString() + " " + 
ex.ErrorText);
-            }
 
             Console.WriteLine();
-            Console.WriteLine("Trying to unmap port 15661, UDP");
-            result = d.BeginDeletePortMap(new Mapping(15661, Protocol.UDP), 
null, new Mapping(15661, Protocol.UDP));
-            result.AsyncWaitHandle.WaitOne();
-            try
-            {
-                d.EndDeletePortMap(result);
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Mapping deleted: " + m.Port.ToString() + " 
" + m.Protocol.ToString());
-            }
-            catch (MappingException ex)
-            {
-                m = (Mapping)result.AsyncState;
-                Console.WriteLine("Failed to delete mapping: " + m.Port + " " 
+ m.Protocol.ToString());
-                Console.WriteLine(ex.ErrorCode.ToString() + " " + 
ex.ErrorText);
-            }
+
         }
 
+               private static void GetSpecificMapping()
+               {
+                               Mapping m = new Mapping(54321, Protocol.Tcp);
+                       try
+                       {
+                               d.CreatePortMap(m, "Test");
+                               System.Threading.Thread.Sleep(100);
+                               Console.WriteLine("Getting specific 
mapping...");
+                               Mapping routerMapping = 
d.GetSpecificMapping(m.Port, m.Protocol);
+                               System.Threading.Thread.Sleep(100);
 
+                               Console.WriteLine("Got specific mapping. Port: 
" + routerMapping.Port + " Protocol: " + routerMapping.Protocol); 
+                               d.DeletePortMap(m);
+                               System.Threading.Thread.Sleep(100);
+
+                       }
+                       catch
+                       {
+                               Console.WriteLine("Failed to get a specific 
mapping");
+                       }
+                       finally
+                       {
+                               try
+                               {
+                                       d.DeletePortMap(m);
+                                       System.Threading.Thread.Sleep(100);
+
+                               }
+                               catch
+                               {
+                               }
+                       }
+               }
+
         static void d_PortMapEvent(object sender, EventArgs e)
         {
             System.Console.WriteLine(sender.ToString());

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to