OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
SessionScheduler.cpp
Go to the documentation of this file.
1/*
2* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3*
4* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5* and international copyright treaties.
6*
7* Access to and use of the software is governed by the terms of the applicable ONIXS Software
8* Services Agreement (the Agreement) and Customer end user license agreements granting
9* a non-assignable, non-transferable and non-exclusive license to use the software
10* for it's own data processing purposes under the terms defined in the Agreement.
11*
12* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13* of this source code or associated reference material to any other location for further reproduction
14* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15*
16* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17* the terms of the Agreement is a violation of copyright law.
18*/
19
22
24
25#include <Common/Helpers.h>
26#include <Common/Settings.h>
27#include <Common/Emulator.h>
28
29using namespace OnixS::ICE;
30using namespace OnixS::ICE::BOE;
31using namespace OnixS::ICE::BOE::Scheduling;
32using namespace Samples;
33
35{
36 void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum /*prevState*/, Session * session) override
37 {
38 std::cout << session->toString() << " changed its state to " << SessionStateId::toString(newState) << '.' << std::endl;
39 }
40};
41
43{
44public:
46
47 ~SchedulingIssueDetector() override = default;
48
49 void onWarning(const SessionScheduler & /*scheduler*/, 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 & /*scheduler*/, Session * session, const std::string & errorReason) override
55 {
56 std::cout << "Error occurred while scheduling session " << session->toString() << ": " << errorReason;
57 }
58};
59
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}
73
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}
86
87void waitUntilState(Session* session, SessionStateId::Enum state, unsigned pollMs = 1000)
88{
89 while (session->state() != state)
91}
92
93int main(int argc, char * argv[])
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}
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)
SessionSchedulerListener * eventListener() const noexcept
SessionScheduler & add(Session *session, const SessionSchedule &schedule, const SessionConnectionSettings &connection)
Adds the session to the automatic connection management.
SessionScheduler & remove(Session *session)
Removes the session from the automatic connection management services.
void dispatchEvents()
Processes events on the Scheduler and performs the necessary session management.
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)
Definition Emulator.h:157
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
static const std::string & toString(Enum state) noexcept
@ Established
Session is fully established.
@ Disconnected
Session is disconnected.