programming4us
programming4us
ENTERPRISE

Programming .NET Components : Remoting - Leasing and Sponsorship (part 3) - Sponsorship Management

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
12/2/2012 6:28:17 PM

6. Sponsorship Management

The sponsor in Example 2 is a completely general-purpose sponsor that simply returns the initial lease time (whatever that may be) on every renewal request. The problem with the code shown in Example 2 is that the client has to keep track of its remote objects and manually unregister each remote lease's sponsor when the application shuts down. However, you can automate this by providing a client-side sponsorship manager, in the form of the SponsorshipManager helper class:


    public class SponsorshipManager : MarshalByRefObject,
                                      ISponsor,
                                      IDisposable,
                                      IEnumerable<ILease>

    {
       public void Dispose( );
       public TimeSpan Renewal(ILease lease);
       public IEnumerator<ILease> GetEnumerator( );

       public void Register(MarshalByRefObject obj);
       public void Unregister(MarshalByRefObject obj);
       public void UnregisterAll( );     
       public void OnExit(object sender,EventArgs e);
    }

Example 3 shows the implementation of SponsorshipManager.

Example 3. The SponsorshipManager helper class
public class SponsorshipManager : MarshalByRefObject,
                                  ISponsor,
                                  IDisposable,
                                  IEnumerable<ILease>

{
   IList<ILease> m_LeaseList = new List<ILease>( );

   ~SponsorshipManager( )
   {
      UnregisterAll( );
   }
   void IDisposable.Dispose( )
   {
      UnregisterAll( );
   }
   TimeSpan ISponsor.Renewal(ILease lease)
   {
      Debug.Assert(lease.CurrentState == LeaseState.Active);
      return lease.InitialLeaseTime;
   }
   IEnumerator<ILease> IEnumerable<ILease>.GetEnumerator( )
   {
      foreach(ILease lease in m_LeaseList)
      {
         yield return lease;
      }
   }
   IEnumerator IEnumerable.GetEnumerator()
   {
      IEnumerable<ILease> enumerable = this;
      return enumerable.GetEnumerator()
   }
   public void OnExit(object sender,EventArgs e)
   {
      UnregisterAll( );
   }
   public void Register(MarshalByRefObject obj)
   {
      ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
      Debug.Assert(lease.CurrentState == LeaseState.Active);

      lease.Register(this);
      lock(this)
      {
         m_LeaseList.Add(lease);
      }
   }
   public void Unregister(MarshalByRefObject obj)
   {
      ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
      Debug.Assert(lease.CurrentState == LeaseState.Active);

      lease.Unregister(this);
      lock(this)
      {
         m_LeaseList.Remove(lease);
      }
   }
   public void UnregisterAll( )
   {
      lock(this)
      {
         while(m_LeaseList.Count>0)
         {
            ILease lease = m_LeaseList[0];
            lease.Unregister(this);
            m_LeaseList.RemoveAt(0);
         }
      }
   }
}


					  

The ClientSponsor Class

.NET provides a class for sponsorship management called ClientSponsor, defined as:

    public class ClientSponsor : MarshalByRefObject,ISponsor
    {
       public ClientSponsor( );
       public ClientSponsor(TimeSpan renewalTime);
       public TimeSpan RenewalTime{get;set;}
       public void Close( );
       public bool Register(MarshalByRefObject obj);
       public virtual TimeSpan Renewal(ILease lease);
       public void Unregister(MarshalByRefObject obj);
    }

ClientSponsor is similar in its design and use to SponsorshipManager, but unlike with SponsorshipManager, which returns the initial lease timeout for each lease, with ClientSponsor the client must explicitly set a single fixed lease renewal timeout for all sponsored leases. Because of this behavior, I recommend that you use SponsorshipManager instead of ClientSponsor.


