Skip to content

Commit

Permalink
[EN-7588] Implement PublishProfiles sample
Browse files Browse the repository at this point in the history
ObservableSubscriptionChangeListener
IsolatedObservableSubscriptionChangeListener
  • Loading branch information
AnatolyKalin committed May 13, 2024
1 parent 75007a1 commit 5503703
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ set(dxFeedGraalCxxApi_Exceptions_Sources

set(dxFeedGraalCxxApi_Isolated_Sources
src/isolated/api/IsolatedDXEndpoint.cpp
src/isolated/api/osub/IsolatedObservableSubscriptionChangeListener.cpp
src/isolated/internal/IsolatedString.cpp
src/isolated/internal/IsolatedTimeFormat.cpp
)
Expand Down
1 change: 1 addition & 0 deletions include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251 4996)
#include "exceptions/GraalException.hpp"

#include "isolated/api/IsolatedDXEndpoint.hpp"
#include "isolated/api/osub/IsolatedObservableSubscriptionChangeListener.hpp"
#include "isolated/internal/IsolatedString.hpp"
#include "isolated/internal/IsolatedTimeFormat.hpp"
#include "isolated/internal/IsolatedTools.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251)

DXFCPP_BEGIN_NAMESPACE

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(){};
struct DXFCPP_EXPORT ObservableSubscriptionChangeListener : RequireMakeShared<ObservableSubscriptionChangeListener> {
explicit ObservableSubscriptionChangeListener(LockExternalConstructionTag);
~ObservableSubscriptionChangeListener() noexcept override;

static std::shared_ptr<ObservableSubscriptionChangeListener>
create(std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsAdded);

static std::shared_ptr<ObservableSubscriptionChangeListener>
create(std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsAdded,
std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsRemoved,
std::function<void()> onSubscriptionClosed);

private:
JavaObjectHandle<ObservableSubscriptionChangeListener> handle_;
SimpleHandler<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsAdded_{};
SimpleHandler<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsRemoved_{};
SimpleHandler<void()> onSubscriptionClosed_{};

struct Impl;

ObservableSubscriptionChangeListener();
std::unique_ptr<Impl> impl_;
};

DXFCPP_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include "../../../internal/Conf.hpp"

#include <string>
#include <string_view>

DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251)

#include "../../../api/osub/ObservableSubscriptionChangeListener.hpp"

DXFCPP_BEGIN_NAMESPACE

namespace isolated::api::IsolatedObservableSubscriptionChangeListener {
/**
* Calls the Graal SDK function `dxfg_ObservableSubscriptionChangeListener_new` in isolation.
* @param functionSymbolsAdded A user function that is used as a `symbolsAdded` callback for the listener.
* @param functionSymbolsRemoved A user function that is used as a `symbolsRemoved` callback for the listener.
* @param functionSubscriptionClosed A user function that is used as a `subscriptionClosed` callback for the listener.
* @param userData User data, which is placed each time as a callback parameter when called from listener.
* @return The ObservableSubscriptionChangeListener's handle.
*
* @throws std::invalid_argument if functionSymbolsAdded or functionSymbolsRemoved or functionSubscriptionClosed is
* nullptr.
* @throws JavaException if something happened with the dxFeed API backend.
* @throws GraalException if something happened with the GraalVM.
*/
/* dxfg_observable_subscription_change_listener_t* */ JavaObjectHandle<dxfcpp::ObservableSubscriptionChangeListener>
create(/* dxfg_ObservableSubscriptionChangeListener_function_symbolsAdded */ void *functionSymbolsAdded,
/* dxfg_ObservableSubscriptionChangeListener_function_symbolsRemoved */ void *functionSymbolsRemoved,
/* dxfg_ObservableSubscriptionChangeListener_function_subscriptionClosed */ void *functionSubscriptionClosed,
void *userData);
} // namespace isolated::api::IsolatedObservableSubscriptionChangeListener

DXFCPP_END_NAMESPACE

DXFCXX_DISABLE_MSC_WARNINGS_POP()
90 changes: 89 additions & 1 deletion src/api/osub/ObservableSubscriptionChangeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,97 @@

DXFCPP_BEGIN_NAMESPACE

