]> git.meshlink.io Git - catta/blob - avahi-sharp/AvahiTest.cs
395d5cb104d6acc67068195fd4483193fc6882ed
[catta] / avahi-sharp / AvahiTest.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.Text;
24 using System.Net;
25 using Avahi;
26
27 public class AvahiTest {
28         private static Client client;
29
30         public static void Main () {
31                 client = new Client ();
32
33                 EntryGroup eg = new EntryGroup (client);
34                 eg.StateChanged += OnEntryGroupChanged;
35                 eg.AddService ("foobar2", "_daap._tcp", client.DomainName,
36                                444, new string[] { "foo", "bar", "baz" });
37                 eg.Commit ();
38                 Console.WriteLine ("Press enter to quit");
39                 Console.ReadLine ();
40         }
41
42         private static void OnEntryGroupChanged (object o, EntryGroupState state)
43         {
44                 Console.WriteLine ("Entry group status: " + state);
45
46                 /*
47                 if (state == EntryGroupState.Established) {
48                         DomainBrowser browser = new DomainBrowser (client);
49                         browser.DomainAdded += OnDomainAdded;
50                 }
51                 */
52
53                 BrowseServiceTypes ("dns-sd.org");
54         }
55
56         private static void OnDomainAdded (object o, DomainInfo info)
57         {
58                 Console.WriteLine ("Got domain added: " + info.Domain);
59                 BrowseServiceTypes (info.Domain);
60         }
61
62         private static void BrowseServiceTypes (string domain)
63         {
64                 ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain);
65                 stb.CacheExhausted += OnCacheExhausted;
66                 stb.ServiceTypeAdded += OnServiceTypeAdded;
67         }
68
69         private static void OnCacheExhausted (object o, EventArgs args)
70         {
71                 Console.WriteLine ("Cache is exhausted");
72         }
73
74         private static void OnServiceTypeAdded (object o, ServiceTypeInfo info)
75         {
76                 Console.WriteLine ("Got service type: " + info.ServiceType);
77                 ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain);
78                 sb.ServiceAdded += OnServiceAdded;
79         }
80
81         private static void OnServiceAdded (object o, ServiceInfo info)
82         {
83                 // Console.WriteLine ("Got service: " + info.Name);
84                 ServiceResolver resolver = new ServiceResolver (client, info);
85                 resolver.Found += OnServiceResolved;
86         }
87
88         private static void OnServiceResolved (object o, ServiceInfo info)
89         {
90                 Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port);
91                 foreach (byte[] bytes in info.Text) {
92                         Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
93                 }
94                 AddressResolver ar = new AddressResolver (client, info.Address);
95                 ar.Found += OnAddressResolved;
96         }
97
98         private static void OnAddressResolved (object o, string host, IPAddress address)
99         {
100                 Console.WriteLine ("Resolved {0} to {1}", address, host);
101                 HostNameResolver hr = new HostNameResolver (client, host);
102                 hr.Found += OnHostNameResolved;
103         }
104
105         private static void OnHostNameResolved (object o, string host, IPAddress address)
106         {
107                 Console.WriteLine ("Resolved {0} to {1}", host, address);
108         }
109 }