Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware abstraction #62

Merged
merged 17 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"*.inc": "cpp"
"*.inc": "cpp",
"regex": "cpp",
"shared_mutex": "cpp"
}
}
4 changes: 2 additions & 2 deletions examples/seat-adjuster/src/SeatAdjusterApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -40,7 +40,7 @@ const auto STATUS_FAIL = 1;

SeatAdjusterApp::SeatAdjusterApp()
: VehicleApp(velocitas::IVehicleDataBrokerClient::createInstance("vehicledatabroker"),
velocitas::IPubSubClient::createInstance("localhost:1883", "SeatAdjusterApp"))
velocitas::IPubSubClient::createInstance("SeatAdjusterApp"))
, m_vehicleModel(std::make_shared<velocitas::Vehicle>()) {}

void SeatAdjusterApp::onStart() {
Expand Down
7 changes: 3 additions & 4 deletions examples/set-data-points/src/SetDataPointsApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -30,9 +30,8 @@ namespace example {
class SetDataPointsApp : public velocitas::VehicleApp {
public:
SetDataPointsApp()
: VehicleApp(
velocitas::IVehicleDataBrokerClient::createInstance("vehicledatabroker"),
velocitas::IPubSubClient::createInstance("localhost:1883", "SetDataPointsApp")) {}
: VehicleApp(velocitas::IVehicleDataBrokerClient::createInstance("vehicledatabroker"),
velocitas::IPubSubClient::createInstance("SetDataPointsApp")) {}

void onStart() override {
velocitas::logger().info("Setting data points!");
Expand Down
31 changes: 31 additions & 0 deletions sdk/include/sdk/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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
*/

#ifndef VEHICLE_APP_SDK_CONFIG_H
#define VEHICLE_APP_SDK_CONFIG_H

#include "middleware/Middleware.h"

namespace velocitas {

class Config {
kse3hi marked this conversation as resolved.
Show resolved Hide resolved
BjoernAtBosch marked this conversation as resolved.
Show resolved Hide resolved
public:
static Middleware& getMiddleware() { return Middleware::getInstance(); }
};

} // namespace velocitas

#endif // VEHICLE_APP_SDK_CONFIG_H
7 changes: 4 additions & 3 deletions sdk/include/sdk/IPubSubClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -30,8 +30,9 @@ namespace velocitas {
*/
class IPubSubClient {
public:
static std::shared_ptr<IPubSubClient> createInstance(const std::string& brokerUri,
const std::string& clientId);
static std::shared_ptr<IPubSubClient> createInstance(const std::string& clientId);
BjoernAtBosch marked this conversation as resolved.
Show resolved Hide resolved
[[deprecated]] static std::shared_ptr<IPubSubClient>
createInstance(const std::string& brokerUri, const std::string& clientId);

virtual ~IPubSubClient() = default;

Expand Down
14 changes: 13 additions & 1 deletion sdk/include/sdk/Utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -22,12 +22,24 @@

namespace velocitas {

/**
* @brief Get the value of the specified environment variable
*
* @param varName Name of the environment variable
* @param defaultValue Default if variable is not set
* @return std::string containing the value of the variable or the default value
*/
std::string getEnvVar(const std::string& varName, const std::string& defaultValue = "");

/**
* @brief Provides utility methods for handling strings.
*
*/
class StringUtils final {
public:
static std::string toLower(const std::string& str);
BjoernAtBosch marked this conversation as resolved.
Show resolved Hide resolved
static std::string toUpper(const std::string& str);

static std::string join(const std::vector<std::string>& stringVector,
const std::string& separator);

Expand Down
11 changes: 5 additions & 6 deletions sdk/include/sdk/grpc/VehicleDataBrokerClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -20,6 +20,7 @@
#include "sdk/vdb/IVehicleDataBrokerClient.h"

#include <memory>
#include <string>
#include <vector>

namespace velocitas {
Expand All @@ -33,8 +34,8 @@ class BrokerAsyncGrpcFacade;
*/
class VehicleDataBrokerClient : public IVehicleDataBrokerClient {
public:
explicit VehicleDataBrokerClient(const std::string& vdbAddress, std::string vdbAppId);
explicit VehicleDataBrokerClient(const std::string& vdbAppId);
explicit VehicleDataBrokerClient(const std::string& vdbAddress, std::string vdbServiceName);
explicit VehicleDataBrokerClient(const std::string& vdbserviceName);

~VehicleDataBrokerClient() override;

Expand All @@ -52,11 +53,9 @@ class VehicleDataBrokerClient : public IVehicleDataBrokerClient {
AsyncSubscriptionPtr_t<DataPointReply> subscribe(const std::string& query) override;

private:
static std::string getVdbEndpointAddress();

std::shared_ptr<BrokerAsyncGrpcFacade> m_asyncBrokerFacade;
std::string m_vdbAppId;
};

} // namespace velocitas

#endif // VEHICLE_APP_SDK_VEHICLEDATABROKERCLIENT_H
62 changes: 62 additions & 0 deletions sdk/include/sdk/middleware/Middleware.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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
*/

#ifndef VEHICLE_APP_SDK_MIDDLEWARE_MIDDLEWARE_H
#define VEHICLE_APP_SDK_MIDDLEWARE_MIDDLEWARE_H

#include <memory>
#include <string>
#include <unordered_map>

namespace velocitas {

class Middleware {
public:
enum class Type { UNKNOWN, NATIVE, DAPR };

static Middleware& getInstance() {
static std::unique_ptr<Middleware> singleton = instantiate();
return *singleton;
}

virtual Type getType() const = 0;

virtual void start() {}
virtual void waitUntilReady() {}
virtual void stop() {}

virtual std::string getServiceLocation(const std::string& serviceName) const = 0;
using Metadata = std::unordered_map<std::string, std::string>;
virtual Metadata getMetadata(const std::string& serviceName) const {
std::ignore = serviceName;
return Metadata{};
}

protected:
Middleware() = default;

private:
static std::unique_ptr<Middleware> instantiate();

Middleware(const Middleware&) = delete;
Middleware(Middleware&&) = delete;
Middleware& operator=(const Middleware&) = delete;
Middleware& operator=(Middleware&&) = delete;
};

} // namespace velocitas

#endif // VEHICLE_APP_SDK_MIDDLEWARE_MIDDLEWARE_H
4 changes: 2 additions & 2 deletions sdk/include/sdk/vdb/IVehicleDataBrokerClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -77,7 +77,7 @@ class IVehicleDataBrokerClient {
*
* @return std::shared_ptr<IVehicleDataBrokerClient>
*/
static std::shared_ptr<IVehicleDataBrokerClient> createInstance(const std::string& vdbAppId);
static std::shared_ptr<IVehicleDataBrokerClient> createInstance(const std::string& serviceName);

protected:
IVehicleDataBrokerClient() = default;
Expand Down
13 changes: 8 additions & 5 deletions sdk/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022 Robert Bosch GmbH
# Copyright (c) 2022-2023 Robert Bosch GmbH
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
Expand All @@ -15,10 +15,7 @@
set(TARGET_NAME "vehicle-app-sdk")

add_library(${TARGET_NAME}
sdk/vdb/DataPointBatch.cpp

sdk/VehicleApp.cpp
sdk/pubsub/MqttPubSubClient.cpp
sdk/Node.cpp
sdk/QueryBuilder.cpp
sdk/DataPoint.cpp
Expand All @@ -28,12 +25,18 @@ add_library(${TARGET_NAME}
sdk/Utils.cpp
sdk/Logger.cpp

sdk/dapr/DaprSupport.cpp
sdk/grpc/GrpcClient.cpp
sdk/grpc/AsyncGrpcFacade.cpp
sdk/grpc/BrokerAsyncGrpcFacade.cpp
sdk/grpc/GrpcDataPointValueProvider.cpp
sdk/grpc/VehicleDataBrokerClient.cpp

sdk/middleware/Middleware.cpp
sdk/middleware/DaprMiddleware.cpp
sdk/middleware/NativeMiddleware.cpp

sdk/pubsub/MqttPubSubClient.cpp
sdk/vdb/DataPointBatch.cpp
)

include_directories(
Expand Down
24 changes: 23 additions & 1 deletion sdk/src/sdk/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -17,10 +17,32 @@
#include "sdk/Utils.h"

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <sstream>

namespace velocitas {

std::string getEnvVar(const std::string& varName, const std::string& defaultValue) {
auto* const value = ::getenv(varName.c_str());
if (value != nullptr) {
return std::string(value);
}
return defaultValue;
}

std::string StringUtils::toLower(const std::string& str) {
std::string result{str};
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}

std::string StringUtils::toUpper(const std::string& str) {
std::string result{str};
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}

std::string StringUtils::join(const std::vector<std::string>& stringVector,
const std::string& separator) {
std::ostringstream oss;
Expand Down
17 changes: 9 additions & 8 deletions sdk/src/sdk/VehicleApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Robert Bosch GmbH
* Copyright (c) 2022-2023 Robert Bosch GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -16,10 +16,10 @@

#include "sdk/VehicleApp.h"

#include "sdk/Config.h"
#include "sdk/IPubSubClient.h"
#include "sdk/Logger.h"
#include "sdk/VehicleModelContext.h"
#include "sdk/dapr/DaprSupport.h"
#include "sdk/vdb/IVehicleDataBrokerClient.h"

#include <fmt/core.h>
Expand All @@ -40,12 +40,11 @@ VehicleApp::VehicleApp(std::shared_ptr<IVehicleDataBrokerClient> vdbClient,
}

void VehicleApp::run() {
m_pubSubClient->connect();

logger().info("Running App...");
Config::getMiddleware().start();
BjoernAtBosch marked this conversation as resolved.
Show resolved Hide resolved
Config::getMiddleware().waitUntilReady();

dapr::waitForSidecar();

m_pubSubClient->connect();
onStart();

// TODO: Fix busy waiting
Expand All @@ -55,10 +54,12 @@ void VehicleApp::run() {
}

void VehicleApp::stop() {
onStop();
logger().info("Stopping App...");

onStop();
m_pubSubClient->disconnect();
logger().info("Stopping App...");

Config::getMiddleware().stop();
}

AsyncSubscriptionPtr_t<std::string> VehicleApp::subscribeToTopic(const std::string& topic) {
Expand Down
Loading