Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 16 additions & 24 deletions apps/hello-cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,38 @@
#include <boost/asio/io_context.hpp>

#include "console_backend.hpp"
#include "logger.hpp"
#include "context_builder.hpp"
#include "launchdarkly/client_side/data_sources/detail/streaming_data_source.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate header + formatting lint are probably worth doing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted the headers, but my formatter doesn't seem to do anything.


#include <iostream>
#include <utility>

namespace net = boost::asio; // from <boost/asio.hpp>

using launchdarkly::ConsoleBackend;
using launchdarkly::ContextBuilder;
using launchdarkly::Logger;
using launchdarkly::LogLevel;
using launchdarkly::client_side::Client;
using launchdarkly::client_side::ConfigBuilder;
using launchdarkly::client_side::flag_manager::detail::FlagManager;
using launchdarkly::client_side::flag_manager::detail::FlagUpdater;

int main() {
Logger logger(std::make_unique<ConsoleBackend>("Hello"));
Logger logger(std::make_unique<ConsoleBackend>(LogLevel::kDebug, "Hello"));

net::io_context ioc;

char const* key = std::getenv("STG_SDK_KEY");
if (!key) {
std::cout << "Set environment variable STG_SDK_KEY to the sdk key\n";
return 1;
}
auto client =
launchdarkly::sse::Builder(ioc.get_executor(),
"https://stream-stg.launchdarkly.com/all")
.header("Authorization", key)
.receiver([&](launchdarkly::sse::Event ev) {
LD_LOG(logger, LogLevel::kInfo) << "event: " << ev.type();
LD_LOG(logger, LogLevel::kInfo)
<< "data: " << std::move(ev).take();
})
.logger([&](std::string msg) {
LD_LOG(logger, LogLevel::kDebug) << std::move(msg);
})
.build();

if (!client) {
LD_LOG(logger, LogLevel::kError) << "Failed to build client";
std::cout << "Set environment variable STG_SDK_KEY to the sdk key";
return 1;
}

client->run();
ioc.run();
Client client(ConfigBuilder(key).Build().value(),
ContextBuilder().kind("user", "ryan").build());

client.WaitForReadySync(std::chrono::seconds(30));

auto value = client.BoolVariation("my-boolean-flag", false);
LD_LOG(logger, LogLevel::kInfo) << "Value was: " << value;
}
24 changes: 24 additions & 0 deletions libs/client-sdk/include/launchdarkly/client_side/api.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#pragma once

#include <boost/asio/io_context.hpp>

#include <condition_variable>
#include <cstdint>
#include <memory>
#include <optional>
#include <thread>

#include <tl/expected.hpp>
#include "config/client.hpp"
#include "context.hpp"
#include "error.hpp"
#include "events/event_processor.hpp"
#include "launchdarkly/client_side/data_source.hpp"
#include "launchdarkly/client_side/data_sources/detail/data_source_status_manager.hpp"
#include "launchdarkly/client_side/flag_manager/detail/flag_manager.hpp"
#include "launchdarkly/client_side/flag_manager/detail/flag_updater.hpp"
#include "logger.hpp"
#include "value.hpp"

Expand Down Expand Up @@ -41,16 +48,33 @@ class Client {

Value JsonVariation(FlagKey const& key, Value default_value);

data_sources::IDataSourceStatusProvider& DataSourceStatus();

void WaitForReadySync(std::chrono::seconds timeout);

~Client();

private:
Value VariationInternal(FlagKey const& key, Value default_value);
void TrackInternal(std::string event_name,
std::optional<Value> data,
std::optional<double> metric_value);

bool initialized_;
std::mutex init_mutex_;
std::condition_variable init_waiter_;

data_sources::detail::DataSourceStatusManager status_manager_;
flag_manager::detail::FlagManager flag_manager_;
flag_manager::detail::FlagUpdater flag_updater_;

Logger logger_;
std::thread thread_;
boost::asio::io_context ioc_;
Context context_;
std::unique_ptr<events::IEventProcessor> event_processor_;
std::unique_ptr<IDataSource> data_source_;
std::thread run_thread_;
};

} // namespace launchdarkly::client_side
27 changes: 27 additions & 0 deletions libs/client-sdk/include/launchdarkly/client_side/connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

namespace launchdarkly::client_side {
// TODO: Can be moved to common.

/**
* Represents the connection of a listener.
* Disconnecting the connection will cause the listener to stop receiving
* events.
*/
class IConnection {
public:
/**
* Disconnect the listener and stop receiving events.
*/
virtual void Disconnect() = 0;

virtual ~IConnection() = default;
IConnection(IConnection const& item) = delete;
IConnection(IConnection&& item) = delete;
IConnection& operator=(IConnection const&) = delete;
IConnection& operator=(IConnection&&) = delete;

protected:
IConnection() = default;
};
} // namespace launchdarkly::client_side
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace launchdarkly::client_side {

class IDataSource {
public:
virtual void start() = 0;
virtual void close() = 0;
virtual void Start() = 0;
virtual void Close() = 0;

virtual ~IDataSource() = default;
IDataSource(IDataSource const& item) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "config/detail/built/service_endpoints.hpp"
#include "context.hpp"
#include "data/evaluation_result.hpp"
#include "launchdarkly/client_side/data_sources/data_source_status.hpp"

namespace launchdarkly::client_side {

Expand Down Expand Up @@ -48,9 +49,6 @@ class IDataSourceUpdateSink {
virtual void Init(std::unordered_map<std::string, ItemDescriptor> data) = 0;
virtual void Upsert(std::string key, ItemDescriptor item) = 0;

// We could add this if we want to support data source status.
// virtual void status(<something>)

IDataSourceUpdateSink(IDataSourceUpdateSink const& item) = delete;
IDataSourceUpdateSink(IDataSourceUpdateSink&& item) = delete;
IDataSourceUpdateSink& operator=(IDataSourceUpdateSink const&) = delete;
Expand Down
Loading