protected override void BrokerInstOnOnNotificationFailed(GcmNotification notification,
                                                          AggregateException exception)
 {
     base.BrokerInstOnOnNotificationFailed(notification, exception);
     exception.Handle(ex =>
     {
         if (ex is GcmNotificationException)
         {
             int count;
             if (WorkingQueue.TryGetValue(notification, out count) && count < TriesCount)
             {
                 RetryQueue(notification);
             }
             else
             {
                 Logger.NotificationFailed(notification, exception);
             }
         }
         else if (ex is GcmMulticastResultException)
         {
             RemoveNotification(notification);
             Logger.NotificationFailed(notification, exception);
         }
         else if (ex is DeviceSubscriptionExpiredException)
         {
             //TODO: Add notification expired logic
             var exc = ex as DeviceSubscriptionExpiredException;
             if (exc.NewSubscriptionId != null)
             {
                 notification.RegistrationIds.Remove(exc.OldSubscriptionId);
                 notification.RegistrationIds.Add(exc.NewSubscriptionId);
                 RetryQueue(notification);
             }
             else
             {
                 Logger.NotificationFailed(notification, exception);
             }
             TokenExperation.AddExpiredToken(exc.OldSubscriptionId, exc.NewSubscriptionId);
         }
         else if (ex is RetryAfterException)
         {
             RemoveNotification(notification);
             Logger.NotificationFailed(notification, exception);
         }
         else
         {
             int count;
             if (WorkingQueue.TryGetValue(notification, out count) && count < TriesCount)
             {
                 RetryQueue(notification);
             }
             else
             {
                 Logger.NotificationFailed(notification, exception);
             }
         }
         return(true);
     });
 }
Example #2
0
        public override void Execute()
        {
            var execution = new Queue <ICommand>();

            lock (SyncWorking)
            {
                SwapQueues();
                while (WorkingQueue.Count > 0)
                {
                    var command = WorkingQueue.Dequeue();
                    var wrapper = command as CommandWrapper;
                    if (!wrapper?.IsExecutable ?? false)
                    {
                        Enqueue(command);
                    }
                    else
                    {
                        execution.Enqueue(command);
                    }
                }
            }

            while (execution.Count > 0)
            {
                var executive = execution.Dequeue();
                try
                {
                                        #if TRACE_COMMANDS
                    var type = executive is ICommandTypeExtractor
                                                ? (executive as ICommandTypeExtractor).CommandType
                                                : executive.GetType();
                    Logger?.Log(_selfType, Level.Debug, $"executing command: {type.NameNice()}", null);
                    executive.Execute();
                                        #else
                    executive.Execute();
                                        #endif
                }
                catch (Exception exception)
                {
                    Logger?.Log(
                        executive is ICommandTypeExtractor
                                                        ? (executive as ICommandTypeExtractor).CommandType
                                                        : executive.GetType(),
                        Level.Error,
                        $"exception during execution from: {_selfType.NameNice()}\n{exception.ToText()}",
                        exception);
                }
            }
        }
 protected override void BrokerInstOnOnNotificationFailed(ApnsNotification notification,
                                                          AggregateException exception)
 {
     base.BrokerInstOnOnNotificationFailed(notification, exception);
     exception.Handle(ex =>
     {
         if (ex is ApnsNotificationException)
         {
             int count;
             if (WorkingQueue.TryGetValue(notification, out count) && count < TriesCount)
             {
                 RetryQueue(notification);
             }
             else
             {
                 TokenExperation.AddExpiredToken(notification.DeviceToken, null);
                 Logger.NotificationFailed(notification, ex);
             }
         }
         return(true);
     });
 }
Example #4
0
        public bool TryConsume(out ICommand result)
        {
            //! slow
            var execution = new Queue <ICommand>();

            result = null;
            lock (SyncWorking)
            {
                SwapQueues();
                while (WorkingQueue.Count > 0)
                {
                    var command = WorkingQueue.Dequeue();
                    var wrapper = command as CommandWrapper;
                    if (!wrapper?.IsExecutable ?? false)
                    {
                        Enqueue(command);
                    }
                    else
                    {
                        if (ReferenceEquals(null, result))
                        {
                            result = wrapper?.Source;
                        }
                        else
                        {
                            execution.Enqueue(command);
                        }
                    }
                }

                while (execution.Count > 0)
                {
                    Enqueue(execution.Dequeue());
                }
            }

            return(!ReferenceEquals(null, result));
        }
Example #5
0
        /// <summary>
        /// The Hopcroft - Karp algorithm finds equivalent states
        ///
        /// </summary>
        ///Algorithm:
        ///P := {{all accepting states}, {all nonaccepting states}};
        ///Q := {{all accepting states}};
        ///E := { all edges }
        ///while (Q is not empty) do
        ///     choose and remove a set A from Q
        ///     for each c in E do
        ///          let X be the set of states for which a transition on c leads to a state in A
        ///          for each set Y in P for which X ∩ Y is nonempty do
        ///               replace Y in P by the two sets X ∩ Y and Y \ X
        ///               if Y is in Q
        ///                    replace Y in Q by the same two sets
        ///               else
        ///                    add the smaller of the two sets to Q
        ///          end;
        ///     end;
        ///end;
        public static LinkedList <HashSet <int> > Original(List <DfaNode> states,
                                                           int edges)
        {
            var P = new LinkedList <HashSet <int> >();

            var acceptingSet    = new HashSet <int>();
            var nonAcceptingSet = new HashSet <int>();

            foreach (var state in states)
            {
                if (state.IsAccepting)
                {
                    acceptingSet.Add(state.Index);
                }
                else
                {
                    nonAcceptingSet.Add(state.Index);
                }
            }
            P.AddLast(acceptingSet);
            P.AddLast(nonAcceptingSet);

            var Q = new WorkingQueue(acceptingSet.ToSet(),
                                     nonAcceptingSet.ToSet());

            var X = new HashSet <int>();

            while (!Q.IsEmpty)
            {
                var A = Q.Remove();
                for (var c = 1; c < edges; c++)
                {
                    //X is the set of states for which a transition on c leads to a state in A
                    X.CopyFrom(states.Where(x => A.Contains(x[c])).Select(x => x.Index));
                    if (X.Count == 0)
                    {
                        continue;
                    }
                    for (var Y = P.First; Y != null; Y = Y.Next)
                    {
                        var intersection = X.Intersect(Y.Value);
                        if (intersection.Count == 0)
                        {
                            continue;
                        }
                        var disjointUnion = Y.Value.DisjointUnion(intersection);
                        if (disjointUnion.Count == 0)
                        {
                            continue;
                        }

                        var oldY = Y.Value;

                        if (disjointUnion.Count > 0)
                        {
                            Y.Value = intersection;
                            P.AddBefore(Y, disjointUnion);
                        }

                        if (!Q.TryReplace(oldY, intersection, disjointUnion))
                        {
                            Q.Add((intersection.Count < disjointUnion.Count) ?
                                  intersection :
                                  disjointUnion);
                        }
                    }
                }
            }

            for (var node = P.First; node != null; node = node.Next)
            {
                if (node.Value.Count <= 1)
                {
                    P.Remove(node);
                }
            }

            return(P);
        }