Mono.Zeroconf – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // RegisterService.cs
3 //
4 // Authors:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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 AV=Avahi;
31  
32 namespace Mono.Zeroconf.Providers.Avahi
33 {
34 public class RegisterService : IRegisterService
35 {
36 private string name;
37 private string regtype;
38 private string reply_domain;
39 private short port;
40 private ITxtRecord txt_record;
41  
42 private AV.Client client;
43 private AV.EntryGroup entry_group;
44 private object eg_mutex = new object();
45  
46 public event RegisterServiceEventHandler Response;
47  
48 public RegisterService()
49 {
50 client = new AV.Client();
51 }
52  
53 public void Register()
54 {
55 lock(eg_mutex) {
56 if(entry_group != null) {
57 entry_group.Reset();
58 } else {
59 entry_group = new AV.EntryGroup(client);
60 entry_group.StateChanged += OnEntryGroupStateChanged;
61 }
62  
63 try {
64 string [] rendered_txt_record = null;
65  
66 if(txt_record != null && txt_record.Count > 0) {
67 rendered_txt_record = new string[txt_record.Count];
68 for(int i = 0; i < txt_record.Count; i++) {
69 TxtRecordItem item = txt_record.GetItemAt(i);
70 rendered_txt_record[i] = String.Format("{0}={1}",
71 item.Key, item.ValueString);
72 }
73 }
74  
75 entry_group.AddService(name, regtype, reply_domain, (ushort)port, rendered_txt_record);
76 entry_group.Commit();
77 } catch(AV.ClientException e) {
78 if(e.ErrorCode == AV.ErrorCode.Collision && OnResponse(e.ErrorCode)) {
79 return;
80 } else {
81 throw e;
82 }
83 }
84 }
85 }
86  
87 public void Dispose()
88 {
89 lock(eg_mutex) {
90 if(entry_group == null) {
91 return;
92 }
93  
94 entry_group.Reset();
95 entry_group.Dispose();
96 entry_group = null;
97 }
98 }
99  
100 private void OnEntryGroupStateChanged(object o, AV.EntryGroupStateArgs args)
101 {
102 switch(args.State) {
103 case AV.EntryGroupState.Collision:
104 if(!OnResponse(AV.ErrorCode.Collision)) {
105 throw new ApplicationException();
106 }
107 break;
108 case AV.EntryGroupState.Failure:
109 if(!OnResponse(AV.ErrorCode.Failure)) {
110 throw new ApplicationException();
111 }
112 break;
113 case AV.EntryGroupState.Established:
114 OnResponse(AV.ErrorCode.Ok);
115 break;
116 }
117 }
118  
119 protected virtual bool OnResponse(AV.ErrorCode errorCode)
120 {
121 RegisterServiceEventArgs args = new RegisterServiceEventArgs();
122  
123 args.Service = this;
124 args.IsRegistered = false;
125 args.ServiceError = AvahiUtils.ErrorCodeToServiceError(errorCode);
126  
127 if(errorCode == AV.ErrorCode.Ok) {
128 args.IsRegistered = true;
129 }
130  
131 RegisterServiceEventHandler handler = Response;
132 if(handler != null) {
133 handler(this, args);
134 return true;
135 } else {
136 return false;
137 }
138 }
139  
140 public string Name {
141 get { return name; }
142 set { name = value; }
143 }
144  
145 public string RegType {
146 get { return regtype; }
147 set { regtype = value; }
148 }
149  
150 public string ReplyDomain {
151 get { return reply_domain; }
152 set { reply_domain = value; }
153 }
154  
155 public short Port {
156 get { return port; }
157 set { port = value; }
158 }
159  
160 public ITxtRecord TxtRecord {
161 get { return txt_record; }
162 set { txt_record = value; }
163 }
164 }
165 }