2 // System.Runtime.Remoting.Identity.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // (C) 2003, Lluis Sanchez Gual
10 // Copyright (C) 2004 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.
33 using System
.Threading
;
34 using System
.Collections
;
36 namespace System
.Runtime
.Remoting
.Lifetime
38 internal class Lease
: MarshalByRefObject
, ILease
40 DateTime _leaseExpireTime
;
41 LeaseState _currentState
;
42 TimeSpan _initialLeaseTime
;
43 TimeSpan _renewOnCallTime
;
44 TimeSpan _sponsorshipTimeout
;
46 Queue _renewingSponsors
;
47 RenewalDelegate _renewalDelegate
;
49 delegate TimeSpan
RenewalDelegate(ILease lease
);
53 _currentState
= LeaseState
.Initial
;
54 _initialLeaseTime
= LifetimeServices
.LeaseTime
;
55 _renewOnCallTime
= LifetimeServices
.RenewOnCallTime
;
56 _sponsorshipTimeout
= LifetimeServices
.SponsorshipTimeout
;
57 _leaseExpireTime
= DateTime
.Now
+ _initialLeaseTime
;
60 public TimeSpan CurrentLeaseTime
62 get { return _leaseExpireTime - DateTime.Now; }
65 public LeaseState CurrentState
67 get { return _currentState; }
70 public void Activate()
72 // Called when then Lease is registered in the LeaseManager
73 _currentState
= LeaseState
.Active
;
76 public TimeSpan InitialLeaseTime
78 get { return _initialLeaseTime; }
81 if (_currentState
!= LeaseState
.Initial
)
82 throw new RemotingException ("InitialLeaseTime property can only be set when the lease is in initial state; state is " + _currentState
+ ".");
84 _initialLeaseTime
= value;
85 _leaseExpireTime
= DateTime
.Now
+ _initialLeaseTime
;
86 if (value == TimeSpan
.Zero
) _currentState
= LeaseState
.Null
;
90 public TimeSpan RenewOnCallTime
92 get { return _renewOnCallTime; }
95 if (_currentState
!= LeaseState
.Initial
)
96 throw new RemotingException ("RenewOnCallTime property can only be set when the lease is in initial state; state is " + _currentState
+ ".");
98 _renewOnCallTime
= value;
102 public TimeSpan SponsorshipTimeout
104 get { return _sponsorshipTimeout; }
107 if (_currentState
!= LeaseState
.Initial
)
108 throw new RemotingException ("SponsorshipTimeout property can only be set when the lease is in initial state; state is " + _currentState
+ ".");
110 _sponsorshipTimeout
= value;
114 public void Register (ISponsor obj
)
116 Register (obj
, TimeSpan
.Zero
);
119 public void Register (ISponsor obj
, TimeSpan renewalTime
)
122 if (_sponsors
== null)
123 _sponsors
= new ArrayList();
127 if (renewalTime
!= TimeSpan
.Zero
)
131 public TimeSpan
Renew (TimeSpan renewalTime
)
133 DateTime newTime
= DateTime
.Now
+ renewalTime
;
134 if (newTime
> _leaseExpireTime
) _leaseExpireTime
= newTime
;
135 return CurrentLeaseTime
;
138 public void Unregister (ISponsor obj
)
141 if (_sponsors
== null) return;
143 // Don't use ArrayList.Remove() here because it will end calling Equals, which may
144 // crash if the sponsor is not available anymore
145 for (int n
=0; n
< _sponsors
.Count
; n
++) {
146 if (object.ReferenceEquals (_sponsors
[n
], obj
)) {
147 _sponsors
.RemoveAt (n
);
154 internal void UpdateState ()
156 // Called by the lease manager to update the state of this lease,
157 // basically for knowing if it has expired
159 if (_currentState
!= LeaseState
.Active
) return;
160 if (CurrentLeaseTime
> TimeSpan
.Zero
) return;
162 // Expired. Try to renew using sponsors.
164 if (_sponsors
!= null)
166 _currentState
= LeaseState
.Renewing
;
168 _renewingSponsors
= new Queue (_sponsors
);
173 _currentState
= LeaseState
.Expired
;
176 void CheckNextSponsor ()
178 if (_renewingSponsors
.Count
== 0) {
179 _currentState
= LeaseState
.Expired
;
180 _renewingSponsors
= null;
184 ISponsor nextSponsor
= (ISponsor
) _renewingSponsors
.Peek();
185 _renewalDelegate
= new RenewalDelegate (nextSponsor
.Renewal
);
186 IAsyncResult ar
= _renewalDelegate
.BeginInvoke (this, null, null);
187 ThreadPool
.RegisterWaitForSingleObject (ar
.AsyncWaitHandle
, new WaitOrTimerCallback (ProcessSponsorResponse
), ar
, _sponsorshipTimeout
, true);
190 void ProcessSponsorResponse (object state
, bool timedOut
)
196 IAsyncResult ar
= (IAsyncResult
)state
;
197 TimeSpan newSpan
= _renewalDelegate
.EndInvoke (ar
);
198 if (newSpan
!= TimeSpan
.Zero
)
201 _currentState
= LeaseState
.Active
;
202 _renewingSponsors
= null;
209 // Sponsor failed, timed out, or returned TimeSpan.Zero
211 Unregister ((ISponsor
) _renewingSponsors
.Dequeue()); // Drop the sponsor