Mono.Zeroconf – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // ServiceBrowser.cs
3 //
4 // Author:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2007-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.Threading;
31 using NDesk.DBus;
32  
33 namespace Mono.Zeroconf.Providers.AvahiDBus
34 {
35 public class RegisterService : Service, IRegisterService
36 {
37 private ushort port;
38 private IAvahiEntryGroup entry_group;
39  
40 public event RegisterServiceEventHandler Response;
2 office 41  
42 private string originalName;
43 private int retryNameModifier = 2;
44  
1 office 45 public RegisterService ()
46 {
47 }
48  
49 public RegisterService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol)
50 : base (name, regtype, replyDomain, @interface, aprotocol)
51 {
52 }
53  
54 public void Register ()
55 {
56 RegisterDBus ();
57  
58 byte [][] txt_record = TxtRecord == null
59 ? new byte[0][]
60 : Mono.Zeroconf.Providers.AvahiDBus.TxtRecord.Render (TxtRecord);
61  
62 entry_group.AddService (AvahiInterface, AvahiProtocol, PublishFlags.None,
63 Name ?? String.Empty, RegType ?? String.Empty, ReplyDomain ?? String.Empty,
64 String.Empty, port, txt_record);
65  
66 entry_group.Commit ();
67 }
68  
69 private void RegisterDBus ()
70 {
71 try {
72 Monitor.Enter (this);
73 DBusManager.Bus.TrapSignals ();
74  
75 if (entry_group != null) {
76 entry_group.Reset ();
77 return;
78 }
79  
80 if (DBusManager.Server.GetState () != AvahiServerState.Running) {
81 throw new ApplicationException ("Avahi Server is not in the Running state");
82 }
83  
84 ObjectPath path = DBusManager.Server.EntryGroupNew ();
85 entry_group = DBusManager.GetObject<IAvahiEntryGroup> (path);
86  
87 Monitor.Exit (this);
88  
89 entry_group.StateChanged += OnEntryGroupStateChanged;
90 } finally {
91 Monitor.Exit (this);
92 DBusManager.Bus.UntrapSignals ();
93 }
94 }
95  
96 private void OnEntryGroupStateChanged (EntryGroupState state, string error)
97 {
98 switch (state) {
99 case EntryGroupState.Collision:
100 if (!OnResponse (ErrorCode.Collision)) {
2 office 101 if (originalName == null)
102 originalName = Name;
103  
104 Name = originalName + " (" + retryNameModifier + ")";
105 retryNameModifier++;
106  
107 Console.WriteLine("ZeroConf had a name collision, trying: " + Name);
108  
109 Register();
1 office 110 }
111 break;
112 case EntryGroupState.Failure:
113 if (!OnResponse (ErrorCode.Failure)) {
2 office 114 Console.WriteLine("Mono.ZeroConf failed to register name with AvahiDBus");
1 office 115 }
116 break;
117 case EntryGroupState.Established:
118 OnResponse (ErrorCode.Ok);
119 break;
120 }
121 }
122  
123 protected virtual bool OnResponse (ErrorCode errorCode)
124 {
125 RegisterServiceEventArgs args = new RegisterServiceEventArgs ();
126  
127 args.Service = this;
128 args.IsRegistered = false;
129 args.ServiceError = AvahiUtils.ErrorCodeToServiceError (errorCode);
130  
131 if (errorCode == ErrorCode.Ok) {
132 args.IsRegistered = true;
133 }
134  
135 RegisterServiceEventHandler handler = Response;
136 if (handler != null) {
137 handler (this, args);
138 return true;
139 }
140  
141 return false;
142 }
143  
144 public void Dispose ()
145 {
146 lock (this) {
147 if (entry_group != null) {
148 entry_group.Reset ();
149 entry_group.Free ();
150 entry_group = null;
151 }
152 }
153 }
154  
155 public short Port {
156 get { return (short)UPort; }
157 set { UPort = (ushort)value; }
158 }
159  
160 public ushort UPort {
161 get { return port; }
162 set { port = value; }
163 }
164 }
165 }