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