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