This sample demonstrates the usage of the Session Scheduler functionality.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
24
28
33
35{
37 {
39 }
40};
41
43{
44public:
46
48
49 void onWarning(
const SessionScheduler & , Session * session,
const std::string & warningReason)
override
50 {
51 std::cout <<
"Scheduler reported a warning for the session " << session->
toString() <<
": " << warningReason;
52 }
53
54 void onError(
const SessionScheduler & , Session * session,
const std::string & errorReason)
override
55 {
56 std::cout <<
"Error occurred while scheduling session " << session->
toString() <<
": " << errorReason;
57 }
58};
59
61{
63 const int MinutesPerHour = 60;
65
67
69 sinceMidnight / SecondsPerHour,
70 (sinceMidnight / SecondsPerMinute) % MinutesPerHour,
71 sinceMidnight % SecondsPerMinute);
72}
73
75{
76 const int sessionActivityTimeInSeconds = 30;
77
79
81
83
85}
86
88{
89 while (session->
state() != state)
91}
92
93int main(
int argc,
char * argv[])
94{
95
101 > cfg{ "Session Scheduler", argc, argv };
102
103 try
104 {
106
108
110
111
112 BusSession busSession(sessionSettings, &sessionStateChangeTracer_);
113
114
115 BgwSession bgwSession(sessionSettings, &sessionStateChangeTracer_);
116
117
122
123
124 std::atomic<bool> stopSchedulerThread{ false };
125 std::thread schedulerThread([&]()
126 {
127 const int spinWaitMicrosecondPause = 1000;
128 while (!stopSchedulerThread)
129 {
130 scheduler.dispatchEvents();
132 }
133 });
134
135 std::cout << "Scheduling BUS session " << busSession.toString() << " for automatic connection." << std::endl;
138 scheduler.add(&busSession, sessionScheduleForBus, sessionConnectivityForBus);
139
140 std::cout << "Scheduling BGW session " << bgwSession.toString() << " for automatic connection." << std::endl;
141
143 scheduler.add(&bgwSession, sessionScheduleForBgw, &busSession);
144
145 std::cout << "Waiting for activity on scheduled sessions " << bgwSession.toString() << '.' << std::endl << std::endl;
146
147
149
151
152 std::cout << std::endl << "Removing sessions from scheduling service." << std::endl;
153
154
155 scheduler.remove(&busSession);
156 scheduler.remove(&bgwSession);
157
158 stopSchedulerThread = true;
159 schedulerThread.join();
160
161 std::cout << std::endl << "Done." << std::endl;
162 }
163 catch(const std::exception & ex)
164 {
165 std::cerr << "Error: " << ex.what() << std::endl;
166 return 1;
167 }
168
169 return 0;
170}
int main(int argc, char *argv[])
SessionSchedule constructShortTimeActivitySchedule(int delayInSeconds=5)
void waitUntilState(Session *session, SessionStateId::Enum state, unsigned pollMs=1000)
TimeOfDay operator+(TimeOfDay time, Scheduling::Seconds seconds)
Connection-related settings.
SessionScheduler events notifications.
SessionSchedulerListener * eventListener() const noexcept
Seconds sinceMidnight() const noexcept
std::string toString() const
SessionStateId::Enum state() const
static void spinWait(size_t microseconds)
Executes a single instruction during the given number of microseconds.
static void sleep(size_t milliseconds)
Suspends the execution of the current thread for the given amount of time.
void onError(const SessionScheduler &, Session *session, const std::string &errorReason) override
Notifies about an error.
SchedulingIssueDetector()=default
~SchedulingIssueDetector() override=default
void onWarning(const SessionScheduler &, Session *session, const std::string &warningReason) override
Notifies about a non-critical issue.
int Seconds
The number of seconds.
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
static const std::string & toString(Enum state) noexcept
@ Established
Session is fully established.
@ Disconnected
Session is disconnected.