ObservableSubscriptionChangeListener::ObservableSubscriptionChangeListener() {
struct ObservableSubscriptionChangeListener::Impl {
static void onSymbolsAdded(graal_isolatethread_t * /* thread */, dxfg_symbol_list *symbols, void *userData) {
if (!symbols) {
return;
}

auto id = Id<ObservableSubscriptionChangeListener>::from(userData);
auto listener =
ApiContext::getInstance()->getManager<EntityManager<ObservableSubscriptionChangeListener>>()->getEntity(id);

if (!listener) {
return;
}

auto symbolList = SymbolWrapper::SymbolListUtils::fromGraalList(symbols);
listener->onSymbolsAdded_({symbolList.begin(), symbolList.end()});
}

static void onSymbolsRemoved(graal_isolatethread_t * /* thread */, dxfg_symbol_list *symbols, void *userData) {
if (!symbols) {
return;
}

auto id = Id<ObservableSubscriptionChangeListener>::from(userData);
auto listener =
ApiContext::getInstance()->getManager<EntityManager<ObservableSubscriptionChangeListener>>()->getEntity(id);

if (!listener) {
return;
}

auto symbolList = SymbolWrapper::SymbolListUtils::fromGraalList(symbols);
listener->onSymbolsRemoved_({symbolList.begin(), symbolList.end()});
}

static void onSubscriptionClosed(graal_isolatethread_t * /* thread */, void *userData) {
auto id = Id<ObservableSubscriptionChangeListener>::from(userData);
auto listener =
ApiContext::getInstance()->getManager<EntityManager<ObservableSubscriptionChangeListener>>()->getEntity(id);

if (!listener) {
return;
}

listener->onSubscriptionClosed_();
}
};

ObservableSubscriptionChangeListener::ObservableSubscriptionChangeListener(LockExternalConstructionTag)
: handle_{}, impl_{std::make_unique<ObservableSubscriptionChangeListener::Impl>()} {
}

ObservableSubscriptionChangeListener::~ObservableSubscriptionChangeListener() noexcept {
}

std::shared_ptr<ObservableSubscriptionChangeListener> ObservableSubscriptionChangeListener::create(
std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsAdded) {
auto listener = ObservableSubscriptionChangeListener::createShared();
auto id =
ApiContext::getInstance()->getManager<EntityManager<ObservableSubscriptionChangeListener>>()->registerEntity(
listener);

listener->handle_ = isolated::api::IsolatedObservableSubscriptionChangeListener::create(
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSymbolsAdded),
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSymbolsRemoved),
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSubscriptionClosed),
dxfcpp::bit_cast<void *>(id.getValue()));
listener->onSymbolsAdded_ += std::move(onSymbolsAdded);

return listener;
}

std::shared_ptr<ObservableSubscriptionChangeListener> ObservableSubscriptionChangeListener::create(
std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsAdded,
std::function<void(const std::unordered_set<SymbolWrapper> &symbols)> onSymbolsRemoved,
std::function<void()> onSubscriptionClosed) {
auto listener = ObservableSubscriptionChangeListener::createShared();
auto id =
ApiContext::getInstance()->getManager<EntityManager<ObservableSubscriptionChangeListener>>()->registerEntity(
listener);

listener->handle_ = isolated::api::IsolatedObservableSubscriptionChangeListener::create(
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSymbolsAdded),
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSymbolsRemoved),
dxfcpp::bit_cast<void *>(&ObservableSubscriptionChangeListener::Impl::onSubscriptionClosed),
dxfcpp::bit_cast<void *>(id.getValue()));
listener->onSymbolsAdded_ += std::move(onSymbolsAdded);
listener->onSymbolsRemoved_ += std::move(onSymbolsRemoved);
listener->onSubscriptionClosed_ += std::move(onSubscriptionClosed);

return listener;
}

DXFCPP_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>

#include <dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp>
#include <dxfeed_graal_cpp_api/isolated/api/osub/IsolatedObservableSubscriptionChangeListener.hpp>

DXFCPP_BEGIN_NAMESPACE

namespace isolated::api::IsolatedObservableSubscriptionChangeListener {

JavaObjectHandle<dxfcpp::ObservableSubscriptionChangeListener>
create(/* dxfg_ObservableSubscriptionChangeListener_function_symbolsAdded */ void *functionSymbolsAdded,
/* dxfg_ObservableSubscriptionChangeListener_function_symbolsRemoved */ void *functionSymbolsRemoved,
/* dxfg_ObservableSubscriptionChangeListener_function_subscriptionClosed */ void *functionSubscriptionClosed,
void *userData) {
if (!functionSymbolsAdded) {
throw std::invalid_argument(
"Unable to create ObservableSubscriptionChangeListener. The `functionSymbolsAdded` parameter is nullptr");
}

if (!functionSymbolsRemoved) {
throw std::invalid_argument(
"Unable to create ObservableSubscriptionChangeListener. The `functionSymbolsRemoved` parameter is nullptr");
}

if (!functionSubscriptionClosed) {
throw std::invalid_argument("Unable to create ObservableSubscriptionChangeListener. The "
"`functionSubscriptionClosed` parameter is nullptr");
}

return JavaObjectHandle<dxfcpp::ObservableSubscriptionChangeListener>(runGraalFunctionAndThrowIfNullptr(
dxfg_ObservableSubscriptionChangeListener_new,
dxfcpp::bit_cast<dxfg_ObservableSubscriptionChangeListener_function_symbolsAdded>(functionSymbolsAdded),
dxfcpp::bit_cast<dxfg_ObservableSubscriptionChangeListener_function_symbolsRemoved>(functionSymbolsRemoved),
dxfcpp::bit_cast<dxfg_ObservableSubscriptionChangeListener_function_subscriptionClosed>(
functionSubscriptionClosed),
userData));
}

} // namespace isolated::api::IsolatedObservableSubscriptionChangeListener

DXFCPP_END_NAMESPACE

0 comments on commit 5503703

Please sign in to comment.