]> git.meshlink.io Git - catta/blob - avahi-sharp/ServiceTypeBrowser.cs
allow varags for the TXT data in AddService
[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 (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
121                 return;
122
123             IntPtr domainPtr = Utility.StringToPtr (domain);
124             handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr,
125                                                      cb, IntPtr.Zero);
126             Utility.Free (domainPtr);
127         }
128
129         private void Stop (bool force)
130         {
131             if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
132                 avahi_service_type_browser_free (handle);
133                 handle = IntPtr.Zero;
134             }
135         }
136
137         private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
138                                                    IntPtr type, IntPtr domain, IntPtr userdata)
139         {
140
141             ServiceTypeInfo info;
142             info.NetworkInterface = iface;
143             info.Protocol = proto;
144             info.Domain = Utility.PtrToString (domain);
145             info.ServiceType = Utility.PtrToString (type);
146
147             infos.Add (info);
148             
149             if (bevent == BrowserEvent.Added) {
150                 infos.Add (info);
151
152                 foreach (ServiceTypeInfoHandler handler in addListeners)
153                     handler (this, info);
154             } else {
155                 infos.Remove (info);
156
157                 foreach (ServiceTypeInfoHandler handler in removeListeners)
158                     handler (this, info);
159             }
160         }
161     }
162 }