2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.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                         lock (this) {
122                                 if (_sponsors == null)
123                                         _sponsors = new ArrayList();
124                                 _sponsors.Add (obj);
125                         }
126
127                         if (renewalTime != TimeSpan.Zero)
128                                 Renew (renewalTime);
129                 }
130
131                 public TimeSpan Renew (TimeSpan renewalTime)
132                 {
133                         DateTime newTime = DateTime.Now + renewalTime;
134                         if (newTime > _leaseExpireTime) _leaseExpireTime = newTime;
135                         return CurrentLeaseTime;
136                 }
137
138                 public void Unregister (ISponsor obj)
139                 {
140                         lock (this) {
141                                 if (_sponsors == null) return;
142                                 
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);
148                                                 break;
149                                         }
150                                 }
151                         }
152                 }
153
154                 internal void UpdateState ()
155                 {
156                         // Called by the lease manager to update the state of this lease,
157                         // basically for knowing if it has expired
158
159                         if (_currentState != LeaseState.Active) return;
160                         if (CurrentLeaseTime > TimeSpan.Zero) return;
161
162                         // Expired. Try to renew using sponsors.
163
164                         if (_sponsors != null)
165                         {
166                                 _currentState = LeaseState.Renewing;
167                                 lock (this) {
168                                         _renewingSponsors = new Queue (_sponsors);
169                                 }
170                                 CheckNextSponsor ();
171                         }
172                         else
173                                 _currentState = LeaseState.Expired;
174                 }
175
176                 void CheckNextSponsor ()
177                 {
178                         if (_renewingSponsors.Count == 0) {
179                                 _currentState = LeaseState.Expired;
180                                 _renewingSponsors = null;
181                                 return;
182                         }
183
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);
188                 }
189
190                 void ProcessSponsorResponse (object state, bool timedOut)
191                 {
192                         if (!timedOut)
193                         {
194                                 try
195                                 {
196                                         IAsyncResult ar = (IAsyncResult)state;
197                                         TimeSpan newSpan = _renewalDelegate.EndInvoke (ar);
198                                         if (newSpan != TimeSpan.Zero)
199                                         {
200                                                 Renew (newSpan);
201                                                 _currentState = LeaseState.Active;
202                                                 _renewingSponsors = null;
203                                                 return;
204                                         }
205                                 }
206                                 catch { }
207                         }
208
209                         // Sponsor failed, timed out, or returned TimeSpan.Zero
210
211                         Unregister ((ISponsor) _renewingSponsors.Dequeue());    // Drop the sponsor
212                         CheckNextSponsor ();
213                 }
214         }\r
215 }\r