New test.
[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                                 _sponsors.Remove (obj);
143                         }
144                 }
145
146                 internal void UpdateState ()
147                 {
148                         // Called by the lease manager to update the state of this lease,
149                         // basically for knowing if it has expired
150
151                         if (_currentState != LeaseState.Active) return;
152                         if (CurrentLeaseTime > TimeSpan.Zero) return;
153
154                         // Expired. Try to renew using sponsors.
155
156                         if (_sponsors != null)
157                         {
158                                 _currentState = LeaseState.Renewing;
159                                 lock (this) {
160                                         _renewingSponsors = new Queue (_sponsors);
161                                 }
162                                 CheckNextSponsor ();
163                         }
164                         else
165                                 _currentState = LeaseState.Expired;
166                 }
167
168                 void CheckNextSponsor ()
169                 {
170                         if (_renewingSponsors.Count == 0) {
171                                 _currentState = LeaseState.Expired;
172                                 _renewingSponsors = null;
173                                 return;
174                         }
175
176                         ISponsor nextSponsor = (ISponsor) _renewingSponsors.Peek();
177                         _renewalDelegate = new RenewalDelegate (nextSponsor.Renewal);
178                         IAsyncResult ar = _renewalDelegate.BeginInvoke (this, null, null);
179                         ThreadPool.RegisterWaitForSingleObject (ar.AsyncWaitHandle, new WaitOrTimerCallback (ProcessSponsorResponse), ar, _sponsorshipTimeout, true);
180                 }
181
182                 void ProcessSponsorResponse (object state, bool timedOut)
183                 {
184                         if (!timedOut)
185                         {
186                                 try
187                                 {
188                                         IAsyncResult ar = (IAsyncResult)state;
189                                         TimeSpan newSpan = _renewalDelegate.EndInvoke (ar);
190                                         if (newSpan != TimeSpan.Zero)
191                                         {
192                                                 Renew (newSpan);
193                                                 _currentState = LeaseState.Active;
194                                                 _renewingSponsors = null;
195                                                 return;
196                                         }
197                                 }
198                                 catch { }
199                         }
200
201                         // Sponsor failed, timed out, or returned TimeSpan.Zero
202
203                         Unregister ((ISponsor) _renewingSponsors.Dequeue());    // Drop the sponsor
204                         CheckNextSponsor ();
205                 }
206         }\r
207 }\r