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