2005-08-11 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Lifetime / Lease.cs
1 //
2 // System.Runtime.Remoting.Identity.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2003, Lluis Sanchez Gual
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 \r
32 using System;\r
33 using System.Threading;\r
34 using System.Collections;\r
35 \r
36 namespace System.Runtime.Remoting.Lifetime\r
37 {\r
38         internal class Lease : MarshalByRefObject, ILease\r
39         {\r
40                 DateTime _leaseExpireTime;
41                 LeaseState _currentState;
42                 TimeSpan _initialLeaseTime;
43                 TimeSpan _renewOnCallTime;
44                 TimeSpan _sponsorshipTimeout;
45                 ArrayList _sponsors;
46                 Queue _renewingSponsors;
47                 RenewalDelegate _renewalDelegate;
48
49                 delegate TimeSpan RenewalDelegate(ILease lease);
50 \r
51                 public Lease()\r
52                 {\r
53                         _currentState = LeaseState.Initial;\r
54                         _initialLeaseTime = LifetimeServices.LeaseTime;\r
55                         _renewOnCallTime = LifetimeServices.RenewOnCallTime;\r
56                         _sponsorshipTimeout = LifetimeServices.SponsorshipTimeout;\r
57                         _leaseExpireTime = DateTime.Now + _initialLeaseTime;\r
58                 }\r
59 \r
60                 public TimeSpan CurrentLeaseTime 
61                 { 
62                         get { return _leaseExpireTime - DateTime.Now; }
63                 }
64
65                 public LeaseState CurrentState 
66                 { 
67                         get { return _currentState; }
68                 }
69
70                 public void Activate()
71                 {
72                         // Called when then Lease is registered in the LeaseManager
73                         _currentState = LeaseState.Active;
74                 }
75
76                 public TimeSpan InitialLeaseTime 
77                 { 
78                         get { return _initialLeaseTime; }
79                         set 
80                         { 
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 + ".");
83
84                                 _initialLeaseTime = value; 
85                                 _leaseExpireTime = DateTime.Now + _initialLeaseTime;\r
86                                 if (value == TimeSpan.Zero) _currentState = LeaseState.Null;
87                         }
88                 }
89
90                 public TimeSpan RenewOnCallTime 
91                 { 
92                         get { return _renewOnCallTime; }
93                         set 
94                         { 
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 + ".");
97
98                                 _renewOnCallTime = value; 
99                         }
100                 }
101
102                 public TimeSpan SponsorshipTimeout 
103                 {
104                         get { return _sponsorshipTimeout; }
105                         set 
106                         { 
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 + ".");
109
110                                 _sponsorshipTimeout = value; 
111                         }
112                 }
113
114                 public void Register (ISponsor obj)
115                 {
116                         Register (obj, TimeSpan.Zero);
117                 }
118
119                 public void Register (ISponsor obj, TimeSpan renewalTime)
120                 {
121                         if (_sponsors == null) \r
122                         {
123                                 lock (this) {
124                                         if (_sponsors == null)
125                                                 _sponsors = new ArrayList();
126                                 }
127                         }
128
129                         lock (_sponsors.SyncRoot) {
130                                 _sponsors.Add (obj);
131                         }
132
133                         if (renewalTime != TimeSpan.Zero)
134                                 Renew (renewalTime);
135                 }
136
137                 public TimeSpan Renew (TimeSpan renewalTime)
138                 {
139                         DateTime newTime = DateTime.Now + renewalTime;
140                         if (newTime > _leaseExpireTime) _leaseExpireTime = newTime;
141                         return CurrentLeaseTime;
142                 }
143
144                 public void Unregister (ISponsor obj)
145                 {
146                         if (_sponsors == null) return;
147                         
148                         lock (_sponsors.SyncRoot) {
149                                 _sponsors.Remove (obj);
150                         }
151                 }
152
153                 internal void UpdateState ()
154                 {
155                         // Called by the lease manager to update the state of this lease,
156                         // basically for knowing if it has expired
157
158                         if (_currentState != LeaseState.Active) return;
159                         if (CurrentLeaseTime > TimeSpan.Zero) return;
160
161                         // Expired. Try to renew using sponsors.
162
163                         if (_sponsors != null)
164                         {
165                                 _currentState = LeaseState.Renewing;
166                                 lock (_sponsors.SyncRoot) {
167                                         _renewingSponsors = new Queue (_sponsors);
168                                 }
169                                 CheckNextSponsor ();
170                         }
171                         else
172                                 _currentState = LeaseState.Expired;
173                 }
174
175                 void CheckNextSponsor ()
176                 {
177                         if (_renewingSponsors.Count == 0) {
178                                 _currentState = LeaseState.Expired;
179                                 _renewingSponsors = null;
180                                 return;
181                         }
182
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);
187                 }
188
189                 void ProcessSponsorResponse (object state, bool timedOut)
190                 {
191                         if (!timedOut)
192                         {
193                                 try
194                                 {
195                                         IAsyncResult ar = (IAsyncResult)state;
196                                         TimeSpan newSpan = _renewalDelegate.EndInvoke (ar);
197                                         if (newSpan != TimeSpan.Zero)
198                                         {
199                                                 Renew (newSpan);
200                                                 _currentState = LeaseState.Active;
201                                                 _renewingSponsors = null;
202                                                 return;
203                                         }
204                                 }
205                                 catch { }
206                         }
207
208                         // Sponsor failed, timed out, or returned TimeSpan.Zero
209
210                         Unregister ((ISponsor) _renewingSponsors.Dequeue());    // Drop the sponsor
211                         CheckNextSponsor ();
212                 }
213         }\r
214 }\r