]> git.meshlink.io Git - catta/blob - avahi-sharp/DomainBrowser.cs
housekeeping
[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
58         private ArrayList addListeners = new ArrayList ();
59         private ArrayList removeListeners = new ArrayList ();
60         
61         [DllImport ("avahi-client")]
62         private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
63                                                                IntPtr domain, int btype, DomainBrowserCallback cb,
64                                                                IntPtr userdata);
65
66         [DllImport ("avahi-client")]
67         private static extern void avahi_domain_browser_free (IntPtr handle);
68
69         public event DomainInfoHandler DomainAdded
70         {
71             add {
72                 addListeners.Add (value);
73                 Start ();
74             }
75             remove {
76                 addListeners.Remove (value);
77                 Stop (false);
78             }
79         }
80         
81         public event DomainInfoHandler DomainRemoved
82         {
83             add {
84                 removeListeners.Add (value);
85                 Start ();
86             }
87             remove {
88                 removeListeners.Remove (value);
89                 Stop (false);
90             }
91         }
92
93         public DomainInfo[] Domains
94         {
95             get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
96         }
97
98         public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
99                                                      DomainBrowserType.Browse) {
100         }
101         
102         public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype)
103         {
104             this.client = client;
105             this.iface = iface;
106             this.proto = proto;
107             this.domain = domain;
108             this.btype = btype;
109         }
110
111         ~DomainBrowser ()
112         {
113             Dispose ();
114         }
115
116         public void Dispose ()
117         {
118             Stop (true);
119         }
120
121         private void Start ()
122         {
123             if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
124                 return;
125
126             IntPtr domainPtr = Utility.StringToPtr (domain);
127             handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
128                                                OnDomainBrowserCallback, IntPtr.Zero);
129             Utility.Free (domainPtr);
130         }
131
132         private void Stop (bool force)
133         {
134             if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
135                 avahi_domain_browser_free (handle);
136                 handle = IntPtr.Zero;
137             }
138         }
139
140         private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
141                                               IntPtr domain, IntPtr userdata)
142         {
143
144             DomainInfo info;
145             info.NetworkInterface = iface;
146             info.Protocol = proto;
147             info.Domain = Utility.PtrToString (domain);
148
149             infos.Add (info);
150             
151             if (bevent == BrowserEvent.Added) {
152                 infos.Add (info);
153
154                 foreach (DomainInfoHandler handler in addListeners)
155                     handler (this, info);
156             } else {
157                 infos.Remove (info);
158
159                 foreach (DomainInfoHandler handler in removeListeners)
160                     handler (this, info);
161             }
162         }
163     }
164 }