OnixS C++ FIX Engine 4.14.0
Users' manual and API documentation
Loading...
Searching...
No Matches
Buy Side Sample

This sample connects to the configured host and port (acts as a FIX Initiator). When the session is established, the SingleOrder - New FIX message is sent to the counterparty.

Source code

Listener.h

class Listener : public OnixS::FIX::ISessionListener
{
public:
Listener();
OnixS::Threading::Semaphore & completionEvent();
bool failed() const;
void onInboundApplicationMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) final;
void onInboundSessionMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) final;
void onStateChange(OnixS::FIX::SessionState::Enum newState, OnixS::FIX::SessionState::Enum prevState, OnixS::FIX::Session * sn) final;
void onError(OnixS::FIX::ErrorReason::Enum reason, const std::string & description, OnixS::FIX::Session * sn) final;
void onWarning(OnixS::FIX::WarningReason::Enum reason, const std::string & description, OnixS::FIX::Session * sn) final;
private:
void recordFailure();
OnixS::Threading::Semaphore executionReportReceived_;
mutable OnixS::Threading::Mutex failureMutex_;
bool failed_;
};

Listener.cpp

#include "Listener.h"
#include <OnixS/FIXEngine/FIX/FIX44.h>
#include <iostream>
#include <string>
using namespace OnixS::FIX;
Listener::Listener() : executionReportReceived_(0), failed_(false) {}
OnixS::Threading::Semaphore & Listener::completionEvent()
{
return executionReportReceived_;
}
bool Listener::failed() const
{
OnixS::Threading::MutexGuard guard(failureMutex_);
return failed_;
}
void Listener::recordFailure()
{
OnixS::Threading::MutexGuard guard(failureMutex_);
failed_ = true;
}
void Listener::onInboundApplicationMsg(Message & msg, Session * )
{
std::clog << "\nIncoming application-level message:\n" << msg << std::endl;
if(msg.type() != FIX44::Values::MsgType::ExecutionReport)
return;
std::clog << "Received ExecutionReport." << std::endl;
executionReportReceived_.release();
}
void Listener::onInboundSessionMsg(Message & msg, Session * )
{
std::clog << "\nIncoming session-level message:\n" << msg << std::endl;
if(msg.type() == FIX44::Values::MsgType::Logon)
std::clog << "BuySide received Logon." << std::endl;
}
void Listener::onStateChange(SessionState::Enum newState, SessionState::Enum prevState,
Session * )
{
std::clog
<< "\nSession's state is changed, prevState="
<< SessionState::toString(prevState)
<< ", newState="
<< std::endl;
}
void Listener::onError(ErrorReason::Enum , const std::string & description, Session * )
{
recordFailure();
executionReportReceived_.release();
std::cerr << "\nSession-level error:" << description << std::endl;
}
void Listener::onWarning(WarningReason::Enum , const std::string & description,
Session * )
{
std::cerr << "\nSession-level warning:" << description << std::endl;
}

BuySide.cpp

#include "Listener.h"
#include "../../Common/Helpers.h"
#include "../../Common/SampleArguments.h"
#include "../../Common/Settings.h"
using namespace OnixS::FIX;
using namespace OnixS::FIX::FIX44;
using namespace Settings;
// Uncomment if you'd like to test SSL feature.
//#define SSL_ENCRYPTION 1
int runBuySide(const Sample::Arguments & arguments)
{
std::clog << "BuySide sample." << std::endl << std::endl;
try {
manageLinuxSignals();
EngineSettings settings;
settings.dictionaryFile("FixDialectDescription.xml")
.licenseStore(LicenseStore);
Engine::init(settings);
EngineShutdownGuard engineShutdownGuard;
Listener listener;
Session session(SenderCompId, TargetCompId, FixProtocolVersion, &listener);
Message customLogon(Values::MsgType::Logon, FixProtocolVersion);
bool setResetSeqNumFlagInLogon = true; // For testing only!
customLogon.setFlag(Tags::ResetSeqNumFlag, setResetSeqNumFlagInLogon);
const std::string rawData;
if(! rawData.empty()) {
customLogon.set(Tags::RawData, rawData)
.set(Tags::RawDataLength, (int) rawData.size());
}
const std::string counterpartyHost = "localhost";
const int heartBtInt = 30;
// Uncomment the line below if you would like to set the outgoing message sequence number "manually".
//session.setOutSeqNum(11);
#ifdef SSL_ENCRYPTION
std::clog << "SSL Mode." << std::endl;
session.encryptionMethod(EncryptionMethod::SSL);
#endif
session.logonAsInitiator(counterpartyHost, arguments.port, heartBtInt, &customLogon);
Sample::waitForUser(arguments, "Press any key to send the order...");
Message order(Values::MsgType::NewOrderSingle, FixProtocolVersion);
setOrderFields(&order);
session.send(&order);
std::clog << "The order " << std::endl << order << std::endl << " was sent" << std::endl;
Sample::waitForUserOrEvent(
arguments,
"Press any key to disconnect the session and terminate the application.",
listener.completionEvent());
session.logout("The session is disconnected by BuySide").shutdown();
if(listener.failed())
return 1;
std::clog << "BuySide completed successfully." << std::endl;
}
catch(const std::exception & ex) {
processSampleException(ex.what());
return 1;
}
return 0;
}
int main(int argc, char * argv[])
{
const int defaultPort =
#ifdef SSL_ENCRYPTION
SslListenPort;
#else
ListenPort;
#endif
return Sample::runSample(argc, argv, defaultPort, &runBuySide);
}