]> git.meshlink.io Git - catta/blob - avahi-sharp/DomainBrowser.cs
s/Host/HostName/ in a couple places, and keep references to callbacks to
[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 (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
126                 return;
127
128             IntPtr domainPtr = Utility.StringToPtr (domain);
129             handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
130                                                cb, IntPtr.Zero);
131             Utility.Free (domainPtr);
132         }
133
134         private void Stop (bool force)
135         {
136             if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
137                 avahi_domain_browser_free (handle);
138                 handle = IntPtr.Zero;
139             }
140         }
141
142         private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
143                                               IntPtr domain, IntPtr userdata)
144         {
145
146             DomainInfo info;
147             info.NetworkInterface = iface;
148             info.Protocol = proto;
149             info.Domain = Utility.PtrToString (domain);
150
151             infos.Add (info);
152             
153             if (bevent == BrowserEvent.Added) {
154                 infos.Add (info);
155
156                 foreach (DomainInfoHandler handler in addListeners)
157                     handler (this, info);
158             } else {
159                 infos.Remove (info);
160
161                 foreach (DomainInfoHandler handler in removeListeners)
162                     handler (this, info);
163             }
164         }
165     }
166 }