Mono.Zeroconf – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 1 Rev 2
1 // 1 //
2 // ServiceBrowser.cs 2 // ServiceBrowser.cs
3 // 3 //
4 // Author: 4 // Author:
5 // Aaron Bockover <abockover@novell.com> 5 // Aaron Bockover <abockover@novell.com>
6 // 6 //
7 // Copyright (C) 2007-2008 Novell, Inc. 7 // Copyright (C) 2007-2008 Novell, Inc.
8 // 8 //
9 // Permission is hereby granted, free of charge, to any person obtaining 9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the 10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including 11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish, 12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to 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 14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions: 15 // the following conditions:
16 // 16 //
17 // The above copyright notice and this permission notice shall be 17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software. 18 // included in all copies or substantial portions of the Software.
19 // 19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 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 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 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. 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // 27 //
28   28  
29 using System; 29 using System;
30 using System.Threading; 30 using System.Threading;
31 using NDesk.DBus; 31 using NDesk.DBus;
32   32  
33 namespace Mono.Zeroconf.Providers.AvahiDBus 33 namespace Mono.Zeroconf.Providers.AvahiDBus
34 { 34 {
35 public class RegisterService : Service, IRegisterService 35 public class RegisterService : Service, IRegisterService
36 { 36 {
37 private ushort port; 37 private ushort port;
38 private IAvahiEntryGroup entry_group; 38 private IAvahiEntryGroup entry_group;
39 39
40 public event RegisterServiceEventHandler Response; 40 public event RegisterServiceEventHandler Response;
41 41  
-   42 private string originalName;
-   43 private int retryNameModifier = 2;
-   44  
42 public RegisterService () 45 public RegisterService ()
43 { 46 {
44 } 47 }
45 48
46 public RegisterService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol) 49 public RegisterService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol)
47 : base (name, regtype, replyDomain, @interface, aprotocol) 50 : base (name, regtype, replyDomain, @interface, aprotocol)
48 { 51 {
49 } 52 }
50 53
51 public void Register () 54 public void Register ()
52 { 55 {
53 RegisterDBus (); 56 RegisterDBus ();
54   57  
55 byte [][] txt_record = TxtRecord == null 58 byte [][] txt_record = TxtRecord == null
56 ? new byte[0][] 59 ? new byte[0][]
57 : Mono.Zeroconf.Providers.AvahiDBus.TxtRecord.Render (TxtRecord); 60 : Mono.Zeroconf.Providers.AvahiDBus.TxtRecord.Render (TxtRecord);
58 61
59 entry_group.AddService (AvahiInterface, AvahiProtocol, PublishFlags.None, 62 entry_group.AddService (AvahiInterface, AvahiProtocol, PublishFlags.None,
60 Name ?? String.Empty, RegType ?? String.Empty, ReplyDomain ?? String.Empty, 63 Name ?? String.Empty, RegType ?? String.Empty, ReplyDomain ?? String.Empty,
61 String.Empty, port, txt_record); 64 String.Empty, port, txt_record);
62   65  
63 entry_group.Commit (); 66 entry_group.Commit ();
64 } 67 }
65 68
66 private void RegisterDBus () 69 private void RegisterDBus ()
67 { 70 {
68 try { 71 try {
69 Monitor.Enter (this); 72 Monitor.Enter (this);
70 DBusManager.Bus.TrapSignals (); 73 DBusManager.Bus.TrapSignals ();
71 74
72 if (entry_group != null) { 75 if (entry_group != null) {
73 entry_group.Reset (); 76 entry_group.Reset ();
74 return; 77 return;
75 } 78 }
76 79
77 if (DBusManager.Server.GetState () != AvahiServerState.Running) { 80 if (DBusManager.Server.GetState () != AvahiServerState.Running) {
78 throw new ApplicationException ("Avahi Server is not in the Running state"); 81 throw new ApplicationException ("Avahi Server is not in the Running state");
79 } 82 }
80   83  
81 ObjectPath path = DBusManager.Server.EntryGroupNew (); 84 ObjectPath path = DBusManager.Server.EntryGroupNew ();
82 entry_group = DBusManager.GetObject<IAvahiEntryGroup> (path); 85 entry_group = DBusManager.GetObject<IAvahiEntryGroup> (path);
83 86
84 Monitor.Exit (this); 87 Monitor.Exit (this);
85 88
86 entry_group.StateChanged += OnEntryGroupStateChanged; 89 entry_group.StateChanged += OnEntryGroupStateChanged;
87 } finally { 90 } finally {
88 Monitor.Exit (this); 91 Monitor.Exit (this);
89 DBusManager.Bus.UntrapSignals (); 92 DBusManager.Bus.UntrapSignals ();
90 } 93 }
91 } 94 }
92 95
93 private void OnEntryGroupStateChanged (EntryGroupState state, string error) 96 private void OnEntryGroupStateChanged (EntryGroupState state, string error)
94 { 97 {
95 switch (state) { 98 switch (state) {
96 case EntryGroupState.Collision: 99 case EntryGroupState.Collision:
97 if (!OnResponse (ErrorCode.Collision)) { 100 if (!OnResponse (ErrorCode.Collision)) {
-   101 if (originalName == null)
98 throw new ApplicationException (); 102 originalName = Name;
-   103  
-   104 Name = originalName + " (" + retryNameModifier + ")";
-   105 retryNameModifier++;
-   106  
-   107 Console.WriteLine("ZeroConf had a name collision, trying: " + Name);
-   108  
-   109 Register();
99 } 110 }
100 break; 111 break;
101 case EntryGroupState.Failure: 112 case EntryGroupState.Failure:
102 if (!OnResponse (ErrorCode.Failure)) { 113 if (!OnResponse (ErrorCode.Failure)) {
103 throw new ApplicationException (); 114 Console.WriteLine("Mono.ZeroConf failed to register name with AvahiDBus");
104 } 115 }
105 break; 116 break;
106 case EntryGroupState.Established: 117 case EntryGroupState.Established:
107 OnResponse (ErrorCode.Ok); 118 OnResponse (ErrorCode.Ok);
108 break; 119 break;
109 } 120 }
110 } 121 }
111 122
112 protected virtual bool OnResponse (ErrorCode errorCode) 123 protected virtual bool OnResponse (ErrorCode errorCode)
113 { 124 {
114 RegisterServiceEventArgs args = new RegisterServiceEventArgs (); 125 RegisterServiceEventArgs args = new RegisterServiceEventArgs ();
115 126
116 args.Service = this; 127 args.Service = this;
117 args.IsRegistered = false; 128 args.IsRegistered = false;
118 args.ServiceError = AvahiUtils.ErrorCodeToServiceError (errorCode); 129 args.ServiceError = AvahiUtils.ErrorCodeToServiceError (errorCode);
119 130
120 if (errorCode == ErrorCode.Ok) { 131 if (errorCode == ErrorCode.Ok) {
121 args.IsRegistered = true; 132 args.IsRegistered = true;
122 } 133 }
123 134
124 RegisterServiceEventHandler handler = Response; 135 RegisterServiceEventHandler handler = Response;
125 if (handler != null) { 136 if (handler != null) {
126 handler (this, args); 137 handler (this, args);
127 return true; 138 return true;
128 } 139 }
129 140
130 return false; 141 return false;
131 } 142 }
132 143
133 public void Dispose () 144 public void Dispose ()
134 { 145 {
135 lock (this) { 146 lock (this) {
136 if (entry_group != null) { 147 if (entry_group != null) {
137 entry_group.Reset (); 148 entry_group.Reset ();
138 entry_group.Free (); 149 entry_group.Free ();
139 entry_group = null; 150 entry_group = null;
140 } 151 }
141 } 152 }
142 } 153 }
143 154
144 public short Port { 155 public short Port {
145 get { return (short)UPort; } 156 get { return (short)UPort; }
146 set { UPort = (ushort)value; } 157 set { UPort = (ushort)value; }
147 } 158 }
148   159  
149 public ushort UPort { 160 public ushort UPort {
150 get { return port; } 161 get { return port; }
151 set { port = value; } 162 set { port = value; }
152 } 163 }
153 } 164 }
154 } 165 }
155   166