]> git.meshlink.io Git - catta/blob - avahi-sharp/DomainBrowser.cs
* allow the user to specify a static browse domain list in the configuration file
[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, LookupResultFlags flags, IntPtr userdata);
30
31     public enum DomainBrowserType {
32         Browse,
33         BrowseDefault,
34         Register,
35         RegisterDefault,
36         BrowseLegacy
37     }
38
39     public struct DomainInfo
40     {
41         public int NetworkInterface;
42         public Protocol Protocol;
43         public string Domain;
44         public LookupResultFlags Flags;
45     }
46
47     public delegate void DomainInfoHandler (object o, DomainInfo info);
48     
49     public class DomainBrowser : BrowserBase, IDisposable
50     {
51         private IntPtr handle;
52         private ArrayList infos = new ArrayList ();
53         private Client client;
54         private int iface;
55         private Protocol proto;
56         private string domain;
57         private DomainBrowserType btype;
58         private LookupFlags flags;
59         private DomainBrowserCallback cb;
60
61         private ArrayList addListeners = new ArrayList ();
62         private ArrayList removeListeners = new ArrayList ();
63         
64         [DllImport ("avahi-client")]
65         private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
66                                                                IntPtr domain, int btype, LookupFlags flags,
67                                                                DomainBrowserCallback cb,
68                                                                IntPtr userdata);
69
70         [DllImport ("avahi-client")]
71         private static extern void avahi_domain_browser_free (IntPtr handle);
72
73         public event DomainInfoHandler DomainAdded
74         {
75             add {
76                 addListeners.Add (value);
77                 Start ();
78             }
79             remove {
80                 addListeners.Remove (value);
81                 Stop (false);
82             }
83         }
84         
85         public event DomainInfoHandler DomainRemoved
86         {
87             add {
88                 removeListeners.Add (value);
89                 Start ();
90             }
91             remove {
92                 removeListeners.Remove (value);
93                 Stop (false);
94             }
95         }
96
97         public DomainInfo[] Domains
98         {
99             get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
100         }
101
102         public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
103                                                      DomainBrowserType.Browse, LookupFlags.None) {
104         }
105         
106         public DomainBrowser (Client client, int iface, Protocol proto, string domain,
107                               DomainBrowserType btype, LookupFlags flags)
108         {
109             this.client = client;
110             this.iface = iface;
111             this.proto = proto;
112             this.domain = domain;
113             this.btype = btype;
114             this.flags = flags;
115             cb = OnDomainBrowserCallback;
116         }
117
118         ~DomainBrowser ()
119         {
120             Dispose ();
121         }
122
123         public void Dispose ()
124         {
125             Stop (true);
126         }
127
128         private void Start ()
129         {
130             if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
131                 (addListeners.Count == 0 && removeListeners.Count == 0))
132                 return;
133
134             lock (client) {
135                 IntPtr domainPtr = Utility.StringToPtr (domain);
136                 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, flags,
137                                                    cb, IntPtr.Zero);
138                 Utility.Free (domainPtr);
139             }
140         }
141
142         private void Stop (bool force)
143         {
144             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
145                 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
146                 lock (client) {
147                     avahi_domain_browser_free (handle);
148                     handle = IntPtr.Zero;
149                 }
150             }
151         }
152
153         private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
154                                               IntPtr domain, LookupResultFlags flags, IntPtr userdata)
155         {
156
157             DomainInfo info;
158             info.NetworkInterface = iface;
159             info.Protocol = proto;
160             info.Domain = Utility.PtrToString (domain);
161             info.Flags = flags;
162
163             infos.Add (info);
164
165             switch (bevent) {
166             case BrowserEvent.Added:
167                 infos.Add (info);
168
169                 foreach (DomainInfoHandler handler in addListeners)
170                     handler (this, info);
171                 break;
172             case BrowserEvent.Removed:
173                 infos.Remove (info);
174
175                 foreach (DomainInfoHandler handler in removeListeners)
176                     handler (this, info);
177                 break;
178             default:
179                 EmitBrowserEvent (bevent);
180                 break;
181             }
182         }
183     }
184 }