]> git.meshlink.io Git - catta/blob - avahi-sharp/ServiceBrowser.cs
housekeeping
[catta] / avahi-sharp / ServiceBrowser.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.Net;
24 using System.Collections;
25 using System.Runtime.InteropServices;
26
27 namespace Avahi
28 {
29     internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
30                                                    IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata);
31     
32     public struct ServiceInfo
33     {
34         public int NetworkInterface;
35         public Protocol Protocol;
36         public string Domain;
37         public string ServiceType;
38         public string Name;
39
40         public string Host;
41         public IPAddress Address;
42         public UInt16 Port;
43         public byte[][] Text;
44
45         public static ServiceInfo Zero = new ServiceInfo ();
46     }
47
48     public delegate void ServiceInfoHandler (object o, ServiceInfo info);
49     
50     public class ServiceBrowser : IDisposable
51     {
52         private IntPtr handle;
53         private ArrayList infos = new ArrayList ();
54         private Client client;
55         private int iface;
56         private Protocol proto;
57         private string domain;
58         private string type;
59
60         private ArrayList addListeners = new ArrayList ();
61         private ArrayList removeListeners = new ArrayList ();
62         
63         [DllImport ("avahi-client")]
64         private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
65                                                                 IntPtr domain, ServiceBrowserCallback cb,
66                                                                 IntPtr userdata);
67
68         [DllImport ("avahi-client")]
69         private static extern void avahi_service_browser_free (IntPtr handle);
70
71         public event ServiceInfoHandler ServiceAdded
72         {
73             add {
74                 addListeners.Add (value);
75                 Start ();
76             }
77             remove {
78                 addListeners.Remove (value);
79                 Stop (false);
80             }
81         }
82         
83         public event ServiceInfoHandler ServiceRemoved
84         {
85             add {
86                 removeListeners.Add (value);
87                 Start ();
88             }
89             remove {
90                 removeListeners.Remove (value);
91                 Stop (false);
92             }
93         }
94
95         public ServiceInfo[] Services
96         {
97             get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
98         }
99
100         public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
101         {
102         }
103         
104         public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
105                                                                                   type, domain)
106         {
107         }
108         
109         public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain)
110         {
111             this.client = client;
112             this.iface = iface;
113             this.proto = proto;
114             this.domain = domain;
115             this.type = type;
116             
117             
118         }
119
120         ~ServiceBrowser ()
121         {
122             Dispose ();
123         }
124
125         public void Dispose ()
126         {
127             Stop (true);
128         }
129
130         private void Start ()
131         {
132             if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
133                 return;
134
135             IntPtr domainPtr = Utility.StringToPtr (domain);
136             IntPtr typePtr = Utility.StringToPtr (type);
137             handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr,
138                                                 OnServiceBrowserCallback, IntPtr.Zero);
139             Utility.Free (domainPtr);
140             Utility.Free (typePtr);
141         }
142
143         private void Stop (bool force)
144         {
145             if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
146                 avahi_service_browser_free (handle);
147                 handle = IntPtr.Zero;
148             }
149         }
150
151         private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
152                                                IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata)
153         {
154
155             ServiceInfo info;
156             info.NetworkInterface = iface;
157             info.Protocol = proto;
158             info.Domain = Utility.PtrToString (domain);
159             info.ServiceType = Utility.PtrToString (type);
160             info.Name = Utility.PtrToString (name);
161             info.Host = null;
162             info.Address = null;
163             info.Port = 0;
164             info.Text = null;
165
166             infos.Add (info);
167             
168             if (bevent == BrowserEvent.Added) {
169                 infos.Add (info);
170
171                 foreach (ServiceInfoHandler handler in addListeners)
172                     handler (this, info);
173             } else {
174                 infos.Remove (info);
175
176                 foreach (ServiceInfoHandler handler in removeListeners)
177                     handler (this, info);
178             }
179         }
180     }
181 }