]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
housekeeping
[catta] / avahi-sharp / Client.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
23 using System;
24 using System.Collections;
25 using System.Runtime.InteropServices;
26
27 namespace Avahi
28 {
29     internal enum ResolverEvent {
30         Found,
31         Timeout
32     }
33     
34     internal enum BrowserEvent {
35         Added,
36         Removed
37     }
38     
39     internal delegate void ClientHandler (IntPtr client, ClientState state, IntPtr userData);
40
41     public enum Protocol {
42         Unspecified = 0,
43         IPv4 = 2,
44         IPv6 = 10
45     }
46     
47     public enum ClientState {
48         Invalid,
49         Registering,
50         Running,
51         Collision,
52         Disconnected = 100
53     }
54     
55     public class Client : IDisposable
56     {
57         private IntPtr handle;
58
59         [DllImport ("avahi-client")]
60         private static extern IntPtr avahi_client_new (IntPtr poll, ClientHandler handler,
61                                                        IntPtr userData, out int error);
62
63         [DllImport ("avahi-client")]
64         private static extern void avahi_client_free (IntPtr handle);
65
66         [DllImport ("avahi-client")]
67         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
68
69         [DllImport ("avahi-client")]
70         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
71
72         [DllImport ("avahi-client")]
73         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
74
75         [DllImport ("avahi-client")]
76         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
77
78         [DllImport ("avahi-client")]
79         private static extern ClientState avahi_client_get_state (IntPtr handle);
80
81         [DllImport ("avahi-client")]
82         private static extern int avahi_client_errno (IntPtr handle);
83         
84         [DllImport ("avahi-glib")]
85         private static extern IntPtr avahi_glib_poll_new (IntPtr context, int priority);
86
87         [DllImport ("avahi-glib")]
88         private static extern IntPtr avahi_glib_poll_get (IntPtr gpoll);
89
90         internal IntPtr Handle
91         {
92             get { return handle; }
93         }
94         
95         public string Version
96         {
97             get { return Utility.PtrToString (avahi_client_get_version_string (handle)); }
98         }
99
100         public string HostName
101         {
102             get { return Utility.PtrToString (avahi_client_get_host_name (handle)); }
103         }
104
105         public string DomainName
106         {
107             get { return Utility.PtrToString (avahi_client_get_domain_name (handle)); }
108         }
109
110         public string HostNameFqdn
111         {
112             get { return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle)); }
113         }
114
115         public ClientState State
116         {
117             get { return (ClientState) avahi_client_get_state (handle); }
118         }
119
120         internal int LastError
121         {
122             get { return avahi_client_errno (handle); }
123         }
124
125         public Client ()
126         {
127             IntPtr gpoll = avahi_glib_poll_new (IntPtr.Zero, 0);
128             IntPtr poll = avahi_glib_poll_get (gpoll);
129
130             int error;
131             handle = avahi_client_new (poll, OnClientCallback, IntPtr.Zero, out error);
132             if (error != 0)
133                 throw new ClientException (error);
134         }
135
136         ~Client ()
137         {
138             Dispose ();
139         }
140
141         public void Dispose ()
142         {
143             if (handle != IntPtr.Zero) {
144                 avahi_client_free (handle);
145                 handle = IntPtr.Zero;
146             }
147         }
148
149         internal void CheckError ()
150         {
151             int error = LastError;
152
153             if (error != 0)
154                 throw new ClientException (error);
155         }
156         
157         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
158         {
159             Console.WriteLine ("Got new state: " + state);
160         }
161     }
162 }