diff --git a/example/bin/main.cpp b/example/bin/main.cpp index 664243e..0a620b3 100644 --- a/example/bin/main.cpp +++ b/example/bin/main.cpp @@ -3,8 +3,8 @@ #include #include #include +#include -#include "Types.h" #include "WebSocketClient.h" std::atomic continueRunning = true; @@ -25,19 +25,19 @@ int main() { constexpr int FRAME_SIZE = 1024; - WebSocketClient webSocketClient{ - ExecutionContext{"localhost", 8080, "/"}, - [](const auto& result) { return WorkflowResult{result}; } + WebSocketClient webSocketClient{ + SimpleWebSocket::ExecutionContext{"localhost", 8080, "/"}, + [](const auto& result) { return SimpleWebSocket::WorkflowResult{result}; } }; - Workflow workflow{ + SimpleWebSocket::Workflow workflow{ [&, client = std::move(webSocketClient)]() { return client.start(continueRunning); }, [](const auto&) { std::cout << "Workflow completed successfully" << std::endl; }, - [](const Failure &failure) { + [](const SimpleWebSocket::Failure &failure) { std::cout << failure << std::endl; std::this_thread::sleep_for(1s); } diff --git a/example/cmake/poco.cmake b/example/cmake/poco.cmake index ba74236..4b97e1b 100644 --- a/example/cmake/poco.cmake +++ b/example/cmake/poco.cmake @@ -2,7 +2,7 @@ FetchContent_Declare( poco GIT_SHALLOW TRUE GIT_REPOSITORY "https://github.com/pocoproject/poco.git" - GIT_TAG "poco-1.11.0-release" + GIT_TAG "poco-1.12.5-release" ) FetchContent_MakeAvailable(poco) FetchContent_GetProperties(poco) diff --git a/example/cmake/simple_websocket.cmake b/example/cmake/simple_websocket.cmake index afdf5e4..c2a9e97 100644 --- a/example/cmake/simple_websocket.cmake +++ b/example/cmake/simple_websocket.cmake @@ -2,7 +2,7 @@ FetchContent_Declare( simple_websocket GIT_SHALLOW TRUE GIT_REPOSITORY https://github.com/abedra/simple_websocket - GIT_TAG v0.0.6 + GIT_TAG v0.0.10 CONFIGURE_COMMAND "" BUILD_COMMAND "") FetchContent_GetProperties(simple_websocket) diff --git a/example/src/Types.h b/example/src/Types.h deleted file mode 100644 index 6b2a3b8..0000000 --- a/example/src/Types.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include - -template struct visitor : As... { using As::operator()...; }; -template visitor(As...) -> visitor; - -struct ExecutionContext final { - ExecutionContext(std::string host, unsigned short port, std::string uri) - : host_(std::move(host)) - , port_(port) - , uri_(std::move(uri)) - { } - - [[nodiscard]] const std::string &host() const { - return host_; - } - - [[nodiscard]] Poco::UInt16 port() const { - return port_; - } - - [[nodiscard]] const std::string &uri() const { - return uri_; - } - -private: - std::string host_; - Poco::UInt16 port_; - std::string uri_; -}; - -struct Failure final { - explicit Failure(std::string value) : value_(std::move(value)) { } - - friend std::ostream &operator<<(std::ostream &os, const Failure &failure) { - os << failure.value_; - return os; - } - - [[nodiscard]] const std::string &value() const { - return value_; - } - -private: - std::string value_; -}; - -struct WorkflowResult final { - explicit WorkflowResult(const std::monostate &unit) : value_(unit) { } - explicit WorkflowResult(const Failure &f) : value_(f) { } - explicit WorkflowResult(std::variant value) : value_(std::move(value)) { } - - [[nodiscard]] const std::variant &value() const { - return value_; - } - - [[nodiscard]] bool complete() const { - return std::holds_alternative(value_); - } - - template - [[nodiscard]] R match(const std::function aFn, const std::function uFn) { - return std::visit(visitor{ - [&aFn](const Failure &a) { return aFn(a); }, - [&uFn](const std::monostate &u){ return uFn(u); } - }, value_); - } - -private: - std::variant value_; -}; - -struct Workflow final { - explicit Workflow(std::function runFn, - std::function successFn, - std::function recoveryFn) - : runFn_(std::move(runFn)) - , successFn_(std::move(successFn)) - , recoveryFn_(std::move(recoveryFn)) - { } - - void runUntilCancelled() { - WorkflowResult workflowResult = runFn_(); - workflowResult.template match(recoveryFn_, successFn_); - - while (!workflowResult.complete()) { - workflowResult = runFn_(); - workflowResult.template match(recoveryFn_, successFn_); - } - } - -private: - const std::function runFn_; - const std::function successFn_; - const std::function recoveryFn_; -}; \ No newline at end of file diff --git a/example/src/WebSocketClient.h b/example/src/WebSocketClient.h index abd7e5a..4dcf1e6 100644 --- a/example/src/WebSocketClient.h +++ b/example/src/WebSocketClient.h @@ -1,17 +1,17 @@ #pragma once -#include "Types.h" +#include #include "ExampleFrameParser.h" -template +template struct WebSocketClient final { - explicit WebSocketClient(ExecutionContext executionContext, - std::function&)> resultFn) + explicit WebSocketClient(SimpleWebSocket::ExecutionContext executionContext, + std::function&)> resultFn) : executionContext_(std::move(executionContext)) , resultFn_(resultFn) { } - [[nodiscard]] A start(std::atomic &continueRunning) const { + [[nodiscard]] SimpleWebSocket::WorkflowResult start(std::atomic &continueRunning) const { ExampleFrameParser frameParser; SimpleWebSocket::MessageParser> parser{ std::make_unique(frameParser) @@ -31,19 +31,19 @@ struct WebSocketClient final { SimpleWebSocket::Message message = SimpleWebSocket::Poco::fromPoco(flags, result.data(), static_cast(result.size())); const std::variant &parseResult = parser.parse(message); if (std::holds_alternative(parseResult)) { - return resultFn_(Failure{"WebSocket connection closed"}); + return resultFn_(SimpleWebSocket::Failure{"WebSocket connection closed"}); } std::cout << std::get(parseResult) << std::endl; } } catch (const std::exception &e) { - return resultFn_(Failure{e.what()}); + return resultFn_(SimpleWebSocket::Failure{e.what()}); } return resultFn_(std::monostate()); } private: - ExecutionContext executionContext_; - std::function&)> resultFn_; + SimpleWebSocket::ExecutionContext executionContext_; + std::function&)> resultFn_; }; \ No newline at end of file