nx_metadata_sdk 1.0 Metadata SDK

random_pauser.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <chrono>
6
7 namespace nx {
8 namespace vms_server_plugins {
9 namespace analytics {
10 namespace stub {
11
12 class RandomPauser
13 {
14 public:
15 RandomPauser(
16 const std::chrono::milliseconds& pauseDuration = std::chrono::seconds(1),
17 int maxNumberOfPauses = 3,
18 int randModulus = 9)
19 :
20 m_duration(pauseDuration),
21 m_maxNumberOfPauses(maxNumberOfPauses),
22 m_randModulus(randModulus)
23 {
24 }
25
26 void update()
27 {
28 if (m_paused && std::chrono::high_resolution_clock::now() - m_pauseStartTime < m_duration)
29 return;
30
31 if (m_paused)
32 {
33 --m_maxNumberOfPauses;
34 m_pauseStartTime = std::chrono::high_resolution_clock::time_point();
35 m_resuming = true;
36 }
37 else if(m_resuming)
38 {
39 m_resuming = false;
40 }
41
42 m_paused = m_maxNumberOfPauses > 0
43 ? rand() % m_randModulus == 0
44 : false;
45
46 if (m_paused)
47 m_pauseStartTime = std::chrono::high_resolution_clock::now();
48 }
49
50 bool paused() const
51 {
52 return m_paused;
53 }
54
55 bool resuming() const
56 {
57 return m_resuming;
58 }
59
60 private:
61 const std::chrono::milliseconds m_duration;
62 int m_maxNumberOfPauses;
63 const int m_randModulus;
64 bool m_paused = false;
65 std::chrono::high_resolution_clock::time_point m_pauseStartTime;
66 bool m_resuming = false;
67 };
68
69 } // namespace stub
70 } // namespace analytics
71 } // namespace vms_server_plugins
72 } // namespace nx
73
nx
Definition: apple_utils.h:6
nx::vms_server_plugins::analytics::stub::RandomPauser
Definition: random_pauser.h:12