Пример #1
0
 /// <summary>
 /// Removes the given item from the collection
 /// </summary>
 /// <returns>True, if the item was removed, otherwise False</returns>
 /// <param name="item">The item that should be removed</param>
 public override bool Remove(IModelElement item)
 {
     if ((this._parent.Status == item))
     {
         this._parent.Status = null;
         return true;
     }
     IScheduledEvent scheduledEventItem = item.As<IScheduledEvent>();
     if (((scheduledEventItem != null)
                 && this._parent.ScheduledEvents.Remove(scheduledEventItem)))
     {
         return true;
     }
     if ((this._parent.Window == item))
     {
         this._parent.Window = null;
         return true;
     }
     if ((this._parent.TimeSchedule == item))
     {
         this._parent.TimeSchedule = null;
         return true;
     }
     return false;
 }
Пример #2
0
 /// <summary>
 /// Adds the given element to the collection
 /// </summary>
 /// <param name="item">The item to add</param>
 public override void Add(IModelElement item)
 {
     if ((this._parent.Status == null))
     {
         IStatus statusCasted = item.As<IStatus>();
         if ((statusCasted != null))
         {
             this._parent.Status = statusCasted;
             return;
         }
     }
     IScheduledEvent scheduledEventsCasted = item.As<IScheduledEvent>();
     if ((scheduledEventsCasted != null))
     {
         this._parent.ScheduledEvents.Add(scheduledEventsCasted);
     }
     if ((this._parent.Window == null))
     {
         IDateTimeInterval windowCasted = item.As<IDateTimeInterval>();
         if ((windowCasted != null))
         {
             this._parent.Window = windowCasted;
             return;
         }
     }
     if ((this._parent.TimeSchedule == null))
     {
         ITimeSchedule timeScheduleCasted = item.As<ITimeSchedule>();
         if ((timeScheduleCasted != null))
         {
             this._parent.TimeSchedule = timeScheduleCasted;
             return;
         }
     }
 }
Пример #3
0
 public void ScheduleEvent(int source, IScheduledEvent @event)
 {
     // The resolution of our scheduler (Task.Delay) is approximately 15ms (on Windows), which should be sufficient for our purposes
     Task.Delay(@event.ScheduledPeriod)
     .ContinueWith(t => RaiseEvent(source, @event))
     .ContinueWith(t => ScheduleEvent(source, @event), TaskContinuationOptions.ExecuteSynchronously);
 }
Пример #4
0
 /// <summary>
 /// Cancel a previously scheduled event.
 /// </summary>
 /// <param name="evt">The event to cancel</param>
 /// <returns>Returns true if the given event was successfully removed form the event queue</returns>
 public bool CancelEvent(IScheduledEvent evt)
 {
     if (_eventQueue.Remove(evt))
     {
         Console.WriteLine($"{DateTime.Now} : Cancelled {evt.GetType().Name} previously scheduled @ {evt.ScheduledTime}");
         return(true);
     }
     return(false);
 }
        public void ScheduleEvent(int source, IScheduledEvent @event)
        {
            var token = _cancellationTokenSource.Token;

            // The resolution of our scheduler (Task.Delay) is approximately 15ms (on Windows), which should be sufficient for our purposes
            Task.Delay(@event.ScheduledPeriod, token)
            .ContinueWith(t => RaiseEvent(source, @event), token)
            .ContinueWith(t => ScheduleEvent(source, @event), token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current);
        }
Пример #6
0
        public void Unschedule <T>(IScheduledEvent <T> args) where T : IEvent
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            this.app.GetScheduledEventHub().Unschedule(args);
        }
Пример #7
0
        /// <summary>
        /// Schedule a new event by adding it to the event queue.
        /// </summary>
        /// <param name="evt">The event to schedule</param>
        /// <exception cref="InvalidOperationException">thrown if evt is in the past</exception>
        public void Schedule(IScheduledEvent evt)
        {
            if (evt.ScheduledTime <= DateTime.Now)
            {
                throw new InvalidOperationException("A passed event cannot be scheduled.");
            }
            _eventQueue.Enqueue(evt);

            Console.WriteLine($"{DateTime.Now} : Scheduled new {evt.GetType().Name} @ {evt.ScheduledTime}");
        }
Пример #8
0
 public void Remove <T>(IScheduledEvent <T> args) where T : IEvent
 {
     for (int i = this.queue.Count - 1; i >= 0; i--)
     {
         if (object.ReferenceEquals(this.queue[i].ScheduledEvent, args))
         {
             this.RemoveAt(i);
             return;
         }
     }
 }
Пример #9
0
        /// <summary>
        /// Called when update interval is elapsed.
        /// Check the queue to trigger the next event if required.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            // While there is event that should be triggered at this time
            while (_eventQueue.Peek()?.ScheduledTime <= e.SignalTime)
            {
                IScheduledEvent evt = _eventQueue.Dequeue();

                Console.WriteLine($"{DateTime.Now} : {evt.GetType().Name} triggered");
                evt.Trigger(this);
            }
        }
        public void Unschedule <T>(IScheduledEvent <T> args) where T : IEvent
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            lock (this.queue)
            {
                this.queue.Remove(args);
            }
        }
