#include "../Settings/Defaults.h"
#include "../Common/Helpers.h"
#include "../Common/Listener.h"
#include "../Common/Settings.h"
#include "../Common/Signal.h"
#include "../Common/Emulator.h"
using namespace Samples;
int main(int argc, char * argv[])
{
const AppConfiguration<
SettingsConfiguration, ConnectivityConfiguration, LogonConfiguration, StorageConfiguration<SessionStorageType::FileBased>
> cfg{"ExternalThreadMode", argc, argv};
try
{
SignalHelper::manageSignals();
auto settings = fillSettings(cfg);
const auto emulator = createEmulator(settings, cfg);
const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
Listener listener;
TcpStandardStack stack;
settings.ipSessionToken(bgwCredentials.ipSessionToken);
settings.threadingModel(ThreadingModel::External);
const auto bgwSession= createSession(stack, SessionType::BGW, settings, &listener, cfg.storage());
const auto connectTask = bgwSession->connectAsync(bgwCredentials.host, bgwCredentials.port);
while (!connectTask.is_ready())
stack.dispatchEvents();
bgwSession->send(Helper::createTraderLogonRequest(cfg.traderId(), cfg.traderPwd()));
Samples::wait(stack, listener.traderLoggedOn).get();
bgwSession->send(Helper::createOrder(cfg.traderId()));
std::clog << "\nPress <Ctrl+C> to disconnect the BGW session and terminate the application." << std::endl;
while(!SignalHelper::interruptDetected())
stack.dispatchEvents();
bgwSession->send(Helper::createTraderLogoutRequest(cfg.traderId()));
const auto disconnectTask = bgwSession->disconnectAsync("The BGW session is disconnected");
while (!disconnectTask.is_ready() || !stack.isQuiescent())
stack.dispatchEvents();
}
catch(const std::exception & ex)
{
std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
return 1;
}
return 0;
}