Exemplo n.º 1
0
        internal void SponsorCall(ISponsor sponsor)
        {
            bool flag = false;

            if (this.state != LeaseState.Expired)
            {
                lock (this.sponsorTable)
                {
                    try
                    {
                        object sponsorId = this.GetSponsorId(sponsor);
                        this.sponsorCallThread = Thread.CurrentThread.GetHashCode();
                        AsyncRenewal     renewal = new AsyncRenewal(sponsor.Renewal);
                        SponsorStateInfo info    = (SponsorStateInfo)this.sponsorTable[sponsorId];
                        info.sponsorState = SponsorState.Waiting;
                        renewal.BeginInvoke(this, new AsyncCallback(this.SponsorCallback), null);
                        if ((info.sponsorState == SponsorState.Waiting) && (this.state != LeaseState.Expired))
                        {
                            this.leaseManager.RegisterSponsorCall(this, sponsorId, this.sponsorshipTimeout);
                        }
                        this.sponsorCallThread = 0;
                    }
                    catch (Exception)
                    {
                        flag = true;
                        this.sponsorCallThread = 0;
                    }
                }
                if (flag)
                {
                    this.Unregister(sponsor);
                    this.ProcessNextSponsor();
                }
            }
        }
Exemplo n.º 2
0
        public void Register(ISponsor obj, TimeSpan renewalTime)
        {
            lock (this)
            {
                BCLDebug.Trace("REMOTE", "Lease " + id + " Register Sponsor  renewalTime ", renewalTime, " state ", ((Enum)state).ToString());
                if (state == LeaseState.Expired || sponsorshipTimeout == TimeSpan.Zero)
                {
                    return;
                }

                Object sponsorId = GetSponsorId(obj);
                lock (sponsorTable)
                {
                    if (renewalTime > TimeSpan.Zero)
                    {
                        AddTime(renewalTime);
                    }
                    if (!sponsorTable.ContainsKey(sponsorId))
                    {
                        // Place in tables
                        sponsorTable[sponsorId] = new SponsorStateInfo(renewalTime, SponsorState.Initial);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void ProcessNextSponsor()
        {
            object   sponsorId = null;
            TimeSpan zero      = TimeSpan.Zero;

            lock (this.sponsorTable)
            {
                IDictionaryEnumerator enumerator = this.sponsorTable.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object           key  = enumerator.Key;
                    SponsorStateInfo info = (SponsorStateInfo)enumerator.Value;
                    if ((info.sponsorState == SponsorState.Initial) && (zero == TimeSpan.Zero))
                    {
                        zero      = info.renewalTime;
                        sponsorId = key;
                    }
                    else if (info.renewalTime > zero)
                    {
                        zero      = info.renewalTime;
                        sponsorId = key;
                    }
                }
            }
            if (sponsorId != null)
            {
                this.SponsorCall(this.GetSponsorFromId(sponsorId));
            }
            else
            {
                this.Cancel();
            }
        }
Exemplo n.º 4
0
        internal void SponsorCall(ISponsor sponsor)
        {
            BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCall state ", ((Enum)state).ToString());
            bool exceptionOccurred = false;

            if (state == LeaseState.Expired)
            {
                return;
            }

            lock (sponsorTable)
            {
                try
                {
                    Object sponsorId = GetSponsorId(sponsor);
                    sponsorCallThread = Thread.CurrentThread.GetHashCode();
                    AsyncRenewal     ar = new AsyncRenewal(sponsor.Renewal);
                    SponsorStateInfo sponsorStateInfo = (SponsorStateInfo)sponsorTable[sponsorId];
                    sponsorStateInfo.sponsorState = SponsorState.Waiting;

                    // The first parameter should be the lease we are trying to renew.
                    IAsyncResult iar = ar.BeginInvoke(this, new AsyncCallback(this.SponsorCallback), null);
                    if ((sponsorStateInfo.sponsorState == SponsorState.Waiting) && (state != LeaseState.Expired))
                    {
                        //   Even if we get here, the operation could still complete before
                        //   we call the the line below. This seems to be a race.

                        // Sponsor could have completed before statement is reached, so only execute
                        // if the sponsor state is still waiting
                        leaseManager.RegisterSponsorCall(this, sponsorId, sponsorshipTimeout);
                    }
                    sponsorCallThread = 0;
                }catch (Exception)
                {
                    // Sponsor not avaiable
                    exceptionOccurred = true;

                    sponsorCallThread = 0;
                }
            }

            if (exceptionOccurred)
            {
                BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCall Sponsor Exception ");
                Unregister(sponsor);
                ProcessNextSponsor();
            }
        }
Exemplo n.º 5
0
 internal void SponsorTimeout(object sponsorId)
 {
     lock (this)
     {
         if (this.sponsorTable.ContainsKey(sponsorId))
         {
             lock (this.sponsorTable)
             {
                 SponsorStateInfo info = (SponsorStateInfo)this.sponsorTable[sponsorId];
                 if (info.sponsorState == SponsorState.Waiting)
                 {
                     this.Unregister(this.GetSponsorFromId(sponsorId));
                     this.ProcessNextSponsor();
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
 public void Unregister(ISponsor sponsor)
 {
     lock (this)
     {
         if (this.state != LeaseState.Expired)
         {
             object sponsorId = this.GetSponsorId(sponsor);
             lock (this.sponsorTable)
             {
                 if (sponsorId != null)
                 {
                     this.leaseManager.DeleteSponsor(sponsorId);
                     SponsorStateInfo info1 = (SponsorStateInfo)this.sponsorTable[sponsorId];
                     this.sponsorTable.Remove(sponsorId);
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 internal void SponsorTimeout(Object sponsorId)
 {
     lock (this)
     {
         if (!sponsorTable.ContainsKey(sponsorId))
         {
             return;
         }
         lock (sponsorTable)
         {
             SponsorStateInfo sponsorStateInfo = (SponsorStateInfo)sponsorTable[sponsorId];
             BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorTimeout  sponsorState ", ((Enum)sponsorStateInfo.sponsorState).ToString());
             if (sponsorStateInfo.sponsorState == SponsorState.Waiting)
             {
                 Unregister(GetSponsorFromId(sponsorId));
                 ProcessNextSponsor();
             }
         }
     }
 }
Exemplo n.º 8
0
        private void ProcessNextSponsor()
        {
            BCLDebug.Trace("REMOTE", "Lease ", id, " ProcessNextSponsor");

            Object   largestSponsor     = null;
            TimeSpan largestRenewalTime = TimeSpan.Zero;


            lock (sponsorTable)
            {
                IDictionaryEnumerator e = sponsorTable.GetEnumerator();
                // Find sponsor with largest previous renewal value
                while (e.MoveNext())
                {
                    Object           sponsorId        = e.Key;
                    SponsorStateInfo sponsorStateInfo = (SponsorStateInfo)e.Value;
                    if ((sponsorStateInfo.sponsorState == SponsorState.Initial) && (largestRenewalTime == TimeSpan.Zero))
                    {
                        largestRenewalTime = sponsorStateInfo.renewalTime;
                        largestSponsor     = sponsorId;
                    }
                    else if (sponsorStateInfo.renewalTime > largestRenewalTime)
                    {
                        largestRenewalTime = sponsorStateInfo.renewalTime;
                        largestSponsor     = sponsorId;
                    }
                }
            }

            if (largestSponsor != null)
            {
                SponsorCall(GetSponsorFromId(largestSponsor));
            }
            else
            {
                // No more sponsors to try, Cancel
                BCLDebug.Trace("REMOTE", "Lease ", id, " ProcessNextSponsor no more sponsors");
                Cancel();
            }
        }
Exemplo n.º 9
0
        public void Unregister(ISponsor sponsor)
        {
            lock (this)
            {
                BCLDebug.Trace("REMOTE", "Lease", id, " Unregister  state ", ((Enum)state).ToString());
                if (state == LeaseState.Expired)
                {
                    return;
                }

                Object sponsorId = GetSponsorId(sponsor);
                lock (sponsorTable)
                {
                    if (sponsorId != null)
                    {
                        leaseManager.DeleteSponsor(sponsorId);
                        SponsorStateInfo sponsorStateInfo = (SponsorStateInfo)sponsorTable[sponsorId];
                        sponsorTable.Remove(sponsorId);
                    }
                }
            }
        }
Exemplo n.º 10
0
        // On another thread
        internal void SponsorCallback(IAsyncResult iar)
        {
            BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback IAsyncResult ", iar, " state ", ((Enum)state).ToString());
            if (state == LeaseState.Expired)
            {
                return;
            }

            int thisThread = Thread.CurrentThread.GetHashCode();

            if (thisThread == sponsorCallThread)
            {
                WaitCallback threadFunc = new WaitCallback(this.SponsorCallback);
                ThreadPool.QueueUserWorkItem(threadFunc, iar);
                return;
            }

            AsyncResult      asyncResult      = (AsyncResult)iar;
            AsyncRenewal     ar               = (AsyncRenewal)asyncResult.AsyncDelegate;
            ISponsor         sponsor          = (ISponsor)ar.Target;
            SponsorStateInfo sponsorStateInfo = null;

            if (iar.IsCompleted)
            {
                // Sponsor came back with renewal
                BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor completed");
                bool     exceptionOccurred = false;
                TimeSpan renewalTime       = TimeSpan.Zero;
                try
                {
                    renewalTime = (TimeSpan)ar.EndInvoke(iar);
                }catch (Exception)
                {
                    // Sponsor not avaiable
                    exceptionOccurred = true;
                }
                if (exceptionOccurred)
                {
                    BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback Sponsor Exception ");
                    Unregister(sponsor);
                    ProcessNextSponsor();
                }
                else
                {
                    Object sponsorId = GetSponsorId(sponsor);
                    lock (sponsorTable)
                    {
                        if (sponsorTable.ContainsKey(sponsorId))
                        {
                            sponsorStateInfo = (SponsorStateInfo)sponsorTable[sponsorId];
                            sponsorStateInfo.sponsorState = SponsorState.Completed;
                            sponsorStateInfo.renewalTime  = renewalTime;
                        }
                        else
                        {
                            // Sponsor was deleted, possibly from a sponsor time out
                        }
                    }

                    if (sponsorStateInfo == null)
                    {
                        // Sponsor was deleted
                        ProcessNextSponsor();
                    }
                    else if (sponsorStateInfo.renewalTime == TimeSpan.Zero)
                    {
                        BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor did not renew ");
                        Unregister(sponsor);
                        ProcessNextSponsor();
                    }
                    else
                    {
                        Renew(sponsorStateInfo.renewalTime);
                    }
                }
            }
            else
            {
                // Sponsor timed out
                // Note time outs should be handled by the LeaseManager
                BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor did not complete, timed out");
                Unregister(sponsor);
                ProcessNextSponsor();
            }
        }
Exemplo n.º 11
0
 internal void SponsorCallback(IAsyncResult iar)
 {
     if (this.state != LeaseState.Expired)
     {
         if (Thread.CurrentThread.GetHashCode() == this.sponsorCallThread)
         {
             WaitCallback callBack = new WaitCallback(this.SponsorCallback);
             ThreadPool.QueueUserWorkItem(callBack, iar);
         }
         else
         {
             AsyncResult      result        = (AsyncResult)iar;
             AsyncRenewal     asyncDelegate = (AsyncRenewal)result.AsyncDelegate;
             ISponsor         target        = (ISponsor)asyncDelegate.Target;
             SponsorStateInfo info          = null;
             if (iar.IsCompleted)
             {
                 bool     flag = false;
                 TimeSpan zero = TimeSpan.Zero;
                 try
                 {
                     zero = asyncDelegate.EndInvoke(iar);
                 }
                 catch (Exception)
                 {
                     flag = true;
                 }
                 if (flag)
                 {
                     this.Unregister(target);
                     this.ProcessNextSponsor();
                 }
                 else
                 {
                     object sponsorId = this.GetSponsorId(target);
                     lock (this.sponsorTable)
                     {
                         if (this.sponsorTable.ContainsKey(sponsorId))
                         {
                             info = (SponsorStateInfo)this.sponsorTable[sponsorId];
                             info.sponsorState = SponsorState.Completed;
                             info.renewalTime  = zero;
                         }
                     }
                     if (info == null)
                     {
                         this.ProcessNextSponsor();
                     }
                     else if (info.renewalTime == TimeSpan.Zero)
                     {
                         this.Unregister(target);
                         this.ProcessNextSponsor();
                     }
                     else
                     {
                         this.RenewInternal(info.renewalTime);
                     }
                 }
             }
             else
             {
                 this.Unregister(target);
                 this.ProcessNextSponsor();
             }
         }
     }
 }
Exemplo n.º 12
0
        public void Register(ISponsor obj, TimeSpan renewalTime)
        {
            lock(this)
            {
                BCLDebug.Trace("REMOTE", "Lease "+id+" Register Sponsor  renewalTime ",renewalTime," state ",((Enum)state).ToString());
                if (state == LeaseState.Expired || sponsorshipTimeout == TimeSpan.Zero)
                    return;

                Object sponsorId = GetSponsorId(obj);
                lock(sponsorTable)
                {
                    if (renewalTime > TimeSpan.Zero)
                        AddTime(renewalTime);
                    if (!sponsorTable.ContainsKey(sponsorId))
                    {
                        // Place in tables
                        sponsorTable[sponsorId] = new SponsorStateInfo(renewalTime, SponsorState.Initial);
                    }
                }
            }
        }