]> git.meshlink.io Git - catta/blob - avahi-sharp/EntryGroup.cs
allow varags for the TXT data in AddService
[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         
44         [DllImport ("avahi-client")]
45         private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
46
47         [DllImport ("avahi-client")]
48         private static extern void avahi_entry_group_commit (IntPtr group);
49
50         [DllImport ("avahi-client")]
51         private static extern void avahi_entry_group_reset (IntPtr group);
52
53         [DllImport ("avahi-client")]
54         private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
55
56         [DllImport ("avahi-client")]
57         private static extern bool avahi_entry_group_is_empty (IntPtr group);
58
59         [DllImport ("avahi-client")]
60         private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
61                                                                          IntPtr name, IntPtr type, IntPtr domain,
62                                                                          IntPtr host, UInt16 port, IntPtr strlst);
63         
64         [DllImport ("avahi-client")]
65         private static extern void avahi_entry_group_free (IntPtr group);
66
67         [DllImport ("avahi-common")]
68         private static extern IntPtr avahi_string_list_new (IntPtr txt);
69
70         [DllImport ("avahi-common")]
71         private static extern IntPtr avahi_string_list_add (IntPtr list, IntPtr txt);
72
73         [DllImport ("avahi-common")]
74         private static extern void avahi_string_list_free (IntPtr list);
75
76         public event EntryGroupStateHandler StateChanged;
77         
78         public EntryGroupState State
79         {
80             get { return avahi_entry_group_get_state (handle); }
81         }
82
83         public bool IsEmpty
84         {
85             get { return avahi_entry_group_is_empty (handle); }
86         }
87         
88         public EntryGroup (Client client)
89         {
90             this.client = client;
91             handle = avahi_entry_group_new (client.Handle, OnEntryGroupCallback, IntPtr.Zero);
92             client.CheckError ();
93         }
94
95         ~EntryGroup ()
96         {
97             Dispose ();
98         }
99
100         public void Dispose ()
101         {
102             if (handle != IntPtr.Zero) {
103                 avahi_entry_group_free (handle);
104                 handle = IntPtr.Zero;
105             }
106         }
107
108         public void Commit ()
109         {
110             avahi_entry_group_commit (handle);
111             client.CheckError ();
112         }
113
114         public void Reset ()
115         {
116             avahi_entry_group_reset (handle);
117             client.CheckError ();
118         }
119
120         public void AddService (string name, string type, string domain,
121                                 UInt16 port, string[] txt)
122         {
123             AddService (-1, Protocol.Unspecified, name, type, domain, null, port, txt);
124         }
125
126         public void AddService (int iface, Protocol proto, string name, string type, string domain,
127                                 string host, UInt16 port, params string[] txt)
128         {
129             IntPtr list = avahi_string_list_new (IntPtr.Zero);
130
131             if (txt != null) {
132                 foreach (string item in txt) {
133                     IntPtr itemPtr = Utility.StringToPtr (item);
134                     list = avahi_string_list_add (list, itemPtr);
135                     Utility.Free (itemPtr);
136                 }
137             }
138
139             IntPtr namePtr = Utility.StringToPtr (name);
140             IntPtr typePtr = Utility.StringToPtr (type);
141             IntPtr domainPtr = Utility.StringToPtr (domain);
142             IntPtr hostPtr = Utility.StringToPtr (host);
143             avahi_entry_group_add_service_strlst (handle, iface, proto, namePtr, typePtr, domainPtr,
144                                                   hostPtr, port, list);
145             avahi_string_list_free (list);
146
147             client.CheckError ();
148         }
149
150         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
151         {
152             if (StateChanged != null)
153                 StateChanged (this, state);
154         }
155     }
156 }