This sample demonstrates the usage of the Session Scheduler functionality.
 
 
#include "../Common/Helpers.h"
 
 
{
    const int MinutesPerHour = 60;
 
 
        sinceMidnight / SecondsPerHour,
        (sinceMidnight / SecondsPerMinute) % MinutesPerHour,
        sinceMidnight % SecondsPerMinute);
}
 
{
    void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum , Session * session) 
ONIXS_ILINK3_OVERRIDE 
    {
        std::cout << session->toString() << " changed its state to " << SessionStateId::toString(newState) << '.' << std::endl;
    }
};
 
{
public:
 
 
    {
    }
 
    void onWarning(
const SessionScheduler & , Session * session, 
const std::string & warningReason) 
ONIXS_ILINK3_OVERRIDE 
    {
        std::cout << "Scheduler reported a warning for the session " << session->toString() << ": " << warningReason;
    }
 
    void onError(
const SessionScheduler & , Session * session, 
const std::string & errorReason) 
ONIXS_ILINK3_OVERRIDE 
    {
        std::cout << "Error occurred while scheduling session " << session->toString() << ": " << errorReason;
    }
};
 
class Sample
{
public:
    Sample()
    {
        about();
    }
 
    ~Sample()
    {
        if(session_)
            session_->disconnect();
 
        delete session_;
        delete scheduler_;
    }
 
    void run(int marketSegmentId, const std::string& host, Scheduling::Port port)
    {
        constructSession(marketSegmentId);
 
        constructScheduler();
 
        std::cout << "Scheduling session " << session_->toString() << " for automatic connection." << std::endl;
 
        const SessionSchedule sessionSchedule = constructShortTimeActivitySchedule();
 
        const SessionConnectionSettings sessionConnectivity(host, port);
 
        scheduler_->add(session_, sessionSchedule, sessionConnectivity);
 
        std::cout << "Waiting for activity on scheduled session " << session_->toString() << '.' << std::endl << std::endl;
 
        
        Threading::ThisThread::sleep(1000 * sessionActivityTimeInSeconds());
 
        waitUntilLogout(session_);
 
        std::cout << std::endl << "Removing session " << session_->toString() << " from scheduling service." << std::endl;
 
        
        scheduler_->remove(session_);
    }
 
    void useConfigurationFile()
    {
        SessionSchedulerOptions schedulerOptions;
 
 
        SessionScheduler scheduler(schedulerOptions);
 
        scheduler.add(session_, "ScheduleId", "ConnectionId");
 
        Threading::ThisThread::sleep(1000 * sessionActivityTimeInSeconds());
 
        scheduler_->remove(session_);
    }
 
private:
    SessionStateChangeTracer sessionStateChangeTracer_;
 
    SessionScheduler* scheduler_;
    SchedulingIssueDetector schedulingIssueDetector_;
 
    void constructScheduler()
    {
        SessionSchedulerOptions schedulerOptions;
 
        scheduler_ = new SessionScheduler(schedulerOptions);
    }
 
    void constructSession(int marketSegmentId)
    {
        const SessionSettings settings = Samples::fillSettings();
        session_ = 
new Session(settings, marketSegmentId, &sessionStateChangeTracer_);
    }
 
    static SessionSchedule constructShortTimeActivitySchedule()
    {
        const TimeOfDay now = TimeOfDay::now();
 
        const TimeOfDay logonTime = now + Scheduling::Seconds(5);
 
        const TimeOfDay logoutTime = logonTime + Scheduling::Seconds(sessionActivityTimeInSeconds());
 
        return SessionSchedule(DayOfWeek::now(), DayOfWeek::now(), logonTime, logoutTime);
    }
 
    static void waitUntilLogout(Session * session)
    {
        const unsigned oneSecondPause = 1000;
 
        while(session->
state() != SessionStateId::Disconnected)
 
            Threading::ThisThread::sleep(oneSecondPause);
    }
 
    static int sessionActivityTimeInSeconds()
    {
        return 30;
    }
 
    static void about()
    {
        std::cout << "OnixS C++ CME iLink 3 Session Scheduling Sample." << std::endl << std::endl;
    }
};
 
void usage()
{
    std::cerr << "usage: [MarketSegmentId] [Host] [Port]" << std::endl;
}
 
int main(int argc, char * argv[])
{
    try
    {
        int marketSegmentId = 0;
        std::string host;
 
        if (argc >= 2)
            marketSegmentId = atoi(argv[1]);
 
        if (argc >= 3)
            host = argv[2];
 
        if (argc >= 4)
            port = atoi(argv[3]);
 
        if (marketSegmentId == 0 || host.empty() || port == 0)
        {
            usage();
            return 1;
        }
 
        Sample sample;
 
        sample.run(marketSegmentId, host, port);
 
        std::cout << std::endl << "Done." << std::endl;
    }
    catch(const std::exception & ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        return 1;
    }
 
    return 0;
}