Skip to content

Commit

Permalink
simplified logging in provider and simplified Result type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Sep 26, 2023
1 parent b6889b1 commit 4e57d2d
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 143 deletions.
2 changes: 0 additions & 2 deletions examples/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ int main(int argc, char** argv) {
client.makeResourceHandle(g_address, g_provider_id,
alpha::UUID::from_string(g_resource.c_str()));

resource.sayHello();

int32_t result;
resource.computeSum(32, 54, &result);

Expand Down
5 changes: 0 additions & 5 deletions include/alpha/ResourceHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ class ResourceHandle {
*/
operator bool() const;

/**
* @brief Sends an RPC to the resource to make it print a hello message.
*/
void sayHello() const;

/**
* @brief Requests the target resource to compute the sum of two numbers.
* If result is null, it will be ignored. If req is not null, this call
Expand Down
99 changes: 96 additions & 3 deletions include/alpha/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef __ALPHA_RESULT_HPP
#define __ALPHA_RESULT_HPP

#include <alpha/Exception.hpp>
#include <string>

namespace alpha {
Expand Down Expand Up @@ -79,6 +80,53 @@ class Result {
return m_value;
}

/**
* @brief Value if the request succeeded,
* throws otherwise.
*/
const T& valueOrThrow() const & {
check();
return m_value;
}

/**
* @brief Value if the request succeeded,
* throws otherwise.
*/
T&& valueOrThrow() && {
check();
return std::move(m_value);
}

/**
* @brief Execute a function on the value
* if the value is present, otherwise throws
* an Exception.
*/
template<typename F>
decltype(auto) andThen(F&& f) const & {
return std::forward<F>(f)(valueOrThrow());
}

/**
* @brief Execute a function on the value
* if the value is present, otherwise throws
* an Exception.
*/
template<typename F>
decltype(auto) andThen(F&& f) && {
return std::forward<F>(f)(valueOrThrow());
}

/**
* @brief Throw an Exception if the Result
* contains an error.
*/
void check() const {
if(!m_success)
throw Exception(m_error);
}

/**
* @brief Serialization function for Thallium.
*
Expand All @@ -88,8 +136,11 @@ class Result {
template<typename Archive>
void serialize(Archive& a) {
a & m_success;
a & m_error;
a & m_value;
if(m_success) {
a & m_value;
} else {
a & m_error;
}
}

private:
Expand Down Expand Up @@ -134,6 +185,31 @@ class Result<std::string> {
return m_content;
}

const std::string& valueOrThrow() const & {
check();
return m_content;
}

std::string&& valueOrThrow() && {
check();
return std::move(m_content);
}

template<typename F>
decltype(auto) andThen(F&& f) const & {
return std::forward<F>(f)(valueOrThrow());
}

template<typename F>
decltype(auto) andThen(F&& f) && {
return std::forward<F>(f)(valueOrThrow());
}

void check() const {
if(!m_success)
throw Exception(m_content);
}

template<typename Archive>
void serialize(Archive& a) {
a & m_success;
Expand Down Expand Up @@ -181,10 +257,27 @@ class Result<bool> {
return m_success;
}

bool valueOrThrow() const {
check();
return true;
}

template<typename F>
decltype(auto) andThen(F&& f) const {
check();
return std::forward<F>(f)();
}

void check() const {
if(!m_success)
throw Exception(m_error);
}

template<typename Archive>
void serialize(Archive& a) {
a & m_success;
a & m_error;
if(!m_success)
a & m_error;
}

private:
Expand Down
18 changes: 4 additions & 14 deletions src/Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ UUID Admin::createResource(const std::string& address,
auto endpoint = self->m_engine.lookup(address);
auto ph = tl::provider_handle(endpoint, provider_id);
Result<UUID> result = self->m_create_resource.on(ph)(token, resource_type, resource_config);
if(not result.success()) {
throw Exception(result.error());
}
return result.value();
return std::move(result).valueOrThrow();
}

UUID Admin::openResource(const std::string& address,
Expand All @@ -60,10 +57,7 @@ UUID Admin::openResource(const std::string& address,
auto endpoint = self->m_engine.lookup(address);
auto ph = tl::provider_handle(endpoint, provider_id);
Result<UUID> result = self->m_open_resource.on(ph)(token, resource_type, resource_config);
if(not result.success()) {
throw Exception(result.error());
}
return result.value();
return std::move(result).valueOrThrow();
}

void Admin::closeResource(const std::string& address,
Expand All @@ -73,9 +67,7 @@ void Admin::closeResource(const std::string& address,
auto endpoint = self->m_engine.lookup(address);
auto ph = tl::provider_handle(endpoint, provider_id);
Result<bool> result = self->m_close_resource.on(ph)(token, resource_id);
if(not result.success()) {
throw Exception(result.error());
}
result.check();
}

void Admin::destroyResource(const std::string& address,
Expand All @@ -85,9 +77,7 @@ void Admin::destroyResource(const std::string& address,
auto endpoint = self->m_engine.lookup(address);
auto ph = tl::provider_handle(endpoint, provider_id);
Result<bool> result = self->m_destroy_resource.on(ph)(token, resource_id);
if(not result.success()) {
throw Exception(result.error());
}
result.check();
}

void Admin::shutdownServer(const std::string& address) const {
Expand Down
8 changes: 2 additions & 6 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ ResourceHandle Client::makeResourceHandle(
auto endpoint = self->m_engine.lookup(address);
auto ph = tl::provider_handle(endpoint, provider_id);
Result<bool> result;
result.success() = true;
if(check) {
result = self->m_check_resource.on(ph)(resource_id);
}
if(result.success()) {
return result.andThen([&]() {
auto resource_impl = std::make_shared<ResourceHandleImpl>(self, std::move(ph), resource_id);
return ResourceHandle(resource_impl);
} else {
throw Exception(result.error());
return ResourceHandle(nullptr);
}
});
}

std::string Client::getConfig() const {
Expand Down
2 changes: 0 additions & 2 deletions src/ClientImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ class ClientImpl {

tl::engine m_engine;
tl::remote_procedure m_check_resource;
tl::remote_procedure m_say_hello;
tl::remote_procedure m_compute_sum;

ClientImpl(const tl::engine& engine)
: m_engine(engine)
, m_check_resource(m_engine.define("alpha_check_resource"))
, m_say_hello(m_engine.define("alpha_say_hello").disable_response())
, m_compute_sum(m_engine.define("alpha_compute_sum"))
{}

Expand Down
Loading

0 comments on commit 4e57d2d

Please sign in to comment.