* AppDomainSetup.cs: If relative paths are used they should be
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Lifetime / LeaseManager.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 using System;
10 using System.Threading;
11 using System.Collections;
12 using System.Runtime.Remoting;
13
14 namespace System.Runtime.Remoting.Lifetime
15 {
16         internal class LeaseManager
17         {
18                 ArrayList _objects = new ArrayList();
19                 Timer _timer = null;
20
21                 public void SetPollTime (TimeSpan timeSpan)
22                 {
23                         lock (_objects.SyncRoot) 
24                         {
25                                 if (_timer != null)
26                                         _timer.Change (timeSpan,timeSpan);
27                         }
28                 }
29
30                 public void TrackLifetime (ServerIdentity identity)
31                 {
32                         lock (_objects.SyncRoot)
33                         {
34                                 identity.Lease.Activate();
35                                 _objects.Add (identity);
36
37                                 if (_timer == null) StartManager();
38                         }
39                 }
40
41                 public void StopTrackingLifetime (ServerIdentity identity)
42                 {
43                         lock (_objects.SyncRoot)
44                         {
45                                 _objects.Remove (identity);
46                         }
47                 }
48
49                 public void StartManager()
50                 {
51                         _timer = new Timer (new TimerCallback (ManageLeases), null, LifetimeServices.LeaseManagerPollTime,LifetimeServices.LeaseManagerPollTime);
52                 }
53
54                 public void StopManager()
55                 {
56                         _timer.Dispose();
57                         _timer = null;
58                 }
59
60                 public void ManageLeases(object state)
61                 {
62                         lock (_objects.SyncRoot)
63                         {
64                                 int n=0;
65                                 while (n < _objects.Count)
66                                 {
67                                         ServerIdentity ident = (ServerIdentity)_objects[n];
68                                         ident.Lease.UpdateState();
69                                         if (ident.Lease.CurrentState == LeaseState.Expired)
70                                         {
71                                                 _objects.RemoveAt (n);
72                                                 ident.OnLifetimeExpired ();
73                                         }
74                                         else
75                                                 n++;
76                                 }
77
78                                 if (_objects.Count == 0) 
79                                         StopManager();
80                         }
81                 }
82         }
83 }