]> git.meshlink.io Git - catta/blob - avahi-sharp/EntryGroup.cs
4373178b1b71dd560fecd60dc78f6ab147b077e2
[catta] / avahi-sharp / EntryGroup.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.Runtime.InteropServices;
24
25
26 namespace Avahi
27 {
28
29     [Flags]
30     public enum PublishFlags {
31         None = 0,
32         Unique = 1,
33         NoProbe = 2,
34         NoAnnounce = 4,
35         AllowMultiple = 8,
36         NoReverse = 16,
37         NoCookie = 32,
38         Update = 64
39     }
40     
41     public enum EntryGroupState {
42         Uncommited,
43         Registering,
44         Established,
45         Collision,
46         Failure
47     }
48
49     internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
50     public delegate void EntryGroupStateHandler (object o, EntryGroupState state);
51     
52     public class EntryGroup : IDisposable
53     {
54         private Client client;
55         private IntPtr handle;
56         private EntryGroupCallback cb;
57         
58         [DllImport ("avahi-client")]
59         private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
60
61         [DllImport ("avahi-client")]
62         private static extern void avahi_entry_group_commit (IntPtr group);
63
64         [DllImport ("avahi-client")]
65         private static extern void avahi_entry_group_reset (IntPtr group);
66
67         [DllImport ("avahi-client")]
68         private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
69
70         [DllImport ("avahi-client")]
71         private static extern bool avahi_entry_group_is_empty (IntPtr group);
72
73         [DllImport ("avahi-client")]
74         private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
75                                                                          PublishFlags flags, IntPtr name, IntPtr type,
76                                                                          IntPtr domain, IntPtr host, UInt16 port,
77                                                                          IntPtr strlst);
78         
79         [DllImport ("avahi-client")]
80         private static extern void avahi_entry_group_free (IntPtr group);
81
82         [DllImport ("avahi-common")]
83         private static extern IntPtr avahi_string_list_new (IntPtr txt);
84
85         [DllImport ("avahi-common")]
86         private static extern IntPtr avahi_string_list_add (IntPtr list, IntPtr txt);
87
88         [DllImport ("avahi-common")]
89         private static extern void avahi_string_list_free (IntPtr list);
90
91         [DllImport ("avahi-common")]
92         private static extern IntPtr avahi_alternative_service_name (IntPtr name);
93
94         public event EntryGroupStateHandler StateChanged;
95         
96         public EntryGroupState State
97         {
98             get {
99                 lock (client) {
100                     return avahi_entry_group_get_state (handle);
101                 }
102             }
103         }
104
105         public bool IsEmpty
106         {
107             get {
108                 lock (client) {
109                     return avahi_entry_group_is_empty (handle);
110                 }
111             }
112         }
113         
114         public EntryGroup (Client client)
115         {
116             this.client = client;
117             cb = OnEntryGroupCallback;
118
119             lock (client) {
120                 handle = avahi_entry_group_new (client.Handle, cb, IntPtr.Zero);
121                 client.CheckError ();
122             }
123         }
124
125         ~EntryGroup ()
126         {
127             Dispose ();
128         }
129
130         public void Dispose ()
131         {
132             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero) {
133                 lock (client) {
134                     avahi_entry_group_free (handle);
135                     handle = IntPtr.Zero;
136                 }
137             }
138         }
139
140         public void Commit ()
141         {
142             lock (client) {
143                 avahi_entry_group_commit (handle);
144                 client.CheckError ();
145             }
146         }
147
148         public void Reset ()
149         {
150             lock (client) {
151                 avahi_entry_group_reset (handle);
152                 client.CheckError ();
153             }
154         }
155
156         public void AddService (string name, string type, string domain,
157                                 UInt16 port, params string[] txt)
158         {
159             AddService (PublishFlags.None, name, type, domain, port, txt);
160         }
161
162         public void AddService (PublishFlags flags, string name, string type, string domain,
163                                 UInt16 port, params string[] txt)
164         {
165             AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
166         }
167
168         public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
169                                 string host, UInt16 port, params string[] txt)
170         {
171             IntPtr list = avahi_string_list_new (IntPtr.Zero);
172
173             if (txt != null) {
174                 foreach (string item in txt) {
175                     IntPtr itemPtr = Utility.StringToPtr (item);
176                     list = avahi_string_list_add (list, itemPtr);
177                     Utility.Free (itemPtr);
178                 }
179             }
180
181             IntPtr namePtr = Utility.StringToPtr (name);
182             IntPtr typePtr = Utility.StringToPtr (type);
183             IntPtr domainPtr = Utility.StringToPtr (domain);
184             IntPtr hostPtr = Utility.StringToPtr (host);
185
186             lock (client) {
187                 avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr,
188                                                       hostPtr, port, list);
189             }
190             
191             avahi_string_list_free (list);
192
193             client.CheckError ();
194         }
195
196         public static string GetAlternativeServiceName (string name) {
197             IntPtr namePtr = Utility.StringToPtr (name);
198             IntPtr result = avahi_alternative_service_name (namePtr);
199             Utility.Free (namePtr);
200
201             return Utility.PtrToStringFree (result);
202         }
203
204         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
205         {
206             if (StateChanged != null)
207                 StateChanged (this, state);
208         }
209     }
210 }