Skip to content

Commit

Permalink
feat: gateway abstraction layer for bidirectional communication over ws
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-blasz committed Dec 12, 2024
1 parent 4055fde commit b450cb9
Show file tree
Hide file tree
Showing 15 changed files with 388 additions and 169 deletions.
6 changes: 0 additions & 6 deletions languages/cpp/src/shared/src/Accessor/Accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,4 @@ namespace FireboltSDK {
return Firebolt::Error::None;
}

Transport<WPEFramework::Core::JSON::IElement>* Accessor::GetTransport()
{
ASSERT(_transport != nullptr);
return _transport;
}

}
10 changes: 5 additions & 5 deletions languages/cpp/src/shared/src/Accessor/Accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Transport/Transport.h"
#include "Async/Async.h"
#include "Event/Event.h"
#include "Gateway/Gateway.h"
#include "Logger/Logger.h"

namespace FireboltSDK {
Expand Down Expand Up @@ -109,6 +110,7 @@ namespace FireboltSDK {
{
Firebolt::Error status = CreateTransport(_config.WsUrl.Value().c_str(), listener, _config.WaitTime.Value());
if (status == Firebolt::Error::None) {
Gateway::Instance().Configure(_transport);
Async::Instance().Configure(_transport);
status = CreateEventHandler();
}
Expand All @@ -118,16 +120,14 @@ namespace FireboltSDK {
Firebolt::Error Disconnect()
{
Firebolt::Error status = Firebolt::Error::None;
DestroyEventHandler();
Async::Dispose();
Gateway::Dispose();
status = DestroyTransport();
if (status == Firebolt::Error::None) {
Async::Dispose();
status = DestroyEventHandler();
}
return status;
}

Event& GetEventManager();
Transport<WPEFramework::Core::JSON::IElement>* GetTransport();

private:
Firebolt::Error CreateEventHandler();
Expand Down
1 change: 1 addition & 0 deletions languages/cpp/src/shared/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ file(GLOB SOURCES *.cpp)
add_library(${TARGET} ${FIREBOLT_LIBRARY_TYPE}
${SOURCES}
Logger/Logger.cpp
Gateway/Gateway.cpp
Transport/Transport.cpp
Accessor/Accessor.cpp
Event/Event.cpp
Expand Down
60 changes: 60 additions & 0 deletions languages/cpp/src/shared/src/Gateway/Gateway.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "Gateway.h"

namespace FireboltSDK {

Gateway* Gateway::_instance = nullptr;

Gateway& Gateway::Instance()
{
if (_instance == nullptr) {
_instance = new Gateway(new GatewayImpl());
ASSERT(_instance != nullptr);
}
return *_instance;
}

void Gateway::Dispose()
{
ASSERT(_instance != nullptr);
if (_instance != nullptr) {
delete _instance;
_instance = nullptr;
}
}

Gateway::Gateway(GatewayImpl *implementation)
: _implementation(implementation)
{
_instance = this;
}

Gateway::~Gateway()
{
delete _implementation;
}

void Gateway::Configure(Transport<WPEFramework::Core::JSON::IElement>* transport)
{
_implementation->Configure(transport);
}

}

79 changes: 79 additions & 0 deletions languages/cpp/src/shared/src/Gateway/Gateway.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#ifndef MODULE_NAME
#define MODULE_NAME OpenRPCNativeSDK
#endif
#include <core/core.h>
#include "error.h"

#include "Transport/Transport.h"

#include <functional>
#include <string>

#ifdef GATEWAY_BIDIRECTIONAL
#include "gateway_impl_bidi.h"
#else
#include "gateway_impl_unidi.h"
#endif

namespace FireboltSDK
{
using EventCallback = std::function<void(const std::string & /* eventName */, const JsonObject & /* parameters */, Firebolt::Error /* error */)>;

class Gateway
{
static Gateway *_instance;

GatewayImpl *_implementation;

private:
Gateway(GatewayImpl *implementation);

public:
Gateway(const Gateway&) = delete;
Gateway& operator=(const Gateway&) = delete;
~Gateway();

static Gateway& Instance();
static void Dispose();

void Configure(Transport<WPEFramework::Core::JSON::IElement>* transport);

template <typename RESPONSETYPE>
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, RESPONSETYPE &response)
{
return _implementation->Request(method, parameters, response);
}

template <typename RESPONSETYPE>
Firebolt::Error Subscribe(const string& event, const string& parameters, RESPONSETYPE& response)
{
return _implementation->Subscribe(event, parameters, response);
}

Firebolt::Error Unsubscribe(const string& event, const string& parameters)
{
return _implementation->Unsubscribe(event, parameters);
}

};
}

94 changes: 94 additions & 0 deletions languages/cpp/src/shared/src/Gateway/gateway_impl_bidi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#include "error.h"
#include "Accessor/Accessor.h"

#include <string>
#include <stdio.h>

namespace FireboltSDK
{

using EventCallback = std::function<void(const std::string & /* eventName */, const JsonObject & /* parameters */, Firebolt::Error /* error */)>;

class Client
{
Firebolt::Error Request(const std::string &method, const JsonObject &parameters)
{
return Firebolt::Error::None;
}
};

class Server
{
Firebolt::Error Subscribe(const std::string &eventName, const EventCallback &callback)
{
return Firebolt::Error::None;
}

Firebolt::Error Request(const std::string &methodName, const JsonObject &parameters)
{
return Firebolt::Error::None;
}

Firebolt::Error Notify()
{
return Firebolt::Error::None;
}
};

class Provider
{
// TBD
};

class GatewayImpl : public Gateway
{
Client _client;
Server _server;

void receive(const JsonObject &response)
{

}

public:
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, JsonObject &response)
{
return _client.Request(method, parameters);
}

virtual Firebolt::Error Subscribe(const std::string &eventName, const EventCallback &callback)
{
return _server.Subscribe(eventName, callback);
}

virtual Firebolt::Error Unsubsribe(const std::string &eventName)
{
return Firebolt::Error::None;
}

virtual Firebolt::Error Provide(const std::string &interfaceName, const Provider &provider)
{
return Firebolt::Error::None;
}
}
}

85 changes: 85 additions & 0 deletions languages/cpp/src/shared/src/Gateway/gateway_impl_unidi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#ifndef MODULE_NAME
#define MODULE_NAME OpenRPCNativeSDK
#endif
#include <core/core.h>
#include "error.h"

#include "Transport/Transport.h"

#include <string>
#include <stdio.h>

namespace FireboltSDK
{

using EventCallback = std::function<void(const std::string & /* eventName */, const JsonObject & /* parameters */, Firebolt::Error /* error */)>;

class GatewayImpl
{

Transport<WPEFramework::Core::JSON::IElement>* _transport;

public:
GatewayImpl()
{
}

public:

void Configure(Transport<WPEFramework::Core::JSON::IElement>* transport)
{
_transport = transport;
}

template <typename RESPONSETYPE>
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, RESPONSETYPE &response)
{
if (_transport == nullptr) {
return Firebolt::Error::NotConnected;
}
return _transport->Invoke(method, parameters, response);
}

template <typename RESPONSETYPE>
Firebolt::Error Subscribe(const string& event, const string& parameters, RESPONSETYPE& response)
{
if (_transport == nullptr) {
return Firebolt::Error::NotConnected;
}
return _transport->Subscribe(event, parameters, response);
}

Firebolt::Error Unsubscribe(const string& event, const string& parameters)
{
if (_transport == nullptr) {
return Firebolt::Error::NotConnected;
}
return _transport->Unsubscribe(event, parameters);
}

// virtual Firebolt::Error Provide(const std::string &interfaceName, const Provider &provider)
// {
// return Firebolt::Error::None;
// }
};
}

Loading

0 comments on commit b450cb9

Please sign in to comment.