Skip to content

Commit

Permalink
[EN-7588] Implement PublishProfiles sample
Browse files Browse the repository at this point in the history
Add a sample code.
  • Loading branch information
AnatolyKalin committed May 6, 2024
1 parent 7609d49 commit da5f44f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 71 deletions.
2 changes: 1 addition & 1 deletion include/dxfeed_graal_cpp_api/api/DXPublisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct DXFCPP_EXPORT DXPublisher : SharedEntity {
/**
* Publishes events to the corresponding feed.
*
* @param collection The collection of events to publish.
* @param events The collection of events to publish.
*/
void publishEvents(std::initializer_list<std::shared_ptr<EventType>> events) noexcept {
publishEvents(std::begin(events), std::end(events));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ struct DXFCPP_EXPORT ObservableSubscriptionChangeListener {
virtual void symbolsAdded(const std::unordered_set<SymbolWrapper> &symbols) = 0;
virtual void symbolsRemoved(const std::unordered_set<SymbolWrapper> & /*symbols*/){};
virtual void subscriptionClosed(){};

static std::unique_ptr<ObservableSubscriptionChangeListener> create();
};

struct DXFCPP_EXPORT ObservableSubscriptionChangeListenerImpl : ObservableSubscriptionChangeListener {

};

DXFCPP_END_NAMESPACE
Expand Down
98 changes: 28 additions & 70 deletions samples/cpp/PublishProfiles/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#include <dxfeed_graal_cpp_api/api.hpp>

#include <atomic>
#include <chrono>
#include <mutex>
#include <string>

#include <range/v3/all.hpp>
Expand All @@ -14,85 +11,46 @@ using namespace dxfcpp;
using namespace dxfcpp::literals;
using namespace std::literals;

void printUsage() {
auto usageString = R"(
Usage:
DxFeedConnect <address> <types> <symbols> [<time>]
Where:
address - The address to connect to retrieve data (remote host or local tape file).
To pass an authorization token, add to the address: "[login=entitle:<token>]",
e.g.: demo.dxfeed.com:7300[login=entitle:<token>]
types - Is comma-separated list of dxfeed event types ()" +
dxfcpp::enum_utils::getEventTypeEnumNamesList() + " or " +
dxfcpp::enum_utils::getEventTypeEnumClassNamesList() + R"().
symbols - Is comma-separated list of symbol names to get events for (e.g. "IBM,AAPL,MSFT").
for Candle event specify symbol with aggregation like in "AAPL{=d}"
time - Is from-time for history subscription in standard formats.
Same examples of valid from-time:
20070101-123456
20070101-123456.123
2005-12-31 21:00:00
2005-12-31 21:00:00.123+03:00
2005-12-31 21:00:00.123+0400
2007-11-02Z
123456789 - value-in-milliseconds
Examples:
DxFeedConnect demo.dxfeed.com:7300 Quote,Trade MSFT,IBM
DxFeedConnect demo.dxfeed.com:7300 TimeAndSale AAPL
DxFeedConnect demo.dxfeed.com:7300 Candle AAPL{=d} 20230901Z)";

std::cout << usageString << std::endl;
}

/*
* Using address like ":7700" it starts a server on a specified port where it provides Profile
* event for any symbol ending with ":TEST" suffix.
*/
int main(int argc, char *argv[]) {
try {
if (argc < 4) {
printUsage();

if (argc < 2) {
return 0;
}

std::mutex ioMtx{};

// Parse args.
std::string address = argv[1];
auto types = CmdArgsUtils::parseTypes(argv[2]);
auto symbols = CmdArgsUtils::parseSymbols(argv[3]);
auto time = -1LL;

if (argc >= 5) {
time = TimeFormat::DEFAULT_WITH_MILLIS_WITH_TIMEZONE.parse(argv[4]);
}

// Create an endpoint and connect to specified address.
auto endpoint = DXEndpoint::create()->connect(address);

// Create a subscription with specified types attached to feed.
auto sub = endpoint->getFeed()->createSubscription(types);

// Add an event listener.
sub->addEventListener([&ioMtx](const auto &events) {
std::lock_guard lock{ioMtx};

for (auto &&e : events) {
std::cout << e << "\n";
}
});

// Add symbols.
if (time != -1) {
sub->addSymbols(symbols | ranges::views::transform([time](const auto &s) {
return TimeSeriesSubscriptionSymbol(s, time);
}));
} else {
sub->addSymbols(symbols);
}
// Create publisher endpoint and connect it to the specified address
auto endpoint = DXEndpoint::create(DXEndpoint::Role::PUBLISHER)->connect(address);
auto publisher = endpoint->getPublisher();

// Observe Profile subscriptions and publish profiles for "xxx:TEST" symbols
// publisher->getSubscription(Profile::TYPE)->addChangeListener(ObservableSubscriptionChangeListener {
// [publisher](auto&& symbols) /* symbolsAdded */ {
// std::vector<std::shared_ptr<Profile>> events{};
// events.reserve(symbols.sise());
//
// for (auto&& symbol : symbols) {
// if (symbol.isStringSymbol()) {
// auto s = symbol.asStringSymbol();
//
// if (s.size() > 5 && s.rfind(":TEST") == s.size() - 5) {
// auto profile = std::make_shared<Profile>(s);
//
// profile->setDescription("Test symbol");
// events.push_back(profile);
// }
// }
// }
//
// events.shrink_to_fit();
// publisher->publishEvents(events);
// }
// });

std::cin.get();
} catch (const JavaException &e) {
Expand Down

0 comments on commit da5f44f

Please sign in to comment.