1 /***
2     This file is part of avahi.
3 
4     avahi is free software; you can redistribute it and/or modify it
5     under the terms of the GNU Lesser General Public License as
6     published by the Free Software Foundation; either version 2.1 of the
7     License, or (at your option) any later version.
8 
9     avahi is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11     or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12     Public License for more details.
13 
14     You should have received a copy of the GNU Lesser General Public
15     License along with avahi; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17     USA.
18 ***/
19 
20 using System;
21 using System.Collections;
22 using System.Text;
23 using System.Net;
24 using Avahi;
25 
26 public class AvahiTest {
27     private static Client client;
28     private static ArrayList objects = new ArrayList ();
29 
Main()30     public static void Main () {
31         client = new Client ();
32 
33 	Console.WriteLine ("joined service name: " + Client.JoinServiceName ("FooBar", "_foo", "local"));
34 
35         EntryGroup eg = new EntryGroup (client);
36         eg.StateChanged += OnEntryGroupChanged;
37         eg.AddService ("foobar2", "_dingdong._tcp", client.DomainName,
38                        444, new string[] { "foo=stuff", "bar=stuff2", "baz=stuff3" });
39         eg.Commit ();
40         BrowseServiceTypes ("local");
41         Console.WriteLine ("Press enter to quit");
42         Console.ReadLine ();
43         client.Dispose ();
44     }
45 
OnEntryGroupChanged(object o, EntryGroupStateArgs args)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 
OnDomainAdded(object o, DomainInfoArgs args)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 
BrowseServiceTypes(string domain)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 
OnCacheExhausted(object o, EventArgs args)72     private static void OnCacheExhausted (object o, EventArgs args)
73     {
74         Console.WriteLine ("Cache is exhausted");
75     }
76 
OnServiceTypeAdded(object o, ServiceTypeInfoArgs args)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 
OnServiceAdded(object o, ServiceInfoArgs args)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 
OnServiceResolved(object o, ServiceInfoArgs args)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 
OnAddressResolverFailed(object o, ErrorCodeArgs args)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 
OnAddressResolved(object o, HostAddressArgs args)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 
OnHostNameResolved(object o, HostAddressArgs args)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 }
132