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
|
//
// System.Runtime.Remoting.Identity.cs
//
// Author: Lluis Sanchez Gual (lluis@ideary.com)
//
// (C) 2003, Lluis Sanchez Gual
//
//
// Copyright (C) 2004 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;
using System.Threading;
using System.Collections;
namespace System.Runtime.Remoting.Lifetime
{
internal class Lease : MarshalByRefObject, ILease
{
DateTime _leaseExpireTime;
LeaseState _currentState;
TimeSpan _initialLeaseTime;
TimeSpan _renewOnCallTime;
TimeSpan _sponsorshipTimeout;
ArrayList _sponsors;
Queue _renewingSponsors;
RenewalDelegate _renewalDelegate;
delegate TimeSpan RenewalDelegate(ILease lease);
public Lease()
{
_currentState = LeaseState.Initial;
_initialLeaseTime = LifetimeServices.LeaseTime;
_renewOnCallTime = LifetimeServices.RenewOnCallTime;
_sponsorshipTimeout = LifetimeServices.SponsorshipTimeout;
_leaseExpireTime = DateTime.UtcNow + _initialLeaseTime;
}
public TimeSpan CurrentLeaseTime
{
get { return _leaseExpireTime - DateTime.UtcNow; }
}
public LeaseState CurrentState
{
get { return _currentState; }
}
public void Activate()
{
// Called when then Lease is registered in the LeaseManager
_currentState = LeaseState.Active;
}
public TimeSpan InitialLeaseTime
{
get { return _initialLeaseTime; }
set
{
if (_currentState != LeaseState.Initial)
throw new RemotingException ("InitialLeaseTime property can only be set when the lease is in initial state; state is " + _currentState + ".");
_initialLeaseTime = value;
_leaseExpireTime = DateTime.UtcNow + _initialLeaseTime;
if (value == TimeSpan.Zero) _currentState = LeaseState.Null;
}
}
public TimeSpan RenewOnCallTime
{
get { return _renewOnCallTime; }
set
{
if (_currentState != LeaseState.Initial)
throw new RemotingException ("RenewOnCallTime property can only be set when the lease is in initial state; state is " + _currentState + ".");
_renewOnCallTime = value;
}
}
public TimeSpan SponsorshipTimeout
{
get { return _sponsorshipTimeout; }
set
{
if (_currentState != LeaseState.Initial)
throw new RemotingException ("SponsorshipTimeout property can only be set when the lease is in initial state; state is " + _currentState + ".");
_sponsorshipTimeout = value;
}
}
public void Register (ISponsor obj)
{
Register (obj, TimeSpan.Zero);
}
public void Register (ISponsor obj, TimeSpan renewalTime)
{
lock (this) {
if (_sponsors == null)
_sponsors = new ArrayList();
_sponsors.Add (obj);
}
if (renewalTime != TimeSpan.Zero)
Renew (renewalTime);
}
public TimeSpan Renew (TimeSpan renewalTime)
{
DateTime newTime = DateTime.UtcNow + renewalTime;
if (newTime > _leaseExpireTime) _leaseExpireTime = newTime;
return CurrentLeaseTime;
}
public void Unregister (ISponsor obj)
{
lock (this) {
if (_sponsors == null) return;
// Don't use ArrayList.Remove() here because it will end calling Equals, which may
// crash if the sponsor is not available anymore
for (int n=0; n < _sponsors.Count; n++) {
if (object.ReferenceEquals (_sponsors [n], obj)) {
_sponsors.RemoveAt (n);
break;
}
}
}
}
internal void UpdateState ()
{
// Called by the lease manager to update the state of this lease,
// basically for knowing if it has expired
if (_currentState != LeaseState.Active) return;
if (CurrentLeaseTime > TimeSpan.Zero) return;
// Expired. Try to renew using sponsors.
if (_sponsors != null)
{
_currentState = LeaseState.Renewing;
lock (this) {
_renewingSponsors = new Queue (_sponsors);
}
CheckNextSponsor ();
}
else
_currentState = LeaseState.Expired;
}
void CheckNextSponsor ()
{
if (_renewingSponsors.Count == 0) {
_currentState = LeaseState.Expired;
_renewingSponsors = null;
return;
}
ISponsor nextSponsor = (ISponsor) _renewingSponsors.Peek();
_renewalDelegate = new RenewalDelegate (nextSponsor.Renewal);
IAsyncResult ar = _renewalDelegate.BeginInvoke (this, null, null);
ThreadPool.RegisterWaitForSingleObject (ar.AsyncWaitHandle, new WaitOrTimerCallback (ProcessSponsorResponse), ar, _sponsorshipTimeout, true);
}
void ProcessSponsorResponse (object state, bool timedOut)
{
if (!timedOut)
{
try
{
IAsyncResult ar = (IAsyncResult)state;
TimeSpan newSpan = _renewalDelegate.EndInvoke (ar);
if (newSpan != TimeSpan.Zero)
{
Renew (newSpan);
_currentState = LeaseState.Active;
_renewingSponsors = null;
return;
}
}
catch { }
}
// Sponsor failed, timed out, or returned TimeSpan.Zero
Unregister ((ISponsor) _renewingSponsors.Dequeue()); // Drop the sponsor
CheckNextSponsor ();
}
}
}
|