#include "exposer.hpp"
+#include <vist/exception.hpp>
#include <vist/transport/message.hpp>
#include <vist/transport/server.hpp>
std::string function = message.signature;
auto iter = exposer.functorMap.find(function);
if (iter == exposer.functorMap.end())
- THROW(ErrCode::RuntimeError) << "Faild to find function.";
+ THROW(ErrCode::ProtocolBroken) << "Not found function: " << function;
DEBUG(VIST) << "Remote method invokation: " << function;
#pragma once
+#include <vist/exception.hpp>
#include <vist/transport/message.hpp>
#include <string>
message.enclose(std::forward<Args>(args)...);
Message reply = this->request(message);
+ if (reply.error())
+ THROW(ErrCode::RuntimeError) << reply.what();
+
R result;
reply.disclose(result);
/*
- * Copyright (c) 2018-present Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License
*/
+/*
+ * @file rmi.cpp
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @brief Testcases of rmi.
+ */
+
#include <vist/rmi/exposer.hpp>
#include <vist/rmi/remote.hpp>
std::string name;
};
+struct Bar {
+ int plusTwo(int number)
+ {
+ return number + 2;
+ }
+};
+
TEST(RmiTests, positive)
{
std::string sockPath = ("/tmp/test-exposer");
exposer.expose(foo, "Foo::setName", &Foo::setName);
exposer.expose(foo, "Foo::getName", &Foo::getName);
+ auto bar = std::make_shared<Bar>();
+ exposer.expose(bar, "Bar::plusTwo", &Bar::plusTwo);
+
auto client = std::thread([&]() {
// caller-side
Remote remote(sockPath);
std::string name = remote.invoke<std::string>("Foo::getName");
EXPECT_EQ(name, param);
+ int num = remote.invoke<int>("Bar::plusTwo", 3);
+ EXPECT_EQ(num, 5);
+
+ exposer.stop();
+ });
+
+ exposer.start();
+
+ if (client.joinable())
+ client.join();
+}
+
+TEST(RmiTests, not_exist_method)
+{
+ std::string sockPath = ("/tmp/test-exposer");
+
+ // exposer-side
+ Exposer exposer(sockPath);
+
+ auto client = std::thread([&]() {
+ // caller-side
+ Remote remote(sockPath);
+
+ std::string param = "RMI-TEST";
+
+ bool rasied = false;
+ try {
+ remote.invoke<bool>("Foo::NotExist", param);
+ } catch (const std::exception& e) {
+ rasied = true;
+ }
+ EXPECT_TRUE(rasied);
+
exposer.stop();
});
this->buffer.resize(this->header.length);
}
+bool Message::success() const noexcept
+{
+ return !error();
+}
+
+bool Message::error() const noexcept
+{
+ return this->header.type == Type::Error;
+}
+
+std::string Message::what() const noexcept
+{
+ return this->signature;
+}
+
std::size_t Message::size(void) const noexcept
{
return this->header.length;
template<typename... Args>
void disclose(Args&... args);
+ bool success() const noexcept;
+ bool error() const noexcept;
+ std::string what() const noexcept;
+
std::size_t size(void) const noexcept;
void resize(std::size_t size);
std::vector<unsigned char>& getBuffer(void) noexcept;
void Protocol::Async::process(const Task& task)
{
- auto result = task(message);
- /// catch
- this->message = result;
+ bool raised = false;
+ std::string errMsg;
+ auto onError = [&raised, &errMsg](const std::string& message) {
+ ERROR(VIST) << "Failed to process task: " << message;
+ raised = true;
+ errMsg = message;
+ };
+
+ try {
+ /// Process dispatched task.
+ auto result = task(message);
+ this->message = result;
+ } catch (const vist::Exception<ErrCode>& e) {
+ onError(e.what());
+ } catch (const std::exception& e) {
+ onError(e.what());
+ }
+
+ if (raised)
+ this->message = Message(Message::Type::Error, errMsg);
+
auto self = shared_from_this();
const auto& headerBuffer = boost::asio::buffer(&this->message.header,
sizeof(Message::Header));
if (written != self->message.size())
THROW(ErrCode::ProtocolBroken) << "Failed to send message content.";
- // Re-dispatch for next request.
+ /// Re-dispatch for next request.
self->dispatch(task);
};