SponsorshipManager implements ISponsor by returning the initial lease time of the provided lease. In addition, it maintains in a member variable called m_LeaseList a generic linked list of all the leases it sponsors. SponsorshipManager provides the Register( ) method:

    public void Register(MarshalByRefObject obj);

Register( ) accepts a remote object, extracts the lease from it, registers SponsorshipManager as the sponsor, and adds the lease to the internal list. The Unregister( ) method of the SponsorshipManager class is defined as:

    public void Unregister(MarshalByRefObject obj);

This method removes the lease associated with the specified object from the list and unregisters SponsorshipManager as a sponsor. SponsorshipManager also provides the UnregisterAll( ) method:

    public void UnregisterAll( );

This method unregisters SponsorshipManager from all the lease objects in the list. Note that the list access is done in a thread-safe manner by locking the SponsorshipManager on every access. Thread safety is required because multiple clients on multiple threads can use the same SponsorshipManager instance to manage their remote leases. However, the clients should not bother themselves with calling Unregister( ) or UnregisterAll( )—SponsorshipManager provides an event-handling method called OnExit( ):

    public void OnExit(object sender,EventArgs e);

OnExit( ) calls UnregisterAll( ) as a response to the client application shutting down. Example 4 shows how to use SponsorshipManager by a Windows Forms client.

Example 4. Using SponsorshipManager
partial class ClientForm : Form
{
   Button m_CallButton;
   SponsorshipManager m_SponsorshipManager;
   MyCAO m_MyCAO; //The remote CAO

   public ClientForm( )
   {
      InitializeComponent( );

      m_SponsorshipManager = new SponsorshipManager( );
      m_MyCAO = new MyCAO( );

      m_SponsorshipManager.Register(m_MyCAO);
      Application.ApplicationExit += m_SponsorshipManager.OnExit;
   }
   void InitializeComponent( )
   {...}

   static void Main( )
   {
      RemotingConfigurationEx.Configure( );
      Application.Run(new ClientForm( ));
   }
   void OnCall(object sender,EventArgs e)
   {     
      m_MyCAO.Count( );
   }
}


					  

The client in Example 4 maintains as member variables a remote object of type MyCAO (defined in Example 10-11) and a SponsorshipManager object. The client registers the remote object with SponsorshipManager and hooks up the application OnExit event with the SponsorshipManager OnExit( ) event-handling method. SponsorshipManager does the rest—it registers itself as the sponsor, and it unregisters all the remote leases it sponsors automatically when the client shuts down. Finally, note the use of C# 2.0 iterators in implementing the IEnumerable<ILease> interface. The interface allows clients to iterate over the list of managed sponsors, if the need ever arises:


    SponsorshipManager sponsorshipManager = new SponsorshipManager( );
    //Some code to initialize sponsorshipManager, then:
    foreach(ILease lease in sponsorshipManager)
    {...}
Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Video Sports
- The Banner Saga 2 [PS4/XOne/PC] PC Launch Trailer
- Welkin Road [PC] Early Access Trailer
- 7th Dragon III Code: VFD [3DS] Character Creation Trailer
- Human: Fall Flat [PS4/XOne/PC] Coming Soon Trailer
- Battlefleet Gothic: Armada [PC] Eldar Trailer
- Neon Chrome [PS4/XOne/PC] PC Release Date Trailer
- Rocketbirds 2: Evolution [Vita/PS4] Launch Trailer
- Battleborn [PS4/XOne/PC] 12 Min Gameplay Trailer
- 7 Days to Die [PS4/XOne/PC] Console Trailer
- Total War: Warhammer [PC] The Empire vs Chaos Warriors Gameplay Trailer
- Umbrella Corps [PS4/PC] Mercenary Customization Trailer
- Niten [PC] Debut Trailer
- Stellaris [PC] Aiming for the Stars - Dev. Diary Trailer #1
- LawBreakers [PC] Dev Diary #4: Concept Art Evolutions
programming4us programming4us
programming4us
 
 
programming4us