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
)
121 if (_sponsors
== null)
124 if (_sponsors
== null)
125 _sponsors
= new ArrayList();
129 lock (_sponsors
.SyncRoot
) {
133 if (renewalTime
!= TimeSpan
.Zero
)
137 public TimeSpan
Renew (TimeSpan renewalTime
)
139 DateTime newTime
= DateTime
.Now
+ renewalTime
;
140 if (newTime
> _leaseExpireTime
) _leaseExpireTime
= newTime
;
141 return CurrentLeaseTime
;
144 public void Unregister (ISponsor obj
)
146 if (_sponsors
== null) return;
148 lock (_sponsors
.SyncRoot
) {
149 _sponsors
.Remove (obj
);
153 internal void UpdateState ()
155 // Called by the lease manager to update the state of this lease,
156 // basically for knowing if it has expired
158 if (_currentState
!= LeaseState
.Active
) return;
159 if (CurrentLeaseTime
> TimeSpan
.Zero
) return;
161 // Expired. Try to renew using sponsors.
163 if (_sponsors
!= null)
165 _currentState
= LeaseState
.Renewing
;
166 lock (_sponsors
.SyncRoot
) {
167 _renewingSponsors
= new Queue (_sponsors
);
172 _currentState
= LeaseState
.Expired
;
175 void CheckNextSponsor ()
177 if (_renewingSponsors
.Count
== 0) {
178 _currentState
= LeaseState
.Expired
;
179 _renewingSponsors
= null;
183 ISponsor nextSponsor
= (ISponsor
) _renewingSponsors
.Peek();
184 _renewalDelegate
= new RenewalDelegate (nextSponsor
.Renewal
);
185 IAsyncResult ar
= _renewalDelegate
.BeginInvoke (this, null, null);
186 ThreadPool
.RegisterWaitForSingleObject (ar
.AsyncWaitHandle
, new WaitOrTimerCallback (ProcessSponsorResponse
), ar
, _sponsorshipTimeout
, true);
189 void ProcessSponsorResponse (object state
, bool timedOut
)
195 IAsyncResult ar
= (IAsyncResult
)state
;
196 TimeSpan newSpan
= _renewalDelegate
.EndInvoke (ar
);
197 if (newSpan
!= TimeSpan
.Zero
)
200 _currentState
= LeaseState
.Active
;
201 _renewingSponsors
= null;
208 // Sponsor failed, timed out, or returned TimeSpan.Zero
210 Unregister ((ISponsor
) _renewingSponsors
.Dequeue()); // Drop the sponsor