Add convenience macro to RMI
authorSangwan Kwon <sangwan.kwon@samsung.com>
Fri, 20 Dec 2019 06:56:28 +0000 (15:56 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 23 Dec 2019 02:42:16 +0000 (11:42 +0900)
The purpose of these macros(EXPOSE, REMOTE_METHOD) is to allow
users to use RMI only with member function type.

- server side -
EXPOSE(gateway, this, &Vist::query);

- client side -
auto query = REMOTE_METHOD(remote, &Vist::query);
auto rows = query.invoke<Rows>(statement);

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/client/query.cpp
src/vist/rmi/gateway.hpp
src/vist/rmi/remote.hpp
src/vist/rmi/tests/rmi.cpp
src/vist/service/vist.cpp

index 6c8ebc651a04e49d63440d80d45067d070854165..5b1cbed63bb78bb63a120f423b230257f85ff552 100644 (file)
@@ -30,7 +30,8 @@ Rows Query::Execute(const std::string& statement)
        INFO(VIST_CLIENT) << "Query execution: " << statement;
        rmi::Remote remote(SOCK_ADDR);
 
-       return remote.invoke<Rows>("Vist::query", statement);
+       auto query = REMOTE_METHOD(remote, &Vist::query);
+       return query.invoke<Rows>(statement);
 }
 
 } // namespace vist
index b8a347b0132fca6dfd2f9bca56b65fc6cfc78853..69a66769f1439db5bc8eede3417bc2aa0df338ad 100644 (file)
@@ -22,6 +22,8 @@
 
 #pragma once
 
+#define EXPOSE(gateway, object, function) gateway.expose(object, #function, function)
+
 #include <vist/klass/functor.hpp>
 
 #include <memory>
index 9a1206a7f6c74407e751a88fef7ecad9228acfd6..4dd64f41a34cc72c5f6a3e5b639ce53bb4163fec 100644 (file)
@@ -28,6 +28,8 @@
 #include <string>
 #include <memory>
 
+#define REMOTE_METHOD(remote, method) vist::rmi::Remote::Method {remote, #method}
+
 namespace vist {
 namespace rmi {
 
@@ -47,6 +49,17 @@ public:
        template<typename R, typename... Args>
        R invoke(const std::string& name, Args&&... args);
 
+       struct Method {
+               Remote& remote;
+               std::string name;
+
+               template<typename R, typename... Args>
+               R invoke(Args&&... args)
+               {
+                       return remote.invoke<R>(name, std::forward<Args>(args)...);
+               }
+       };
+
 private:
        Message request(Message& message);
 
index 250bed5e9a6fc48eb716f4b2fb9992af98a7d375..da5beb5b4df246e2af84288b3859576a5f021486 100644 (file)
@@ -66,8 +66,9 @@ TEST(RmiTests, positive)
        gateway.expose(foo, "Foo::setName", &Foo::setName);
        gateway.expose(foo, "Foo::getName", &Foo::getName);
 
+       // convenience macro
        auto bar = std::make_shared<Bar>();
-       gateway.expose(bar, "Bar::plusTwo", &Bar::plusTwo);
+       EXPOSE(gateway, bar, &Bar::plusTwo);
 
        auto client = std::thread([&]() {
                // caller-side
@@ -80,7 +81,9 @@ TEST(RmiTests, positive)
                std::string name = remote.invoke<std::string>("Foo::getName");
                EXPECT_EQ(name, param);
 
-               int num = remote.invoke<int>("Bar::plusTwo", 3);
+               // convenience macro
+               auto plusTwo = REMOTE_METHOD(remote, &Bar::plusTwo);
+               int num = plusTwo.invoke<int>(3);
                EXPECT_EQ(num, 5);
 
                gateway.stop();
index 8978d042106673c608b646a6443178c5cf333cf8..be4b68cca1471eea4149fa1247671837fc84f020 100644 (file)
@@ -39,7 +39,7 @@ void Vist::start()
        INFO(VIST) << "Vist daemon starts.";
        rmi::Gateway gateway(SOCK_ADDR);
 
-       gateway.expose(this, "Vist::query", &Vist::query);
+       EXPOSE(gateway, this, &Vist::query);
        gateway.start();
 }