]> git.meshlink.io Git - catta/blob - avahi-sharp/AvahiTest.cs
* add byte[] overloads for EntryGroup methods
[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.Collections;
24 using System.Text;
25 using System.Net;
26 using Avahi;
27
28 public class AvahiTest {
29     private static Client client;
30     private static ArrayList objects = new ArrayList ();
31
32     public static void Main () {
33         client = new Client ();
34
35         Console.WriteLine ("joined service name: " + EntryGroup.JoinServiceName ("FooBar", "_foo", "local"));
36
37         EntryGroup eg = new EntryGroup (client);
38         eg.StateChanged += OnEntryGroupChanged;
39         eg.AddService ("foobar2", "_daap._tcp", client.DomainName,
40                        444, new string[] { "foo=stuff", "bar=stuff2", "baz=stuff3" });
41         eg.Commit ();
42         Console.WriteLine ("Press enter to quit");
43         Console.ReadLine ();
44     }
45
46     private static void OnEntryGroupChanged (object o, EntryGroupStateArgs args)
47     {
48         Console.WriteLine ("Entry group status: " + args.State);
49         if (args.State == EntryGroupState.Established) {
50             DomainBrowser browser = new DomainBrowser (client);
51             objects.Add (browser);
52             
53             browser.DomainAdded += OnDomainAdded;
54         }
55     }
56
57     private static void OnDomainAdded (object o, DomainInfoArgs args)
58     {
59         Console.WriteLine ("Got domain added: " + args.Domain.Domain);
60         BrowseServiceTypes (args.Domain.Domain);
61     }
62
63     private static void BrowseServiceTypes (string domain)
64     {
65         ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain);
66         objects.Add (stb);
67         
68         stb.CacheExhausted += OnCacheExhausted;
69         stb.ServiceTypeAdded += OnServiceTypeAdded;
70     }
71
72     private static void OnCacheExhausted (object o, EventArgs args)
73     {
74         Console.WriteLine ("Cache is exhausted");
75     }
76
77     private static void OnServiceTypeAdded (object o, ServiceTypeInfoArgs args)
78     {
79         Console.WriteLine ("Got service type: " + args.ServiceType.ServiceType);
80         ServiceBrowser sb = new ServiceBrowser (client, args.ServiceType.ServiceType, args.ServiceType.Domain);
81         objects.Add (sb);
82         
83         sb.ServiceAdded += OnServiceAdded;
84     }
85
86     private static void OnServiceAdded (object o, ServiceInfoArgs args)
87     {
88         // Console.WriteLine ("Got service: " + info.Name);
89         ServiceResolver resolver = new ServiceResolver (client, args.Service);
90         objects.Add (resolver);
91         resolver.Found += OnServiceResolved;
92     }
93
94     private static void OnServiceResolved (object o, ServiceInfoArgs args)
95     {
96         objects.Remove (o);
97         
98         Console.WriteLine ("Service '{0}' at {1}:{2}", args.Service.Name, args.Service.HostName, args.Service.Port);
99         foreach (byte[] bytes in args.Service.Text) {
100             Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
101         }
102
103         AddressResolver ar = new AddressResolver (client, args.Service.Address);
104         objects.Add (ar);
105
106         ar.Found += OnAddressResolved;
107         ar.Failed += OnAddressResolverFailed;
108     }
109
110     private static void OnAddressResolverFailed (object o, ErrorCodeArgs args)
111     {
112         Console.WriteLine ("Failed to resolve '{0}': {1}", (o as AddressResolver).Address, args.ErrorCode);
113     }
114
115     private static void OnAddressResolved (object o, HostAddressArgs args)
116     {
117         objects.Remove (o);
118         
119         Console.WriteLine ("Resolved {0} to {1}", args.Address, args.Host);
120         HostNameResolver hr = new HostNameResolver (client, args.Host);
121         objects.Add (hr);
122         
123         hr.Found += OnHostNameResolved;
124     }
125
126     private static void OnHostNameResolved (object o, HostAddressArgs args)
127     {
128         objects.Remove (o);
129         Console.WriteLine ("Resolved {0} to {1}", args.Host, args.Address);
130     }
131 }