]> git.meshlink.io Git - catta/commitdiff
first pass at updating to the 0.6 apis
authorJames Willcox <snopr@snorp.net>
Wed, 26 Oct 2005 03:20:44 +0000 (03:20 +0000)
committerJames Willcox <snopr@snorp.net>
Wed, 26 Oct 2005 03:20:44 +0000 (03:20 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@876 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-sharp/AddressResolver.cs
avahi-sharp/AvahiTest.cs
avahi-sharp/BrowserBase.cs
avahi-sharp/Client.cs
avahi-sharp/ClientException.cs
avahi-sharp/EntryGroup.cs
avahi-sharp/HostNameResolver.cs
avahi-sharp/ResolverBase.cs
avahi-sharp/ServiceResolver.cs
avahi-sharp/Utility.cs

index 01211aecd9cb6e20676c5efe260decfcda3369a9..3231cbf492efa6c6f404890af982adaadfe02e16 100644 (file)
@@ -29,7 +29,7 @@ namespace Avahi
 {
 
     internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
-                                                    ResolverEvent revent, Protocol aproto, IntPtr address,
+                                                    ResolverEvent revent, IntPtr address,
                                                     IntPtr hostname, LookupResultFlags flags, IntPtr userdata);
 
     public delegate void HostAddressHandler (object o, string host, IPAddress address);
@@ -124,7 +124,7 @@ namespace Avahi
                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
                 return;
 
-            IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
+            IntPtr addrPtr = Utility.AddressToPtr (address);
 
             lock (client) {
                 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags,
@@ -147,7 +147,7 @@ namespace Avahi
         }
 
         private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
-                                                ResolverEvent revent, Protocol aproto, IntPtr address,
+                                                ResolverEvent revent, IntPtr address,
                                                 IntPtr hostname, LookupResultFlags flags, IntPtr userdata)
         {
             switch (revent) {
@@ -158,15 +158,8 @@ namespace Avahi
                 foreach (HostAddressHandler handler in foundListeners)
                     handler (this, currentHost, currentAddress);
                 break;
-            case ResolverEvent.Timeout:
-                currentAddress = null;
-                currentHost = null;
-                
-                foreach (EventHandler handler in timeoutListeners)
-                    handler (this, new EventArgs ());
-                break;
-            default:
-                EmitResolverEvent (revent);
+            case ResolverEvent.Failure:
+                EmitFailure (client.LastError);
                 break;
             }
         }
index 395d5cb104d6acc67068195fd4483193fc6882ed..9c4aeca50fbcc96bd9353bf39ef7d7d9e43d06a2 100644 (file)
 /* $Id$ */
 
 /***
-  This file is part of avahi.
-
-  avahi is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as
-  published by the Free Software Foundation; either version 2.1 of the
-  License, or (at your option) any later version.
-
-  avahi is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
-  Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with avahi; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-  USA.
+    This file is part of avahi.
+
+    avahi is free software; you can redistribute it and/or modify it
+    under the terms of the GNU Lesser General Public License as
+    published by the Free Software Foundation; either version 2.1 of the
+    License, or (at your option) any later version.
+
+    avahi is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+    Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with avahi; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+    USA.
 ***/
 
 using System;
