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