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