Mono.Zeroconf – Blame information for rev 1

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;
41  
42 public RegisterService ()
43 {
44 }
45  
46 public RegisterService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol)
47 : base (name, regtype, replyDomain, @interface, aprotocol)
48 {
49 }
50  
51 public void Register ()
52 {
53 RegisterDBus ();
54  
55 byte [][] txt_record = TxtRecord == null
56 ? new byte[0][]
57 : Mono.Zeroconf.Providers.AvahiDBus.TxtRecord.Render (TxtRecord);
58  
59 entry_group.AddService (AvahiInterface, AvahiProtocol, PublishFlags.None,
60 Name ?? String.Empty, RegType ?? String.Empty, ReplyDomain ?? String.Empty,
61 String.Empty, port, txt_record);
62  
63 entry_group.Commit ();
64 }
65  
66 private void RegisterDBus ()
67 {
68 try {
69 Monitor.Enter (this);
70 DBusManager.Bus.TrapSignals ();
71  
72 if (entry_group != null) {
73 entry_group.Reset ();
74 return;
75 }
76  
77 if (DBusManager.Server.GetState () != AvahiServerState.Running) {
78 throw new ApplicationException ("Avahi Server is not in the Running state");
79 }
80  
81 ObjectPath path = DBusManager.Server.EntryGroupNew ();
82 entry_group = DBusManager.GetObject<IAvahiEntryGroup> (path);
83  
84 Monitor.Exit (this);
85  
86 entry_group.StateChanged += OnEntryGroupStateChanged;
87 } finally {
88 Monitor.Exit (this);
89 DBusManager.Bus.UntrapSignals ();
90 }
91 }
92  
93 private void OnEntryGroupStateChanged (EntryGroupState state, string error)
94 {
95 switch (state) {
96 case EntryGroupState.Collision:
97 if (!OnResponse (ErrorCode.Collision)) {
98 throw new ApplicationException ();
99 }
100 break;
101 case EntryGroupState.Failure:
102 if (!OnResponse (ErrorCode.Failure)) {
103 throw new ApplicationException ();
104 }
105 break;
106 case EntryGroupState.Established:
107 OnResponse (ErrorCode.Ok);
108 break;
109 }
110 }
111  
112 protected virtual bool OnResponse (ErrorCode errorCode)
113 {
114 RegisterServiceEventArgs args = new RegisterServiceEventArgs ();
115  
116 args.Service = this;
117 args.IsRegistered = false;
118 args.ServiceError = AvahiUtils.ErrorCodeToServiceError (errorCode);
119  
120 if (errorCode == ErrorCode.Ok) {
121 args.IsRegistered = true;
122 }
123  
124 RegisterServiceEventHandler handler = Response;
125 if (handler != null) {
126 handler (this, args);
127 return true;
128 }
129  
130 return false;
131 }
132  
133 public void Dispose ()
134 {
135 lock (this) {
136 if (entry_group != null) {
137 entry_group.Reset ();
138 entry_group.Free ();
139 entry_group = null;
140 }
141 }
142 }
143  
144 public short Port {
145 get { return (short)UPort; }
146 set { UPort = (ushort)value; }
147 }
148  
149 public ushort UPort {
150 get { return port; }
151 set { port = value; }
152 }
153 }
154 }