OnixS ICE iMpact Multicast Price Feed Handler C++ library 8.18.0
Users' manual and API documentation
Loading...
Searching...
No Matches
Getting Started Sample

Demonstrates the minimal end-to-end workflow: create a Feed Engine, configure a Handler (network interface, logging, license and connectivity configuration), bind it to the Feed Engine, attach event tracers, subscribe to a market and start receiving market data. It also shows how to manage POSIX signals in a dedicated thread on Linux.

Source code

#include "../Common/Defaults.h"
#include "../Common/ExchangeEventTracer.h"
#include "../Common/FeedEventTracer.h"
#include "../Common/MarketDataEventTracer.h"
#include "../Common/ServiceEventTracer.h"
#include "../Common/Utils.h"
#ifndef _WIN32
# include <pthread.h>
# include <csignal>
# include <cstdio>
# include <vector>
#endif
#include <cstdlib>
#ifndef _WIN32
void manageLinuxSignals();
#endif
int main(int /*argc*/, char** /*argv*/)
{
try
{
#ifndef _WIN32
manageLinuxSignals();
#endif
introduceMyself("Getting Started");
FeedEngineSettings feedEngineSettings;
FeedEngine feedEngine(feedEngineSettings);
HandlerSettings handlerSettings;
// For most of Linux operating systems it would be
// better to define following parameter to be able to
// successfully receive market data from multicast feeds.
handlerSettings.networkInterface = NETWORK_INTERFACE;
// Let's activate logging to get data for support.
handlerSettings.logLevel = LogLevels::Debug;
handlerSettings.logDirectory = LOG_DIRECTORY;
handlerSettings.logFileNamePrefix = LOG_FILE_NAME_PREFIX;
// By default, license is stored in lib folder of the distribute
// package. Instead of copying, let's use original file.
handlerSettings.licenseDirectory = LICENSE_DIRECTORY;
// Primary settings - connectivity configuration.
handlerSettings.connectivityConfiguration = AP1_C10Y_FILE;
// Constructs handler with custom license root and enabled logging.
Handler handler(handlerSettings);
// Binds the handler to the feed engine.
handler.bindFeedEngine(feedEngine);
// Traces all service events coming from handlers.
ServiceEventTracer serviceEventTracer;
serviceEventTracer.attachTo(handler);
// Traces all feed events coming from handlers.
FeedEventTracer feedEventTracer;
feedEventTracer.attachTo(handler);
// Traces all exchange events coming from handlers.
ExchangeEventTracer exchangeEventTracer;
exchangeEventTracer.attachTo(handler);
// Traces all market data events coming from handlers.
MarketDataEventTracer marketDataEventTracer;
marketDataEventTracer.attachTo(handler);
// Making subscription.
MarketSubscriptions subscriptions;
MarketSubscription brentFutures(
);
// Uncomment this line to add specific markets into your subscription.
// brentFutures.marketIds.insert(777);
subscriptions.insert(brentFutures);
// Now starts the subscription.
handler.start(subscriptions);
readUserResponse();
std::cout << "Shutdown the Handler..." << std::endl;
// Stop multicast data processing.
handler.stop();
std::cout << "Done." << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "Error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
catch (...)
{
std::cerr << "Unknown exception." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#ifndef _WIN32
// The code below illustrates how to manage some chosen signals:
// - Prevent all threads against signal receiving;
// - Establish separate thread to catch signals;
// - Track & suppress chosen signal(s) in this separate thread.
// Vector to keep suppressed signal numbers,
// it is used just for initialization of entire
// signal handling scheme.
typedef std::vector<int> SuppressedSignals;
SuppressedSignals suppressedSignals;
// Set of suppressed signals,
// used to block signals at inherited threads
// and to wait signals at signalWaiter() thread function.
sigset_t signalSet;
void errorAbort(int status, const char* reason)
{
printf("Aborted due to status %d: %s\n", status, reason ? reason : "no error message");
exit(1);
}
// Suppress signals enumerated at the SuppressedSignals vector.
void* signalWaiter(void*)
{
int signalNumber;
while (1)
{
sigwait(&signalSet, &signalNumber);
printf("Signal %d received and suppressed.\n", signalNumber);
}
return NULL;
}
void manageLinuxSignals()
{
// Initialize a set of suppressed signals, e.g. - signals which we are waiting for.
suppressedSignals.push_back(SIGPIPE);
//
pthread_t signalThreadId;
int status;
// Start by masking suppressed signals at the primary thread. All other
// threads inherit this signal mask and therefore will have the same signals
// suppressed.
sigemptyset(&signalSet);
for (SuppressedSignals::const_iterator i = suppressedSignals.begin(), e = suppressedSignals.end(); i != e; ++i)
{
sigaddset(&signalSet, *i);
}
// Install the signal mask against primary thread.
status = pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
if (status != 0)
{
errorAbort(status, "Set signal mask");
}
// Create the sigwait thread.
status = pthread_create(&signalThreadId, NULL, signalWaiter, NULL);
if (status != 0)
{
errorAbort(status, "Create signalWaiter");
}
}
#endif // !_WIN32