This sample demonstrates all aspects of the session scheduler functionality.
#include <memory>
#include <fstream>
#include <iostream>
#include "../../Common/Helpers.h"
#include "../../Common/Settings.h"
using namespace Settings;
namespace {
{
public:
void onInboundApplicationMsg(
Message &,
Session *) ONIXS_FIXENGINE_FINAL {}
};
};
struct SchedulingTimeSpan {
{
public:
: span_(span) {
}
int span() const {
return span_;
}
private:
int span_;
};
};
{
const int SecondsPerMinute = 60;
const int MinutesPerHour = 60;
const int SecondsPerHour = SecondsPerMinute * MinutesPerHour;
sinceMidnight / SecondsPerHour,
(sinceMidnight / SecondsPerMinute) % MinutesPerHour,
sinceMidnight % SecondsPerMinute);
}
{
Session * session) ONIXS_FIXENGINE_FINAL {
std::cout << static_cast<const std::string &>(*session) << " changed its state to " <<
SessionState::toString(newState) << '.' << std::endl;
}
void onInboundApplicationMsg(
Message &,
Session *) ONIXS_FIXENGINE_FINAL {
;
}
};
{
public:
{}
{}
void onWarning(
const SessionScheduler & ,
Session * session,
const std::string & warningReason) ONIXS_FIXENGINE_FINAL
{
std::cout << "Scheduler reported a warning for the session " << static_cast<const std::string &>(*session) << ": " << warningReason;
}
{
std::cout << "Error occurred while scheduling session " << static_cast<const std::string &>(*session) << ": " << errorReason;
}
};
class Sample
{
public:
threadingModel_(model), reactor_(threadingModel_ ==
SchedulerThreadingModel::ExternalThread ? new TCPStandard::Stack() : ONIXS_FIXENGINE_NULLPTR)
{
representYourself();
initializeFixEngine();
constructSessions();
constructScheduler();
}
~Sample() {
initiator_->shutdown();
initiator_.reset();
acceptor_->shutdown();
acceptor_.reset();
scheduler_.reset();
Engine::shutdown();
if(threadingModel_ == SchedulerThreadingModel::ExternalThread)
{
while(!reactor_->isQuiescent())
reactor_->dispatchEvents();
}
}
void run() {
acceptor_->resetLocalSequenceNumbers();
initiator_->resetLocalSequenceNumbers();
acceptor_->logonAsAcceptor();
std::cout << "Scheduling Initiator session " << static_cast<const std::string &>
(*initiator_) << " for automatic connection." << std::endl;
scheduler_->add(initiator_.get(), initiatorSchedule, initiatorConnectivity);
std::cout << "Waiting for activity on scheduled session " << static_cast<const std::string &>
(*initiator_) << '.' << std::endl << std::endl;
waitUntilState(initiator_.get(), SessionState::Active);
waitUntilState(initiator_.get(), SessionState::Disconnected);
std::cout << std::endl << "Removing Initiator session " << static_cast<const std::string &>
(*initiator_) << " from scheduling service." << std::endl;
scheduler_->remove(initiator_.get());
acceptor_->logout();
}
void useConfigurationFile() {
initiator_->resetLocalSequenceNumbers();
scheduler.
add(initiator_.get(),
"ScheduleId",
"ConnectionId");
scheduler_->remove(initiator_.get());
}
private:
MySessionListener acceptorListener_;
SessionStateChangeTracer initiatorStateChangeTracer_;
SchedulingIssueDetector schedulingIssueDetector_;
void constructScheduler() {
}
void constructSessions() {
acceptor_.reset(
new Session(TargetCompId, SenderCompId, FixProtocolVersion, &acceptorListener_));
initiator_.reset(
new Session(reactor_.get(), SenderCompId, TargetCompId, FixProtocolVersion, &initiatorStateChangeTracer_));
}
DayOfWeek::Monday,
DayOfWeek::Sunday,
logonTime,
logoutTime,
SequenceNumberResetPolicy::Never);
}
const unsigned oneSecondPause = 1000;
const int spinWaitMicrosecondPause = 10;
while(session->
state() != state)
{
if(threadingModel_ == SchedulerThreadingModel::DedicatedThreads)
else
{
scheduler_->dispatchEvents();
reactor_->dispatchEvents();
}
}
}
static void initializeFixEngine() {
Engine::init(settings);
}
static int sessionActivityTimeInSeconds() {
return 30;
}
void representYourself() {
std::cout << "OnixS C++ FIX Engine Session Scheduling Sample with " << SchedulerThreadingModel::toString(threadingModel_) << "." << std::endl << std::endl;
std::cout << "Usage: SessionScheduler threadingModel[DedicatedThreads][ExternalThread]" << std::endl << std::endl;
}
};
int main(int argc, char * argv[])
{
try {
const std::string
ThreadingModel = (argc > 1 ? argv[1] :
"DedicatedThreads");
Sample sample(ThreadingModel == "ExternalThread" ? SchedulerThreadingModel::ExternalThread : SchedulerThreadingModel::DedicatedThreads);
sample.run();
std::cout << std::endl << "Done." << std::endl;
return 0;
}
catch(const std::exception & ex) {
processSampleException(ex.what());
return 1;
}
}