Skip to content

Commit

Permalink
fix: Reverted PR 197 and 201
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinshahfws committed Aug 21, 2024
1 parent 75481e8 commit 093e41b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
42 changes: 29 additions & 13 deletions languages/cpp/src/shared/src/Event/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,35 @@ namespace FireboltSDK {
_transport->SetEventHandler(this);
}

uint32_t Event::Unsubscribe(const string& eventName, void* usercb)
Firebolt::Error Event::Unsubscribe(const string& eventName, void* usercb)
{
uint32_t status = Revoke(eventName, usercb);
Firebolt::Error status = Revoke(eventName, usercb);

if (status == FireboltSDKErrorNone) {
if (status == Firebolt::Error::None) {
if (_transport != nullptr) {

const string parameters("{\"listen\":false}");
status = _transport->Unsubscribe(eventName, parameters);
}
} else {
status = Firebolt::Error::None;
}
return ((status == FireboltSDKErrorInUse) ? FireboltSDKErrorNone: status);
return status;
}

uint32_t Event::ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) /* override */
Firebolt::Error Event::ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) /* override */
{
uint32_t result = FireboltSDKErrorGeneral;
Firebolt::Error result = Firebolt::Error::General;
Response response;
_transport->FromMessage((WPEFramework::Core::JSON::IElement*)&response, *jsonResponse);
if (response.Listening.IsSet() == true) {
result = FireboltSDKErrorNone;
result = Firebolt::Error::None;
enabled = response.Listening.Value();
}
return result;
}

uint32_t Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) /* override */
Firebolt::Error Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) /* override */
{
string response = jsonResponse->Result.Value();
_adminLock.Lock();
Expand Down Expand Up @@ -117,14 +119,14 @@ namespace FireboltSDK {
}
_adminLock.Unlock();

return FireboltSDKErrorNone;;
return Firebolt::Error::None;;
}

