]> git.meshlink.io Git - catta/blob - avahi-sharp/ServiceTypeBrowser.cs
lose the dep on avahi-glib, and run the avahi loop in a thread
[catta] / avahi-sharp / ServiceTypeBrowser.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 ServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
29                                                        IntPtr type, IntPtr domain, IntPtr userdata);
30     
31     public struct ServiceTypeInfo
32     {
33         public int NetworkInterface;
34         public Protocol Protocol;
35         public string Domain;
36         public string ServiceType;
37     }
38
39     public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfo info);
40     
41     public class ServiceTypeBrowser : IDisposable
42     {
43         private IntPtr handle;
44         private ArrayList infos = new ArrayList ();
45         private Client client;
46         private int iface;
47         private Protocol proto;
48         private string domain;
49         private ServiceTypeBrowserCallback cb;
50
51         private ArrayList addListeners = new ArrayList ();
52         private ArrayList removeListeners = new ArrayList ();
53         
54         [DllImport ("avahi-client")]
55         private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
56                                                                      IntPtr domain, ServiceTypeBrowserCallback cb,
57                                                                      IntPtr userdata);
58
59         [DllImport ("avahi-client")]
60         private static extern void avahi_service_type_browser_free (IntPtr handle);
61
62         public event ServiceTypeInfoHandler ServiceTypeAdded
63         {
64             add {
65                 addListeners.Add (value);
66                 Start ();
67             }
68             remove {
69                 addListeners.Remove (value);
70                 Stop (false);
71             }
72         }
73         
74         public event ServiceTypeInfoHandler ServiceTypeRemoved
75         {
76             add {
77                 removeListeners.Add (value);
78                 Start ();
79             }
80             remove {
81                 removeListeners.Remove (value);
82                 Stop (false);
83             }
84         }
85
86         public ServiceTypeInfo[] ServiceTypes
87         {
88             get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
89         }
90
91         public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
92         {
93         }
94
95         public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified, domain)
96         {
97         }
98         
99         public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain)
100         {
101             this.client = client;
102             this.iface = iface;
103             this.proto = proto;
104             this.domain = domain;
105             cb = OnServiceTypeBrowserCallback;
106         }
107
108         ~ServiceTypeBrowser ()
109         {
110             Dispose ();
111         }
112
113         public void Dispose ()
114         {
115             Stop (true);
116         }
117
118         private void Start ()
119         {
120             if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
121                 (addListeners.Count == 0 && removeListeners.Count == 0))
122                 return;
123
124             lock (client) {
125                 IntPtr domainPtr = Utility.StringToPtr (domain);
126                 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr,
127                                                          cb, IntPtr.Zero);
128                 Utility.Free (domainPtr);
129             }
130         }
131
132         private void Stop (bool force)
133         {
134             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
135                 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
136
137                 lock (client) {
138                     avahi_service_type_browser_free (handle);
139                     handle = IntPtr.Zero;
140                 }
141             }
142         }
143
144         private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
145                                                    IntPtr type, IntPtr domain, IntPtr userdata)
146         {
147
148             ServiceTypeInfo info;
149             info.NetworkInterface = iface;
150             info.Protocol = proto;
151             info.Domain = Utility.PtrToString (domain);
152             info.ServiceType = Utility.PtrToString (type);
153
154             infos.Add (info);
155             
156             if (bevent == BrowserEvent.Added) {
157                 infos.Add (info);
158
159                 foreach (ServiceTypeInfoHandler handler in addListeners)
160                     handler (this, info);
161             } else {
162                 infos.Remove (info);
163
164                 foreach (ServiceTypeInfoHandler handler in removeListeners)
165                     handler (this, info);
166             }
167         }
168     }
169 }