public Poller(int pollIntervalSeconds, IPollable itemToPoll)
		{
			this.itemToPoll = itemToPoll;
			timer = new Timer(pollIntervalSeconds*1000);
			timer.AutoReset = false;
			timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
		}
예제 #2
0
파일: MinWaiter.cs 프로젝트: brifl/public
 public MinWaiter(TimeSpan minTime, IPollable <DateTime> timeProvider, IDelay delay)
 {
     _minTimeMs    = minTime.TotalMilliseconds;
     _timeProvider = timeProvider;
     _delay        = delay;
     _lastSet      = _timeProvider.GetValue();
 }
예제 #3
0
 public Poller(int pollIntervalSeconds, IPollable itemToPoll)
 {
     this.itemToPoll = itemToPoll;
     timer           = new Timer(pollIntervalSeconds * 1000);
     timer.AutoReset = false;
     timer.Elapsed  += new ElapsedEventHandler(Timer_Elapsed);
 }
예제 #4
0
        /// <summary>
        /// Register the service
        /// </summary>
        /// <param name="service">The service to register</param>
        public void Add(IService service)
        {
            services.Add(service);

            // If the service is a pollable, also do some bookkeeping to schedule the pollable when necesary
            if (service is IPollable && Started)
            {
                IPollable pollable = service as IPollable;
                Schedule(pollable, DateTime.UtcNow.Ticks + pollable.PollTimeSpan.Ticks);
            }
        }
예제 #5
0
        public void Remove(IService service)
        {
            services.Remove(service);

            // If the service is a pollable, also do some bookkeeping and remove the pollable from scheduling
            if (service is IPollable && Started)
            {
                IPollable pollable = service as IPollable;
                var       item     = pollableTimers.First(kvp => kvp.Value == pollable);
                pollableTimers.Remove(item);
            }
        }
예제 #6
0
        private void Schedule(IPollable pollable, long tick)
        {
            ISet <IPollable> pollables;

            if (!pollableTimers.TryGetValue(tick, out pollables))
            {
                pollables            = new HashSet <IPollable>();
                pollableTimers[tick] = pollables;
            }

            pollables.Add(pollable);
        }
예제 #7
0
        // Update all entities
        public void Poll()
        {
            foreach (Entity <Room> e in entities)
            {
                IPollable p = e as IPollable;
                if (p != null)
                {
                    p.Poll();
                }
            }

            Postprocess();
        }
예제 #8
0
        /// <summary>
        /// Start main processing loop
        /// </summary>
        /// <remarks>
        /// This algorithm uses a sorted dictionary to determine what pollable needs to be ran next.
        /// This results in a O(log n) implementation instead of a typical O(n) when looping through each pollable
        /// </remarks>
        public void Start()
        {
            pollableTimers = new SortedDictionary <long, ISet <IPollable> >();
            Started        = true;
            long currentTime = DateTime.UtcNow.Ticks;

            // Populate sorted dictionary with initial runtimes
            foreach (IService service in services)
            {
                service.Start();

                if (service is IPollable)
                {
                    IPollable pollable = service as IPollable;
                    Schedule(pollable, currentTime + pollable.PollTimeSpan.Ticks);
                }
            }

            while (!CancellationToken.IsCancellationRequested)
            {
                // See whats next
                var kvp = pollableTimers.First();
                pollableTimers.Remove(kvp);

                long             nextTime  = kvp.Key;
                ISet <IPollable> pollables = kvp.Value;

                // See how long we need to wait to run it, if at all
                currentTime = DateTime.UtcNow.Ticks;
                if (nextTime > currentTime)
                {
                    Thread.Sleep(TimeSpan.FromTicks(nextTime - currentTime));
                }

                // Poll the pollable, readd it to the sorted dictionary so we can continue
                while (pollables.Count > 0)
                {
                    IPollable pollable = pollables.First();
                    pollables.Remove(pollable);
                    pollable.Poll();
                    Schedule(pollable, nextTime + pollable.PollTimeSpan.Ticks);
                }
            }
        }
예제 #9
0
 public Poller(int pollIntervalMilliseconds, IPollable itemToPoll)
 {
     this.itemToPoll = itemToPoll;
     this.pollIntervalMilliseconds = pollIntervalMilliseconds;
 }
예제 #10
0
 public Metronome(IPollable <DateTime> time)
 {
     _time = time;
 }
예제 #11
0
 public TestRunnable2(IPollable <TemperatureHumidity> tempHumidity)
 {
     _tempHumidity = tempHumidity;
 }
예제 #12
0
 public Poller(int pollIntervalMilliseconds, IPollable itemToPoll)
 {
     this.itemToPoll = itemToPoll;
     this.pollIntervalMilliseconds = pollIntervalMilliseconds;
 }
예제 #13
0
파일: Delayer.cs 프로젝트: brifl/public
 public Delayer(IPollable <DateTime> timeProvider)
 {
     _timeProvider = timeProvider;
 }