]> git.meshlink.io Git - catta/blob - avahi-sharp/DomainBrowser.cs
lose the dep on avahi-glib, and run the avahi loop in a thread
[catta] / avahi-sharp / DomainBrowser.cs
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 using System;
23 using System.Collections;
24 using System.Runtime.InteropServices;
25
26 namespace Avahi
27 {
28     internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
29                                                   IntPtr domain, IntPtr userdata);
30
31     public enum DomainBrowserType {
32         Register,
33         RegisterDefault,
34         Browse,
35         BrowseDefault,
36         BrowseLegacy
37     }
38
39     public struct DomainInfo
40     {
41         public int NetworkInterface;
42         public Protocol Protocol;
43         public string Domain;
44     }
45
46     public delegate void DomainInfoHandler (object o, DomainInfo info);
47     
48     public class DomainBrowser : IDisposable
49     {
50         private IntPtr handle;
51         private ArrayList infos = new ArrayList ();
52         private Client client;
53         private int iface;
54         private Protocol proto;
55         private string domain;
56         private DomainBrowserType btype;
57         private DomainBrowserCallback cb;
58
59         private ArrayList addListeners = new ArrayList ();
60         private ArrayList removeListeners = new ArrayList ();
61         
62         [DllImport ("avahi-client")]
63         private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
64                                                                IntPtr domain, int btype, DomainBrowserCallback cb,
65                                                                IntPtr userdata);
66
67         [DllImport ("avahi-client")]
68         private static extern void avahi_domain_browser_free (IntPtr handle);
69
70         public event DomainInfoHandler DomainAdded
71         {
72             add {
73                 addListeners.Add (value);
74                 Start ();
75             }
76             remove {
77                 addListeners.Remove (value);
78                 Stop (false);
79             }
80         }
81         
82         public event DomainInfoHandler DomainRemoved
83         {
84             add {
85                 removeListeners.Add (value);
86                 Start ();
87             }
88             remove {
89                 removeListeners.Remove (value);
90                 Stop (false);
91             }
92         }
93
94         public DomainInfo[] Domains
95         {
96             get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
97         }
98
99         public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
100                                                      DomainBrowserType.Browse) {
101         }
102         
103         public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype)
104         {
105             this.client = client;
106             this.iface = iface;
107             this.proto = proto;
108             this.domain = domain;
109             this.btype = btype;
110             cb = OnDomainBrowserCallback;
111         }
112
113         ~DomainBrowser ()
114         {
115             Dispose ();
116         }
117
118         public void Dispose ()
119         {
120             Stop (true);
121         }
122
123         private void Start ()
124         {
125             if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
126                 (addListeners.Count == 0 && removeListeners.Count == 0))
127                 return;
128
129             lock (client) {
130                 IntPtr domainPtr = Utility.StringToPtr (domain);
131                 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
132                                                    cb, IntPtr.Zero);
133                 Utility.Free (domainPtr);
134             }
135         }
136
137         private void Stop (bool force)
138         {
139             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
140                 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
141                 lock (client) {
142                     avahi_domain_browser_free (handle);
143                     handle = IntPtr.Zero;
144                 }
145             }
146         }
147
148         private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
149                                               IntPtr domain, IntPtr userdata)
150         {
151
152             DomainInfo info;
153             info.NetworkInterface = iface;
154             info.Protocol = proto;
155             info.Domain = Utility.PtrToString (domain);
156
157             infos.Add (info);
158             
159             if (bevent == BrowserEvent.Added) {
160                 infos.Add (info);
161
162                 foreach (DomainInfoHandler handler in addListeners)
163                     handler (this, info);
164             } else {
165                 infos.Remove (info);
166
167                 foreach (DomainInfoHandler handler in removeListeners)
168                     handler (this, info);
169             }
170         }
171     }
172 }