This sample shows how to use Gateway emulator.
#include "../Common/Helpers.h"
#include "../Common/Listener.h"
 
using namespace Samples;
class GatewaySampleListener : public GatewayListener
{
public:
    void onNewOrderSingle(const NewOrderSingle514 & msg, Testing::Gateway* gateway) override
    {
        std::clog << "\nReceived:\n" << msg << std::endl;
 
        updateReport(msg);
        report_->setUuId(gateway->uuid());
 
        gateway->send(report_, ++reportsCounter_);
        gateway->outSeqNum(reportsCounter_ + 1);
 
        std::clog << "\nSent:\n" << report_.message() << std::endl;
    }
 
    void onOrderCancelReplaceRequest(const Messaging::OrderCancelReplaceRequest515 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onOrderCancelRequest(const Messaging::OrderCancelRequest516 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onMassQuote(const Messaging::MassQuote517 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onQuoteCancel(const Messaging::QuoteCancel528 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onOrderStatusRequest(const Messaging::OrderStatusRequest533 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onOrderMassStatusRequest(const Messaging::OrderMassStatusRequest530 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onOrderMassActionRequest(const Messaging::OrderMassActionRequest529 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onNewOrderCross(const Messaging::NewOrderCross544 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onRequestForQuote(const Messaging::RequestForQuote543 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onSecurityDefinitionRequest(const Messaging::SecurityDefinitionRequest560 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onPartyDetailsDefinitionRequest(const Messaging::PartyDetailsDefinitionRequest518 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onPartyDetailsListRequest(const Messaging::PartyDetailsListRequest537 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
    void onExecutionAck(const Messaging::ExecutionAck539 & msg, Testing::Gateway * gateway) override
    {
        onMessage(msg, gateway);
    }
 
private:
    template <typename Message>
    void onMessage(const Message& msg, Testing::Gateway * gateway)
    {
        std::clog << "\nReceived:\n" << msg << std::endl;
 
        MessageHolder<BusinessReject521> reject;
 
        reject->
            setBusinessRejectReason(BusinessRejectReason::Other)
            .setPossRetransFlag(BooleanFlag::False)
            .setSendingTimeEpoch(123)
            .setText("Rejected")
            .setUuId(gateway->uuid())
            ;
 
        gateway->send(reject, ++reportsCounter_);
        gateway->outSeqNum(reportsCounter_ + 1);
 
        std::clog << "\nSent:\n" << reject.message() << std::endl;
    }
};
 
int main(int argc, char * argv[])
{
    clog << "CME iLink 3 Exchange Emulator Sample." << endl;
 
    string host = "127.0.0.1";
 
    if (argc > 1)
        port = atoi(argv[1]);
 
    try
    {
        SignalHelper::manageLinuxSignals();
 
        std::future<void> emulatorTaskDone;
 
        std::promise<void> emulatorTaskPromise;
        emulatorTaskDone = emulatorTaskPromise.get_future();
 
        std::unique_ptr<Testing::Gateway> gateway(
            new Testing::Gateway(fillSettings(true).licenseStores(), port, host.c_str()));
 
        gateway->enableLogger("ExchangeEmulatorSample.log");
 
        std::atomic<bool> stopRequested(false);
 
        std::thread(std::bind([&](std::promise<void>& p)
        {
            try
            {
                GatewaySampleListener clientMessageListener;
                bool connectionAccepted = false;
 
                while(!stopRequested && !connectionAccepted)
                    connectionAccepted = gateway->tryAcceptConnection(std::chrono::milliseconds(1));
 
                if(connectionAccepted)
                {
                    std::clog << "Connection accepted." << endl;
 
                    gateway->acceptSession();
 
                    std::clog << "Session accepted." << endl;
 
                    const auto terminate = gateway->waitUntilTerminate(&clientMessageListener, nullptr, &stopRequested);
 
                    if(terminate.valid())
                        std::clog  << "The client has disconnected." << endl;
                }
 
                p.set_value();
            }
            catch (const std::exception& ex)
            {
                std::cerr << "Exception in Emulator's thread: " << ex.what() << '.' << std::endl;
                p.set_exception(current_exception());
            }
            catch(...)
            {
                std::cerr << "UNKNOWN Exception in Emulator's thread." << std::endl;
                p.set_exception(current_exception());
            }
        },
            std::move(emulatorTaskPromise))
        ).detach();
 
 
        Helper::waitUntilEnterKey("terminate the application");
        stopRequested = true;
 
        if(emulatorTaskDone.valid())
        {
            emulatorTaskDone.wait_for(std::chrono::seconds(10));
            emulatorTaskDone.get();
        }
    }
    catch(const std::exception & ex)
    {
        cerr << "\nEXCEPTION: " << ex.what() << endl;
        return 1;
    }
 
    return 0;
}