1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
//
// CustomPeerResolverService.cs
//
// Author:
// Marcos Cobena (marcoscobena@gmail.com)
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Transactions;
using System.Timers;
namespace System.ServiceModel.PeerResolvers
{
[MonoTODO ("Implement cleanup and refresh")]
[ServiceBehavior (ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single,
UseSynchronizationContext = false)]
public class CustomPeerResolverService : IPeerResolverContract
{
static ServiceHost localhost;
static int port;
static void SetupCustomPeerResolverServiceHost ()
{
string customPort = Environment.GetEnvironmentVariable ("MONO_CUSTOMPEERRESOLVERSERVICE_PORT");
if (customPort == null || !int.TryParse (customPort, out port))
port = 8931;
// launch peer resolver service locally only when it does not seem to be running ...
var t = new TcpListener (port);
try {
t.Start ();
t.Stop ();
} catch {
return;
}
Console.WriteLine ("WARNING: it is running peer resolver service locally. This means, the node registration is valid only within this application domain...");
var host = new ServiceHost (new LocalPeerResolverService (TextWriter.Null));
host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
host.AddServiceEndpoint (typeof (ICustomPeerResolverContract), new BasicHttpBinding (), $"http://localhost:{port}");
localhost = host;
host.Open ();
}
ICustomPeerResolverClient client;
bool control_shape, opened;
TimeSpan refresh_interval, cleanup_interval;
public CustomPeerResolverService ()
{
client = ChannelFactory<ICustomPeerResolverClient>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ($"http://localhost:{port}"));
refresh_interval = new TimeSpan (0, 10, 0);
cleanup_interval = new TimeSpan (0, 1, 0);
}
public TimeSpan CleanupInterval {
get { return cleanup_interval; }
set {
if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
throw new ArgumentOutOfRangeException (
"The interval is either zero or greater than max value.");
if (opened)
throw new InvalidOperationException ("The interval must be set before it is opened");
cleanup_interval = value;
}
}
public bool ControlShape {
get { return control_shape; }
set {
if (opened)
throw new InvalidOperationException ("The interval must be set before it is opened");
control_shape = value;
}
}
public TimeSpan RefreshInterval {
get { return refresh_interval; }
set {
if ((value < TimeSpan.Zero) || (value > TimeSpan.MaxValue))
throw new ArgumentOutOfRangeException (
"The interval is either zero or greater than max value.");
if (opened)
throw new InvalidOperationException ("The interval must be set before it is opened");
refresh_interval = value;
}
}
[MonoTODO ("Do we have to unregister nodes here?")]
public virtual void Close ()
{
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed by a previous call to this method.");
client.Close ();
opened = false;
if (localhost != null) {
localhost.Close ();
localhost = null;
}
}
public virtual ServiceSettingsResponseInfo GetServiceSettings ()
{
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
return client.GetServiceSettings ();
}
public virtual void Open ()
{
if (localhost == null)
SetupCustomPeerResolverServiceHost ();
if ((CleanupInterval == TimeSpan.Zero) || (RefreshInterval == TimeSpan.Zero))
throw new ArgumentException ("Cleanup interval or refresh interval are set to a time span interval of zero.");
if (opened)
throw new InvalidOperationException ("The service has been started by a previous call to this method.");
opened = true;
client.Open ();
client.SetCustomServiceSettings (new PeerServiceSettingsInfo () { ControlMeshShape = control_shape, RefreshInterval = refresh_interval, CleanupInterval = cleanup_interval });
}
public virtual RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
{
if (refreshInfo == null)
throw new ArgumentException ("Refresh info cannot be null.");
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
return client.Refresh (refreshInfo);
}
public virtual RegisterResponseInfo Register (RegisterInfo registerInfo)
{
if (registerInfo == null)
throw new ArgumentException ("Register info cannot be null.");
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
return client.Register (registerInfo);
}
public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address)
{
return Register (new RegisterInfo (clientId, meshId, address));
}
public virtual ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
{
if (resolveInfo == null)
throw new ArgumentException ("Resolve info cannot be null.");
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
return client.Resolve (resolveInfo);
}
public virtual void Unregister (UnregisterInfo unregisterInfo)
{
if (unregisterInfo == null)
throw new ArgumentException ("Unregister info cannot be null.");
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
client.Unregister (unregisterInfo);
}
public virtual RegisterResponseInfo Update (UpdateInfo updateInfo)
{
if (updateInfo == null)
throw new ArgumentException ("Update info cannot be null.");
if (! opened)
throw new InvalidOperationException ("The service has never been opened or it was closed previously.");
return client.Update (updateInfo);
}
}
}
|