uint32_t Event::Revoke(const string& eventName, void* usercb)
Firebolt::Error Event::Revoke(const string& eventName, void* usercb)
{
uint32_t status = FireboltSDKErrorNone;
Firebolt::Error status = Firebolt::Error::None;
_adminLock.Lock();
EventMap::iterator eventIndex = _eventMap.find(eventName);
EventMap::iterator eventIndex = _eventMap.begin();
if (eventIndex != _eventMap.end()) {
CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb);
if (callbackIndex->second.state != State::EXECUTING) {
Expand All @@ -137,11 +139,25 @@ namespace FireboltSDK {
if (eventIndex->second.size() == 0) {
_eventMap.erase(eventIndex);
} else {
status = FireboltSDKErrorInUse;
status = Firebolt::Error::General;
}
}
_adminLock.Unlock();

return status;
}

void Event::Clear()
{
EventMap::iterator eventIndex = _eventMap.begin();
while (eventIndex != _eventMap.end()) {
CallbackMap::iterator callbackIndex = eventIndex->second.begin();
while (callbackIndex != eventIndex->second.end()) {
callbackIndex = eventIndex->second.erase(callbackIndex);
}
eventIndex = _eventMap.erase(eventIndex);
}
_adminLock.Unlock();
}

}
40 changes: 19 additions & 21 deletions languages/cpp/src/shared/src/Event/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace FireboltSDK {

class Event : public IEventHandler {
public:
typedef std::function<uint32_t(void*, const void*, const string& parameters)> DispatchFunction;
typedef std::function<Firebolt::Error(void*, const void*, const string& parameters)> DispatchFunction;
private:
enum State : uint8_t {
IDLE,
Expand Down Expand Up @@ -73,20 +73,20 @@ namespace FireboltSDK {

public:
template <typename RESULT, typename CALLBACK>
uint32_t Subscribe(const string& eventName, const CALLBACK& callback, void* usercb, const void* userdata)
Firebolt::Error Subscribe(const string& eventName, const CALLBACK& callback, void* usercb, const void* userdata)
{
JsonObject jsonParameters;
return Subscribe<RESULT, CALLBACK>(eventName, jsonParameters, callback, usercb, userdata);
}

template <typename RESULT, typename CALLBACK>
uint32_t Subscribe(const string& eventName, JsonObject& jsonParameters, const CALLBACK& callback, void* usercb, const void* userdata)
Firebolt::Error Subscribe(const string& eventName, JsonObject& jsonParameters, const CALLBACK& callback, void* usercb, const void* userdata)
{
uint32_t status = FireboltSDKErrorUnavailable;
Firebolt::Error status = Firebolt::Error::General;
if (_transport != nullptr) {

status = Assign<RESULT, CALLBACK>(eventName, callback, usercb, userdata);
if (status == FireboltSDKErrorNone) {
if (status == Firebolt::Error::None) {
Response response;

WPEFramework::Core::JSON::Variant Listen = true;
Expand All @@ -96,35 +96,33 @@ namespace FireboltSDK {

status = _transport->Subscribe<Response>(eventName, parameters, response);

if (status != FireboltSDKErrorNone) {
if (status != Firebolt::Error::None) {
Revoke(eventName, usercb);
} else if ((response.Listening.IsSet() == true) &&
(response.Listening.Value() == true)) {
status = FireboltSDKErrorNone;
} else {
status = FireboltSDKErrorNotSubscribed;
status = Firebolt::Error::None;
}
}
}

return status;
}

uint32_t Unsubscribe(const string& eventName, void* usercb);
Firebolt::Error Unsubscribe(const string& eventName, void* usercb);

private:
template <typename PARAMETERS, typename CALLBACK>
uint32_t Assign(const string& eventName, const CALLBACK& callback, void* usercb, const void* userdata)
Firebolt::Error Assign(const string& eventName, const CALLBACK& callback, void* usercb, const void* userdata)
{
uint32_t status = FireboltSDKErrorNone;
Firebolt::Error status = Firebolt::Error::General;
std::function<void(void* usercb, const void* userdata, void* parameters)> actualCallback = callback;
DispatchFunction implementation = [actualCallback](void* usercb, const void* userdata, const string& parameters) -> uint32_t {
DispatchFunction implementation = [actualCallback](void* usercb, const void* userdata, const string& parameters) -> Firebolt::Error {

WPEFramework::Core::ProxyType<PARAMETERS>* inbound = new WPEFramework::Core::ProxyType<PARAMETERS>();
*inbound = WPEFramework::Core::ProxyType<PARAMETERS>::Create();
(*inbound)->FromString(parameters);
actualCallback(usercb, userdata, static_cast<void*>(inbound));
return (FireboltSDKErrorNone);
return (Firebolt::Error::None);
};
CallbackData callbackData = {implementation, userdata, State::IDLE};

Expand All @@ -134,27 +132,27 @@ namespace FireboltSDK {
CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb);
if (callbackIndex == eventIndex->second.end()) {
eventIndex->second.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData));
} else {
// Already registered, no need to register again;
status = FireboltSDKErrorInUse;
status = Firebolt::Error::None;
}
} else {

CallbackMap callbackMap;
callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData));
_eventMap.emplace(std::piecewise_construct, std::forward_as_tuple(eventName), std::forward_as_tuple(callbackMap));
status = Firebolt::Error::None;

}

_adminLock.Unlock();
return status;
}
uint32_t Revoke(const string& eventName, void* usercb);
Firebolt::Error Revoke(const string& eventName, void* usercb);

private:
uint32_t ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) override;
uint32_t Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) override;

void Clear();
Firebolt::Error ValidateResponse(const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse, bool& enabled) override;
Firebolt::Error Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& jsonResponse) override;

private:
EventMap _eventMap;
WPEFramework::Core::CriticalSection _adminLock;
Expand Down

0 comments on commit 093e41b

Please sign in to comment.