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