]> git.meshlink.io Git - catta/blob - avahi-sharp/AddressResolver.cs
bump version number to 0.6
[catta] / avahi-sharp / AddressResolver.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.Collections;
24 using System.Net;
25 using System.Runtime.InteropServices;
26 using Mono.Unix;
27
28 namespace Avahi
29 {
30
31     internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
32                                                     ResolverEvent revent, Protocol aproto, IntPtr address,
33                                                     IntPtr hostname, IntPtr userdata);
34
35     public delegate void HostAddressHandler (object o, string host, IPAddress address);
36     
37     public class AddressResolver : IDisposable
38     {
39         private IntPtr handle;
40         private Client client;
41         private int iface;
42         private Protocol proto;
43         private IPAddress address;
44         private AddressResolverCallback cb;
45
46         private IPAddress currentAddress;
47         private string currentHost;
48
49         private ArrayList foundListeners = new ArrayList ();
50         private ArrayList timeoutListeners = new ArrayList ();
51         
52         [DllImport ("avahi-client")]
53         private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
54                                                                  IntPtr address, AddressResolverCallback cb,
55                                                                  IntPtr userdata);
56
57         [DllImport ("avahi-client")]
58         private static extern void avahi_address_resolver_free (IntPtr handle);
59
60         public event HostAddressHandler Found
61         {
62             add {
63                 foundListeners.Add (value);
64                 Start ();
65             }
66             remove {
67                 foundListeners.Remove (value);
68                 Stop (false);
69             }
70         }
71         
72         public event EventHandler Timeout
73         {
74             add {
75                 timeoutListeners.Add (value);
76                 Start ();
77             }
78             remove {
79                 timeoutListeners.Remove (value);
80                 Stop (false);
81             }
82         }
83
84         public IPAddress Address
85         {
86             get { return currentAddress; }
87         }
88
89         public string HostName
90         {
91             get { return currentHost; }
92         }
93
94         public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
95         {
96         }
97
98         public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
99         {
100             this.client = client;
101             this.iface = iface;
102             this.proto = proto;
103             this.address = address;
104             cb = OnAddressResolverCallback;
105         }
106
107         ~AddressResolver ()
108         {
109             Dispose ();
110         }
111
112         public void Dispose ()
113         {
114             Stop (true);
115         }
116
117         private void Start ()
118         {
119             if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
120                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
121                 return;
122
123             IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
124
125             lock (client) {
126                 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
127                                                      cb, IntPtr.Zero);
128             }
129             
130             Utility.Free (addrPtr);
131         }
132
133         private void Stop (bool force)
134         {
135             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
136                 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
137
138                 lock (client) {
139                     avahi_address_resolver_free (handle);
140                     handle = IntPtr.Zero;
141                 }
142             }
143         }
144
145         private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
146                                                 ResolverEvent revent, Protocol aproto, IntPtr address,
147                                                 IntPtr hostname, IntPtr userdata)
148         {
149             if (revent == ResolverEvent.Found) {
150                 currentAddress = Utility.PtrToAddress (address);
151                 currentHost = Utility.PtrToString (hostname);
152
153                 foreach (HostAddressHandler handler in foundListeners)
154                     handler (this, currentHost, currentAddress);
155             } else {
156                 currentAddress = null;
157                 currentHost = null;
158                 
159                 foreach (EventHandler handler in timeoutListeners)
160                     handler (this, new EventArgs ());
161             }
162         }
163     }
164 }