+using System.Collections;
 using System.Text;
 using System.Net;
 using Avahi;
 
 public class AvahiTest {
-       private static Client client;
-
-       public static void Main () {
-               client = new Client ();
-
-               EntryGroup eg = new EntryGroup (client);
-               eg.StateChanged += OnEntryGroupChanged;
-               eg.AddService ("foobar2", "_daap._tcp", client.DomainName,
-                              444, new string[] { "foo", "bar", "baz" });
-               eg.Commit ();
-               Console.WriteLine ("Press enter to quit");
-               Console.ReadLine ();
-       }
-
-       private static void OnEntryGroupChanged (object o, EntryGroupState state)
-       {
-               Console.WriteLine ("Entry group status: " + state);
-
-               /*
-               if (state == EntryGroupState.Established) {
-                       DomainBrowser browser = new DomainBrowser (client);
-                       browser.DomainAdded += OnDomainAdded;
-               }
-               */
-
-               BrowseServiceTypes ("dns-sd.org");
-       }
-
-       private static void OnDomainAdded (object o, DomainInfo info)
-       {
-               Console.WriteLine ("Got domain added: " + info.Domain);
-               BrowseServiceTypes (info.Domain);
-       }
-
-       private static void BrowseServiceTypes (string domain)
-       {
-               ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain);
-               stb.CacheExhausted += OnCacheExhausted;
-               stb.ServiceTypeAdded += OnServiceTypeAdded;
-       }
-
-       private static void OnCacheExhausted (object o, EventArgs args)
-       {
-               Console.WriteLine ("Cache is exhausted");
-       }
-
-       private static void OnServiceTypeAdded (object o, ServiceTypeInfo info)
-       {
-               Console.WriteLine ("Got service type: " + info.ServiceType);
-               ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain);
-               sb.ServiceAdded += OnServiceAdded;
-       }
-
-       private static void OnServiceAdded (object o, ServiceInfo info)
-       {
-               // Console.WriteLine ("Got service: " + info.Name);
-               ServiceResolver resolver = new ServiceResolver (client, info);
-               resolver.Found += OnServiceResolved;
-       }
-
-       private static void OnServiceResolved (object o, ServiceInfo info)
-       {
-               Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port);
-               foreach (byte[] bytes in info.Text) {
-                       Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
-               }
-               AddressResolver ar = new AddressResolver (client, info.Address);
-               ar.Found += OnAddressResolved;
-       }
-
-       private static void OnAddressResolved (object o, string host, IPAddress address)
-       {
-               Console.WriteLine ("Resolved {0} to {1}", address, host);
-               HostNameResolver hr = new HostNameResolver (client, host);
-               hr.Found += OnHostNameResolved;
-       }
-
-       private static void OnHostNameResolved (object o, string host, IPAddress address)
-       {
-               Console.WriteLine ("Resolved {0} to {1}", host, address);
-       }
+    private static Client client;
+    private static ArrayList objects = new ArrayList ();
+
+    public static void Main () {
+        client = new Client ();
+
+        EntryGroup eg = new EntryGroup (client);
+        eg.StateChanged += OnEntryGroupChanged;
+        eg.AddService ("foobar2", "_daap._tcp", client.DomainName,
+                       444, new string[] { "foo", "bar", "baz" });
+        eg.Commit ();
+        Console.WriteLine ("Press enter to quit");
+        Console.ReadLine ();
+    }
+
+    private static void OnEntryGroupChanged (object o, EntryGroupState state)
+    {
+        Console.WriteLine ("Entry group status: " + state);
+        if (state == EntryGroupState.Established) {
+            DomainBrowser browser = new DomainBrowser (client);
+            objects.Add (browser);
+            
+            browser.DomainAdded += OnDomainAdded;
+        }
+    }
+
+    private static void OnDomainAdded (object o, DomainInfo info)
+    {
+        Console.WriteLine ("Got domain added: " + info.Domain);
+        BrowseServiceTypes (info.Domain);
+    }
+
+    private static void BrowseServiceTypes (string domain)
+    {
+        ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain);
+        objects.Add (stb);
+        
+        stb.CacheExhausted += OnCacheExhausted;
+        stb.ServiceTypeAdded += OnServiceTypeAdded;
+    }
+
+    private static void OnCacheExhausted (object o, EventArgs args)
+    {
+        Console.WriteLine ("Cache is exhausted");
+    }
+
+    private static void OnServiceTypeAdded (object o, ServiceTypeInfo info)
+    {
+        Console.WriteLine ("Got service type: " + info.ServiceType);
+        ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain);
+        objects.Add (sb);
+        
+        sb.ServiceAdded += OnServiceAdded;
+    }
+
+    private static void OnServiceAdded (object o, ServiceInfo info)
+    {
+        // Console.WriteLine ("Got service: " + info.Name);
+        ServiceResolver resolver = new ServiceResolver (client, info);
+        objects.Add (resolver);
+        resolver.Found += OnServiceResolved;
+    }
+
+    private static void OnServiceResolved (object o, ServiceInfo info)
+    {
+        objects.Remove (o);
+        
+        Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port);
+        foreach (byte[] bytes in info.Text) {
+            Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
+        }
+        AddressResolver ar = new AddressResolver (client, info.Address);
+        objects.Add (ar);
+        
+        ar.Found += OnAddressResolved;
+    }
+
+    private static void OnAddressResolved (object o, string host, IPAddress address)
+    {
+        objects.Remove (o);
+        
+        Console.WriteLine ("Resolved {0} to {1}", address, host);
+        HostNameResolver hr = new HostNameResolver (client, host);
+        objects.Add (hr);
+        
+        hr.Found += OnHostNameResolved;
+    }
+
+    private static void OnHostNameResolved (object o, string host, IPAddress address)
+    {
+        objects.Remove (o);
+        Console.WriteLine ("Resolved {0} to {1}", host, address);
+    }
 }
