OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
SessionScheduler.cpp File Reference

Go to the source code of this file.

Classes

class  SessionStateChangeTracer
class  SchedulingIssueDetector

Functions

TimeOfDay operator+ (TimeOfDay time, Scheduling::Seconds seconds)
SessionSchedule constructShortTimeActivitySchedule (int delayInSeconds=5)
void waitUntilState (Session *session, SessionStateId::Enum state, unsigned pollMs=1000)
int main (int argc, char *argv[])

Function Documentation

◆ constructShortTimeActivitySchedule()

SessionSchedule constructShortTimeActivitySchedule ( int delayInSeconds = 5)

Definition at line 74 of file SessionScheduler.cpp.

75{
76 const int sessionActivityTimeInSeconds = 30;
77
78 const TimeOfDay now = TimeOfDay::now();
79
80 const TimeOfDay logonTime = now + Scheduling::Seconds(delayInSeconds);
81
82 const TimeOfDay logoutTime = logonTime + Scheduling::Seconds(sessionActivityTimeInSeconds);
83
84 return SessionSchedule(DayOfWeek::now(), DayOfWeek::now(), logonTime, logoutTime);
85}
int Seconds
The number of seconds.

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 93 of file SessionScheduler.cpp.

94{
95 // `--help` to show options.
96 const AppConfiguration<
101 > cfg{ "Session Scheduler", argc, argv };
102
103 try
104 {
105 auto sessionSettings = fillSettings(cfg);
106
107 const auto emulator = createEmulator(sessionSettings, cfg);
108
109 SessionStateChangeTracer sessionStateChangeTracer_;
110
111 // Construct a BUS session.
112 BusSession busSession(sessionSettings, &sessionStateChangeTracer_);
113
114 // Construct a BGW session.
115 BgwSession bgwSession(sessionSettings, &sessionStateChangeTracer_);
116
117 // Construct a scheduler.
118 SchedulingIssueDetector schedulingIssueDetector_;
119 SessionSchedulerOptions schedulerOptions;
120 schedulerOptions.eventListener(&schedulingIssueDetector_);
121 SessionScheduler scheduler(schedulerOptions);
122
123 // Start a thread to dispatch events for the scheduler.
124 std::atomic<bool> stopSchedulerThread{ false };
125 std::thread schedulerThread([&]()
126 {
127 const int spinWaitMicrosecondPause = 1000;
128 while (!stopSchedulerThread)
129 {
130 scheduler.dispatchEvents();
131 Threading::ThisThread::spinWait(spinWaitMicrosecondPause);
132 }
133 });
134
135 std::cout << "Scheduling BUS session " << busSession.toString() << " for automatic connection." << std::endl;
136 const SessionSchedule sessionScheduleForBus = constructShortTimeActivitySchedule(5);
137 const SessionConnectionSettings sessionConnectivityForBus(cfg.host(), cfg.port());
138 scheduler.add(&busSession, sessionScheduleForBus, sessionConnectivityForBus);
139
140 std::cout << "Scheduling BGW session " << bgwSession.toString() << " for automatic connection." << std::endl;
141 //Give BGW session additional 5 seconds to ensure BUS session started
142 const SessionSchedule sessionScheduleForBgw = constructShortTimeActivitySchedule(10);
143 scheduler.add(&bgwSession, sessionScheduleForBgw, &busSession);
144
145 std::cout << "Waiting for activity on scheduled sessions " << bgwSession.toString() << '.' << std::endl << std::endl;
146
147 // Should sleep for a while to let Scheduler connect session.
149
151
152 std::cout << std::endl << "Removing sessions from scheduling service." << std::endl;
153
154 // The sessions have to 'pulse' connection till this time so that scheduling could be destroyed.
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}
SessionSchedule constructShortTimeActivitySchedule(int delayInSeconds=5)
void waitUntilState(Session *session, SessionStateId::Enum state, unsigned pollMs=1000)
SessionSchedulerListener * eventListener() const noexcept
static void spinWait(size_t microseconds)
Executes a single instruction during the given number of microseconds.
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
Definition Emulator.h:157
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
@ Established
Session is fully established.
@ Disconnected
Session is disconnected.

◆ operator+()

TimeOfDay operator+ ( TimeOfDay time,
Scheduling::Seconds seconds )

Definition at line 60 of file SessionScheduler.cpp.

61{
62 const Scheduling::Seconds SecondsPerMinute = 60;
63 const int MinutesPerHour = 60;
64 const Scheduling::Seconds SecondsPerHour = SecondsPerMinute * MinutesPerHour;
65
66 const Scheduling::Seconds sinceMidnight = time.sinceMidnight() + seconds;
67
68 return TimeOfDay(
69 sinceMidnight / SecondsPerHour,
70 (sinceMidnight / SecondsPerMinute) % MinutesPerHour,
71 sinceMidnight % SecondsPerMinute);
72}

◆ waitUntilState()

void waitUntilState ( Session * session,
SessionStateId::Enum state,
unsigned pollMs = 1000 )

Definition at line 87 of file SessionScheduler.cpp.

88{
89 while (session->state() != state)
91}
SessionStateId::Enum state() const
static void sleep(size_t milliseconds)
Suspends the execution of the current thread for the given amount of time.