Пример #11
0
            /// <summary>
            /// Adds the given element to the collection
            /// </summary>
            /// <param name="item">The item to add</param>
            public override void Add(IModelElement item)
            {
                IScheduledEvent scheduledEventsCasted = item.As <IScheduledEvent>();

                if ((scheduledEventsCasted != null))
                {
                    this._parent.ScheduledEvents.Add(scheduledEventsCasted);
                }
                if ((this._parent.RequestedWindow == null))
                {
                    IDateTimeInterval requestedWindowCasted = item.As <IDateTimeInterval>();
                    if ((requestedWindowCasted != null))
                    {
                        this._parent.RequestedWindow = requestedWindowCasted;
                        return;
                    }
                }
                if ((this._parent.ForInspectionDataSet == null))
                {
                    IInspectionDataSet forInspectionDataSetCasted = item.As <IInspectionDataSet>();
                    if ((forInspectionDataSetCasted != null))
                    {
                        this._parent.ForInspectionDataSet = forInspectionDataSetCasted;
                        return;
                    }
                }
                if ((this._parent.EstimatedWindow == null))
                {
                    IDateTimeInterval estimatedWindowCasted = item.As <IDateTimeInterval>();
                    if ((estimatedWindowCasted != null))
                    {
                        this._parent.EstimatedWindow = estimatedWindowCasted;
                        return;
                    }
                }
                IDocument documentsCasted = item.As <IDocument>();

                if ((documentsCasted != null))
                {
                    this._parent.Documents.Add(documentsCasted);
                }
                if ((this._parent.Status == null))
                {
                    IStatus statusCasted = item.As <IStatus>();
                    if ((statusCasted != null))
                    {
                        this._parent.Status = statusCasted;
                        return;
                    }
                }
            }
        public void Schedule <T>(IScheduledEvent <T> args) where T : IEvent
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var now  = this.app.GetTime();
            var item = new PriorityQueueItem <T>(this.app, args);

            item.MoveToNextOccurrence(now);
            if (now < item.Occurrence)
            {
                lock (this.queue)
                {
                    this.queue.Push(item);
                }
            }
        }
Пример #13
0
        public void Add_AddsScheduledEventThatIsCalledOnTimePeriod()
        {
            IScheduledEvent event1 = NewMock <IScheduledEvent>();
            IScheduledEvent event2 = NewMock <IScheduledEvent>();

            Expect.Once.On(eventFactory).Method("Create").WithAnyArguments().Will(Return.Value(event1));
            Expect.Once.On(eventFactory).Method("Create").WithAnyArguments().Will(Return.Value(event2));

            DateTime now = DateTime.Now;

            Expect.Once.On(clock).GetProperty("Now").Will(Return.Value(now));
            Expect.Once.On(clock).GetProperty("Now").Will(Return.Value(now));

            scheduler.Add(onScheduledEvent, TimeSpan.FromSeconds(3));
            scheduler.Add(onScheduledEvent, TimeSpan.FromSeconds(3));

            Assert.AreEqual(0, tickCount);

            // TODO
        }
Пример #14
0
            /// <summary>
            /// Removes the given item from the collection
            /// </summary>
            /// <returns>True, if the item was removed, otherwise False</returns>
            /// <param name="item">The item that should be removed</param>
            public override bool Remove(IModelElement item)
            {
                IScheduledEvent scheduledEventItem = item.As <IScheduledEvent>();

                if (((scheduledEventItem != null) &&
                     this._parent.ScheduledEvents.Remove(scheduledEventItem)))
                {
                    return(true);
                }
                if ((this._parent.RequestedWindow == item))
                {
                    this._parent.RequestedWindow = null;
                    return(true);
                }
                if ((this._parent.ForInspectionDataSet == item))
                {
                    this._parent.ForInspectionDataSet = null;
                    return(true);
                }
                if ((this._parent.EstimatedWindow == item))
                {
                    this._parent.EstimatedWindow = null;
                    return(true);
                }
                IDocument documentItem = item.As <IDocument>();

                if (((documentItem != null) &&
                     this._parent.Documents.Remove(documentItem)))
                {
                    return(true);
                }
                if ((this._parent.Status == item))
                {
                    this._parent.Status = null;
                    return(true);
                }
                return(false);
            }
Пример #15
0
 // Including the 'source' value as an argument here is an explicit design decision.
 // Section 9.5.3.1 specifies that this should be passed at the entropy provider level for security reasons, but because all sources are
 // to be used from within the same Application Domain (and hence same shared memory), this is an acceptable risk.
 private void RaiseEvent(int source, IScheduledEvent @event)
 {
     EntropyAvailable?.Invoke(source, @event.EventCallback());
 }
Пример #16
0
 public bool Exists(IScheduledEvent scheduledEvent)
 => scheduler.Exists(scheduledEvent);
Пример #17
0
 public void Remove(IScheduledEvent scheduledEvent)
 => scheduler.Remove(scheduledEvent);
Пример #18
0
 public void Add(IScheduledEvent scheduledEvent)
 => scheduler.Add(scheduledEvent);
Пример #19
0
 public int CompareTo(IScheduledEvent other)
 {
     return(ScheduledTime.CompareTo(other.ScheduledTime));
 }