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