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