index 2968114294c337e018d6e213a48e1df6480fe216..1f1f5501f9441a3a2b2ece1c8d49214a3cb3106e 100644 (file)
@@ -41,10 +41,6 @@ namespace Avahi
                 if (AllForNow != null)
                     AllForNow (this, new EventArgs ());
                 break;
-            case BrowserEvent.NotFound:
-                if (NotFound != null)
-                    NotFound (this, new EventArgs ());
-                break;
             case BrowserEvent.Failure:
                 if (Failed != null)
                     Failed (this, new EventArgs ());
index e148ff30ba8f379e04b76a00ec49614d097b34ec..73b6f57c0b85f19db23ea3e84c51d027e928a161 100644 (file)
@@ -29,8 +29,6 @@ namespace Avahi
 {
     internal enum ResolverEvent {
         Found,
-        Timeout,
-        NotFound,
         Failure
     }
     
@@ -39,7 +37,6 @@ namespace Avahi
         Removed,
         CacheExhausted,
         AllForNow,
-        NotFound,
         Failure
     }
 
@@ -66,8 +63,7 @@ namespace Avahi
     public enum LookupFlags {
         None = 0,
         UseWideArea = 1,
-        UseMulticast = 2,
-        NoTxt = 4,
+        UseMulticast = 4,
         NoAddress = 8
     }
 
@@ -76,7 +72,9 @@ namespace Avahi
         None = 0,
         Cached = 1,
         WideArea = 2,
-        Multicast = 4
+        Multicast = 4,
+        Local = 8,
+        OurOwn = 16,
     }
     
     public class Client : IDisposable
@@ -123,7 +121,7 @@ namespace Avahi
         private static extern void avahi_simple_poll_free (IntPtr spoll);
 
         [DllImport ("avahi-common")]
-        private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout);
+        private static extern int avahi_simple_poll_loop (IntPtr spoll);
 
         [DllImport ("avahi-common")]
         private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
@@ -134,10 +132,6 @@ namespace Avahi
         [DllImport ("avahi-client")]
         private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
 
-        [DllImport ("avahi-client")]
-        private static extern int avahi_client_is_service_local (IntPtr client, int iface, Protocol proto,
-                                                                 IntPtr name, IntPtr type, IntPtr domain);
-
 
         [DllImport ("libc")]
         private static extern int poll(IntPtr ufds, uint nfds, int timeout);
@@ -203,11 +197,11 @@ namespace Avahi
             }
         }
 
-        internal int LastError
+        internal ErrorCode LastError
         {
             get {
                 lock (this) {
-                    return avahi_client_errno (handle);
+                    return (ErrorCode) avahi_client_errno (handle);
                 }
             }
         }
@@ -250,32 +244,11 @@ namespace Avahi
             }
         }
 
-        public bool IsServiceLocal (ServiceInfo service)
-        {
-            return IsServiceLocal (service.NetworkInterface, service.Protocol, service.Name,
-                                   service.ServiceType, service.Domain);
-        }
-
-        public bool IsServiceLocal (int iface, Protocol proto, string name, string type, string domain)
-        {
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-            
-            int result = avahi_client_is_service_local (handle, iface, proto, namePtr, typePtr, domainPtr);
-
-            Utility.Free (namePtr);
-            Utility.Free (typePtr);
-            Utility.Free (domainPtr);
-
-            return result == 1;
-        }
-
         internal void CheckError ()
         {
-            int error = LastError;
+            ErrorCode error = LastError;
 
-            if (error != 0)
+            if (error != ErrorCode.Ok)
                 throw new ClientException (error);
         }
         
