Mono.Zeroconf – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // BrowseService.cs
3 //
4 // Author:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2008 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28  
29 using System;
30 using System.Net;
31 using NDesk.DBus;
32 using Mono.Zeroconf;
33  
34 namespace Mono.Zeroconf.Providers.AvahiDBus
35 {
36 public class BrowseService : Service, IResolvableService, IDisposable
37 {
38 private string full_name;
39 private IPHostEntry host_entry;
40 private string host_target;
41 private short port;
42 private bool disposed;
43 private IAvahiServiceResolver resolver;
44  
45 public event ServiceResolvedEventHandler Resolved;
46  
47 public BrowseService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol)
48 : base (name, regtype, replyDomain, @interface, aprotocol)
49 {
50 }
51  
52 public void Dispose ()
53 {
54 lock (this) {
55 disposed = true;
56 DisposeResolver ();
57 }
58 }
59  
60 private void DisposeResolver ()
61 {
62 lock (this) {
63 if (resolver != null) {
64 resolver.Failure -= OnResolveFailure;
65 resolver.Found -= OnResolveFound;
66 resolver.Free ();
67 resolver = null;
68 }
69 }
70 }
71  
72 public void Resolve ()
73 {
74 if (disposed) {
75 throw new InvalidOperationException ("The service has been disposed and cannot be resolved. " +
76 " Perhaps this service was removed?");
77 }
78  
79 DBusManager.Bus.TrapSignals ();
80  
81 lock (this) {
82 if (resolver != null) {
83 throw new InvalidOperationException ("The service is already running a resolve operation");
84 }
85  
86 ObjectPath path = DBusManager.Server.ServiceResolverNew (AvahiInterface, AvahiProtocol,
87 Name ?? String.Empty, RegType ?? String.Empty, ReplyDomain ?? String.Empty,
88 AvahiProtocol, LookupFlags.None);
89  
90 resolver = DBusManager.GetObject<IAvahiServiceResolver> (path);
91 }
92  
93 resolver.Failure += OnResolveFailure;
94 resolver.Found += OnResolveFound;
95  
96 DBusManager.Bus.UntrapSignals ();
97 }
98  
99 protected virtual void OnResolved ()
100 {
101 ServiceResolvedEventHandler handler = Resolved;
102 if (handler != null) {
103 handler (this, new ServiceResolvedEventArgs (this));
104 }
105 }
106  
107 private void OnResolveFailure (string error)
108 {
109 DisposeResolver ();
110 }
111  
112 private void OnResolveFound (int @interface, Protocol protocol, string name,
113 string type, string domain, string host, Protocol aprotocol, string address,
114 ushort port, byte [][] txt, LookupResultFlags flags)
115 {
116 Name = name;
117 RegType = type;
118 AvahiInterface = @interface;
119 AvahiProtocol = protocol;
120 ReplyDomain = domain;
121 TxtRecord = new TxtRecord (txt);
122  
123 this.full_name = String.Format ("{0}.{1}.{2}", name.Replace (" ", "\\032"), type, domain);
124 this.port = (short)port;
125 this.host_target = host;
126  
127 host_entry = new IPHostEntry ();
128 host_entry.AddressList = new IPAddress[1];
129 if (IPAddress.TryParse (address, out host_entry.AddressList[0]) && protocol == Protocol.IPv6) {
130 host_entry.AddressList[0].ScopeId = @interface;
131 }
132 host_entry.HostName = host;
133  
134 OnResolved ();
135 }
136  
137 public string FullName {
138 get { return full_name; }
139 }
140  
141 public IPHostEntry HostEntry {
142 get { return host_entry; }
143 }
144  
145 public string HostTarget {
146 get { return host_target; }
147 }
148  
149 public short Port {
150 get { return port; }
151 }
152 }
153 }