2 // CustomPeerResolverService.cs
5 // Marcos Cobena (marcoscobena@gmail.com)
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
10 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
35 using System.Net.Sockets;
36 using System.Transactions;
39 namespace System.ServiceModel.PeerResolvers
41 [MonoTODO ("Implement cleanup and refresh")]
42 [ServiceBehavior (ConcurrencyMode = ConcurrencyMode.Multiple,
43 InstanceContextMode = InstanceContextMode.Single,
44 UseSynchronizationContext = false)]
45 public class CustomPeerResolverService : IPeerResolverContract
47 static ServiceHost localhost;
49 static void SetupCustomPeerResolverServiceHost ()
51 // launch peer resolver service locally only when it does not seem to be running ...
52 var t = new TcpListener (8931);
59 Console.WriteLine ("WARNING: it is running peer resolver service locally. This means, the node registration is valid only within this application domain...");
60 var host = new ServiceHost (new LocalPeerResolverService (TextWriter.Null));
61 host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
62 host.AddServiceEndpoint (typeof (ICustomPeerResolverContract), new BasicHttpBinding (), "http://localhost:8931");
67 ICustomPeerResolverClient client;
68 bool control_shape, opened;
69 TimeSpan refresh_interval, cleanup_interval;
71 public CustomPeerResolverService ()
73 client = ChannelFactory<ICustomPeerResolverClient>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ("http://localhost:8931"));
75 refresh_interval = new TimeSpan (0, 10, 0);
76 cleanup_interval = new TimeSpan (0, 1, 0);
79 public TimeSpan CleanupInterval {
80 get { return cleanup_interval; }
82 if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
83 throw new ArgumentOutOfRangeException (
84 "The interval is either zero or greater than max value.");
86 throw new InvalidOperationException ("The interval must be set before it is opened");
88 cleanup_interval = value;
92 public bool ControlShape {
93 get { return control_shape; }
96 throw new InvalidOperationException ("The interval must be set before it is opened");
97 control_shape = value;
101 public TimeSpan RefreshInterval {
102 get { return refresh_interval; }
104 if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
105 throw new ArgumentOutOfRangeException (
106 "The interval is either zero or greater than max value.");
108 throw new InvalidOperationException ("The interval must be set before it is opened");
110 refresh_interval = value;
114 [MonoTODO ("Do we have to unregister nodes here?")]
115 public virtual void Close ()
118 throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
122 if (localhost != null) {
128 public virtual ServiceSettingsResponseInfo GetServiceSettings ()
131 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
133 return client.GetServiceSettings ();
136 public virtual void Open ()
138 if (localhost == null)
139 SetupCustomPeerResolverServiceHost ();
141 if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
142 throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
145 throw new InvalidOperationException ("The service has been started by a previous call to this method.");
150 client.SetCustomServiceSettings (new PeerServiceSettingsInfo () { ControlMeshShape = control_shape, RefreshInterval = refresh_interval, CleanupInterval = cleanup_interval });
153 public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
155 if (refreshInfo == null)
156 throw new ArgumentException ("Refresh info cannot be null.");
159 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
161 return client.Refresh (refreshInfo);
164 public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
166 if (registerInfo == null)
167 throw new ArgumentException ("Register info cannot be null.");
170 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
172 return client.Register (registerInfo);
175 public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
177 return Register (new RegisterInfo (clientId, meshId, address));
180 public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
182 if (resolveInfo == null)
183 throw new ArgumentException ("Resolve info cannot be null.");
186 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
188 return client.Resolve (resolveInfo);
191 public virtual void Unregister (UnregisterInfo unregisterInfo)
193 if (unregisterInfo == null)
194 throw new ArgumentException ("Unregister info cannot be null.");
197 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
199 client.Unregister (unregisterInfo);
202 public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
204 if (updateInfo == null)
205 throw new ArgumentException ("Update info cannot be null.");
208 throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
210 return client.Update (updateInfo);