@@ -295,10 +268,7 @@ namespace Avahi
         private void PollLoop () {
             try {
                 lock (this) {
-                    while (true) {
-                        if (avahi_simple_poll_iterate (spoll, -1) != 0)
-                            break;
-                    }
+                    avahi_simple_poll_loop (spoll);
                 }
             } catch (ThreadAbortException e) {
             } catch (Exception e) {
index 2abc57cbb71322ec3fa22803fc211729a6475d06..3ee0cab12550736c4c4e4812423f3f8b8175ecd4 100644 (file)
@@ -25,24 +25,78 @@ using System.Runtime.InteropServices;
 
 namespace Avahi
 {
+    public enum ErrorCode {
+        Ok = 0,
+        Failure = -1,
+        BadState = -2,
+        InvalidHostName = - 3,
+        InvalidDomainName = -4,
+        NoNetwork = -5,
+        InvalidTTL = -6,
+        IsPattern = -7,
+        Collision = -8,
+        InvalidRecord = -9,
+        InvalidServiceName = -10,
+        InvalidServiceType = -11,
+        InvalidPort = -12,
+        InvalidKey = -13,
+        InvalidAddress = -14,
+        Timeout = -15,
+        TooManyClients = -16,
+        TooManyObjects = -17,
+        TooManyEntries = -18,
+        OS = -19,
+        AccessDenied = -20,
+        InvalidOperation = -21,
+        DBusError = -22,
+        NotConnected = -23,
+        NoMemory = -24,
+        InvalidObject = -25,
+        NoDaemon = -26,
+        InvalidInterface = -27,
+        InvalidProtocol = -28,
+        InvalidFlags = -29,
+        NotFound = -30,
+        InvalidConfig = -31,
+        VersionMismatch = -32,
+        InvalidServiceSubtype = -33,
+        InvalidPacket = -34,
+        InvalidDnsError = -35,
+        DnsFormErr = -36,
+        DnsServFail = -37,
+        DnsNxDomain = -38,
+        DnsNoTimp = -39,
+        DnsRefused = -40,
+        DnsYxDomain = -41,
+        DnsYxRrSet = -42,
+        DnsNxRrSet = -43,
+        DnsNotAuth = -44,
+        DnsNotZone = -45
+    }
+
+    public delegate void ErrorCodeHandler (object o, ErrorCode code);
+    
     public class ClientException : ApplicationException
     {
-        private int code;
+        private ErrorCode code;
 
         [DllImport ("avahi-common")]
-        private static extern IntPtr avahi_strerror (int code);
+        private static extern IntPtr avahi_strerror (ErrorCode code);
         
-        public int ErrorCode
+        public ErrorCode ErrorCode
         {
             get { return code; }
         }
+
+        internal ClientException (int code) : this ((ErrorCode) code) {
+        }
         
-        internal ClientException (int code) : base (GetErrorString (code))
+        internal ClientException (ErrorCode code) : base (GetErrorString (code))
         {
             this.code = code;
         }
 
-        private static string GetErrorString (int code)
+        private static string GetErrorString (ErrorCode code)
         {
             IntPtr str = avahi_strerror (code);
             return Utility.PtrToString (str);
index 85c9a1d51e5cad7388bd5b3b2ae8ca54a7854d4f..4373178b1b71dd560fecd60dc78f6ab147b077e2 100644 (file)
@@ -26,11 +26,24 @@ using System.Runtime.InteropServices;
 namespace Avahi
 {
 
+    [Flags]
+    public enum PublishFlags {
+        None = 0,
+        Unique = 1,
+        NoProbe = 2,
+        NoAnnounce = 4,
+        AllowMultiple = 8,
+        NoReverse = 16,
+        NoCookie = 32,
+        Update = 64
+    }
+    
     public enum EntryGroupState {
         Uncommited,
         Registering,
         Established,
-        Collision
+        Collision,
+        Failure
     }
 
     internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
@@ -59,8 +72,9 @@ namespace Avahi
 
         [DllImport ("avahi-client")]
         private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
-                                                                         IntPtr name, IntPtr type, IntPtr domain,
-                                                                         IntPtr host, UInt16 port, IntPtr strlst);
+                                                                         PublishFlags flags, IntPtr name, IntPtr type,
+                                                                         IntPtr domain, IntPtr host, UInt16 port,
+                                                                         IntPtr strlst);
         
         [DllImport ("avahi-client")]
         private static extern void avahi_entry_group_free (IntPtr group);
@@ -142,10 +156,16 @@ namespace Avahi
         public void AddService (string name, string type, string domain,
                                 UInt16 port, params string[] txt)
         {
-            AddService (-1, Protocol.Unspecified, name, type, domain, null, port, txt);
+            AddService (PublishFlags.None, name, type, domain, port, txt);
+        }
+
+        public void AddService (PublishFlags flags, string name, string type, string domain,
+                                UInt16 port, params string[] txt)
+        {
+            AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
         }
 
-        public void AddService (int iface, Protocol proto, string name, string type, string domain,
+        public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
                                 string host, UInt16 port, params string[] txt)
         {
             IntPtr list = avahi_string_list_new (IntPtr.Zero);
@@ -164,7 +184,7 @@ namespace Avahi
             IntPtr hostPtr = Utility.StringToPtr (host);
 
             lock (client) {
-                avahi_entry_group_add_service_strlst (handle, iface, proto, namePtr, typePtr, domainPtr,
+                avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr,
                                                       hostPtr, port, list);
             }
             
index 44febcdf4d1a8f4d2bc4703978bdb936deb4f501..9780811408a1fb3b64567a189e31c8cffd4163a0 100644 (file)
@@ -159,15 +159,8 @@ namespace Avahi
                 foreach (HostAddressHandler handler in foundListeners)
                     handler (this, currentHost, currentAddress);
                 break;
-            case ResolverEvent.Timeout:
-                currentAddress = null;
-                currentHost = null;
-                
-                foreach (EventHandler handler in timeoutListeners)
-                    handler (this, new EventArgs ());
-                break;
-            default:
-                EmitResolverEvent (revent);
+            case ResolverEvent.Failure:
+                EmitFailure (client.LastError);
                 break;
             }
         }
index 0fdd4a1415c66d5bec0c2382278df857909fccb8..5d375b3bb7a23b220f23382217aabfef614784c4 100644 (file)
@@ -25,23 +25,12 @@ namespace Avahi
 {
     public abstract class ResolverBase
     {
-        public event EventHandler NotFound;
-        public event EventHandler Failed;
+        public event ErrorCodeHandler Failed;
 
-        internal void EmitResolverEvent (ResolverEvent revent)
+        internal void EmitFailure (ErrorCode code)
         {
-            switch (revent) {
-            case ResolverEvent.NotFound:
-                if (NotFound != null)
-                    NotFound (this, new EventArgs ());
-                break;
-            case ResolverEvent.Failure:
-                if (Failed != null)
-                    Failed (this, new EventArgs ());
-                break;
-            default:
-                break;
-            }
+            if (Failed != null)
+                Failed (this, code);
         }
     }
 }
index 7527652d6ac485f20346dbeb710331354ca3151c..4a6a8a068f3bff7ad57867b25b59823e06d06da9 100644 (file)
@@ -207,14 +207,8 @@ namespace Avahi
                 foreach (ServiceInfoHandler handler in foundListeners)
                     handler (this, info);
                 break;
-            case ResolverEvent.Timeout:
-                currentInfo = ServiceInfo.Zero;
-                
-                foreach (EventHandler handler in timeoutListeners)
-                    handler (this, new EventArgs ());
-                break;
-            default:
-                EmitResolverEvent (revent);
+            case ResolverEvent.Failure:
+                EmitFailure (client.LastError);
                 break;
             }
         }
index 55254ebde96531458e2e94aaab37cbf17e598313..f8b4ffd8982857d55b65f603f839efb666cfa6fd 100644 (file)
@@ -36,6 +36,9 @@ namespace Avahi
         [DllImport ("avahi-common")]
         private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address);
 
+        [DllImport ("avahi-common")]
+        private static extern IntPtr avahi_address_parse (IntPtr str, Protocol proto, IntPtr ret);
+
         public static string PtrToString (IntPtr ptr)
         {
             if (ptr == IntPtr.Zero)
@@ -74,6 +77,16 @@ namespace Avahi
             Stdlib.free (ptr);
         }
 
+        public static IntPtr AddressToPtr (IPAddress address)
+        {
+            IntPtr straddr = Utility.StringToPtr (address.ToString ());
+            IntPtr addrPtr = Stdlib.malloc (32);
+            avahi_address_parse (straddr, Protocol.Unspecified, addrPtr);
+            Utility.Free (straddr);
+
+            return addrPtr;
+        }
+
         public static IPAddress PtrToAddress (IntPtr ptr)
         {
             IPAddress address = null;