Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Montura authored Dec 9, 2023
1 parent b802e53 commit 934b1ca
Showing 1 changed file with 70 additions and 62 deletions.
132 changes: 70 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,80 +49,88 @@ Java API and implementing them in the native library.
### Implementation details and usage

```C
#include <cstdio>
#include <iostream>
#include <chrono>
#include <thread>
#include <chrono>

#include "api/dxfg_api.h"
#include "api/dxfg_catch_exception.h"
#include "dxfeed/utils/JNICommon.hpp"

void printEvent(const dxfg_event_type_t* pEvent) {
if (pEvent->clazz == DXFG_EVENT_TIME_AND_SALE) {
const auto* time_and_sale = (const dxfg_time_and_sale_t*) pEvent;
printf(
"C: TIME_AND_SALE{event_symbol=%s, bid_price=%f, exchange_sale_conditions=%s, buyer=%s, seller=%s}\n",
time_and_sale->market_event.event_symbol,
time_and_sale->bid_price,
time_and_sale->exchange_sale_conditions,
time_and_sale->buyer,
time_and_sale->seller
);
}
fflush(stdout);
#include "api/dxfg_javac.h"
#include "api/dxfg_endpoint.h"
#include "api/dxfg_feed.h"
#include "api/dxfg_subscription.h"

void printUsage() {
std::cout <<
"Usage:" << std::endl <<
"DxFeedConnect <JAVA_HOME> <address> <types> <symbols>" << std::endl << std::endl <<

"Where:" << std::endl <<
" JAVA_HOME - the absolute path directory where JDK(JRE) is installed," << std::endl <<
" e.g.: /Users/FirstName.SecondName/Documents/amazon-corretto-8.jdk/Contents/Home" << std::endl <<
" address - The address to connect to retrieve data (remote host or local tape file)." << std::endl <<
" To pass an authorization token, add to the address: \"[login=entitle:<token>]\"," << std::endl <<
" e.g.: demo.dxfeed.com:7300[login=entitle:<token>]" << std::endl <<
" types - Is comma-separated list of dxfeed event types ({eventTypeNames})." << std::endl <<
" symbol - Is comma-separated list of symbol names to get events for (e.g. ""IBM,AAPL,MSFT"")." << std::endl <<
" for Candle event specify symbol with aggregation like in \"AAPL{{=d}}\"" << std::endl << std::endl <<

"Examples:" << std::endl <<
" DxFeedConnect /Users/userName/Documents/java8/Home demo.dxfeed.com:7300 Quote,Trade MSFT,IBM" << std::endl <<
" DxFeedConnect /Users/userName/Documents/java8/Home demo.dxfeed.com:7300 TimeAndSale AAPL" << std::endl;
}

void c_print(graal_isolatethread_t *thread, dxfg_event_type_list *events, void *user_data) {
for (int i = 0; i < events->size; ++i) {
printEvent(events->elements[i]);
dxfg_event_type_t* pEvent = events->elements[i];
if (pEvent && pEvent->clazz == DXFG_EVENT_QUOTE) {
const auto* quote = (const dxfg_quote_t*) pEvent;
printf(
"C: QUOTE{event_symbol=%s, bid_price=%f, bid_time=%lld, ask_price=%f, ask_time=%lld}\n",
quote->market_event.event_symbol,
quote->bid_price,
quote->bid_time,
quote->ask_price,
quote->ask_time);
}
}
}

int main(int argc, char** argv) {
// load cmd args
const int defaultArgSize = 4;
if (argc < defaultArgSize) {
std::cerr << "Error: expected 3 args: <JAVA_HOME, address:port, symbol, ... VMOptions >" << std::endl;
int main() {
const auto address = "demo.dxfeed.com:7300";
const auto symbol = "AAPL";

// Create thread on which all the work will be executed.
auto thread = create_thread();
if (thread == nullptr) {
return -1;
}
const auto javaHomePath = argv[1];
const auto address = argv[2];
const auto symbol = argv[3];

// init java VM
const int vmOptionsSize = 2;
const char* jvmArgs[vmOptionsSize] = { "-Xmx12G", "-Dcom.devexperts.qd.impl.matrix.Agent.MaxBufferSize=50000000" };
dxfeed::jni::VMOptions vmOptions { javaHomePath, jvmArgs, vmOptionsSize };
graal_isolate_t* isolate;
graal_isolatethread_t* thread;
int hr = graal_create_isolate(&vmOptions, &isolate, &thread);
if (hr == JNI_OK) {
// init context, connection, subscription
std::cout << "Connection to address:" << address << std::endl;
dxfg_endpoint_t* endpoint = dxfg_DXEndpoint_create(thread);
dxfg_DXEndpoint_connect(thread, endpoint, address);
dxfg_feed_t* feed = dxfg_DXEndpoint_getFeed(thread, endpoint);
dxfg_subscription_t* subscriptionTimeAndSale = dxfg_DXFeed_createSubscription(thread, feed, DXFG_EVENT_TIME_AND_SALE);

// add listener with user code
dxfg_feed_event_listener_t* listener = dxfg_DXFeedEventListener_new(thread, &c_print, nullptr);
dxfg_DXFeedSubscription_addEventListener(thread, subscriptionTimeAndSale, listener);

dxfg_string_symbol_t symbolAAPL;
symbolAAPL.supper.type = STRING;
symbolAAPL.symbol = symbol;

// add symbol to subscription
dxfg_DXFeedSubscription_setSymbol(thread, subscriptionTimeAndSale, &symbolAAPL.supper);

// sleep then clean up
std::chrono::seconds seconds(30);
std::this_thread::sleep_for(seconds);

dxfg_DXFeedSubscription_close(thread, subscriptionTimeAndSale);
dxfg_JavaObjectHandler_release(thread, &listener->handler);
dxfg_JavaObjectHandler_release(thread, &subscriptionTimeAndSale->handler);
dxfg_JavaObjectHandler_release(thread, &endpoint->handler);

// Create endpoint and connect to specified address.
dxfg_endpoint_t* endpoint = dxfg_DXEndpoint_create(thread);
dxfg_DXEndpoint_connect(thread, endpoint, address);

// Create feed and subscription with specified types attached to feed.
dxfg_feed_t* feed = dxfg_DXEndpoint_getFeed(thread, endpoint);
dxfg_subscription_t* subscription = dxfg_DXFeed_createSubscription(thread, feed, DXFG_EVENT_QUOTE);

// Adds event listener.
dxfg_feed_event_listener_t* listener = dxfg_DXFeedEventListener_new(thread, &c_print, nullptr);
dxfg_DXFeedSubscription_addEventListener(thread, subscription, listener);

// Create STRING symbol for APPLE.
dxfg_string_symbol_t stringSymbol;
stringSymbol.supper.type = STRING;
stringSymbol.symbol = symbol;

dxfg_DXFeedSubscription_setSymbol(thread, subscription, &stringSymbol.supper);
std::chrono::seconds seconds(10);
std::this_thread::sleep_for(seconds);

dxfg_DXFeedSubscription_close(thread, subscription);
dxfg_DXEndpoint_close(thread, endpoint);
dxfg_JavaObjectHandler_release(thread, &listener->handler);
dxfg_JavaObjectHandler_release(thread, &subscription->handler);
dxfg_JavaObjectHandler_release(thread, &endpoint->handler);
}
```
Expand Down

0 comments on commit 934b1ca

Please sign in to comment.