Cosmetic improvements.
[invirt/third/libt4.git] / rsm_client.h
index 3219179..06ca5a6 100644 (file)
@@ -1,11 +1,8 @@
 #ifndef rsm_client_h
 #define rsm_client_h
 
-#include "rpc.h"
+#include "types.h"
 #include "rsm_protocol.h"
-#include <string>
-#include <vector>
-
 
 //
 // rsm client interface.
 
 class rsm_client {
     protected:
-        std::string primary;
-        std::vector<std::string> known_mems;
-        pthread_mutex_t rsm_client_mutex;
-        void primary_failure();
-        bool init_members();
+        string primary;
+        vector<string> known_mems;
+        mutex rsm_client_mutex;
+        void primary_failure(lock & rsm_client_mutex_lock);
+        bool init_members(lock & rsm_client_mutex_lock);
+        rsm_protocol::status invoke(unsigned int proc, string & rep, const string & req);
+        template<class R> int call_m(unsigned int proc, R & r, const marshall & req);
     public:
-        rsm_client(std::string dst);
-        rsm_protocol::status invoke(int proc, std::string req, std::string &rep);
-
-        template<class R, class A1>
-            int call(unsigned int proc, const A1 & a1, R &r);
-
-        template<class R, class A1, class A2>
-            int call(unsigned int proc, const A1 & a1, const A2 & a2, R &r);
-
-        template<class R, class A1, class A2, class A3>
-            int call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3,
-                    R &r);
-
-        template<class R, class A1, class A2, class A3, class A4>
-            int call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3,
-                    const A4 & a4, R &r);
+        rsm_client(string dst);
 
-        template<class R, class A1, class A2, class A3, class A4, class A5>
-            int call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3,
-                    const A4 & a4, const A5 & a5, R &r);
-    private:
-        template<class R> int call_m(unsigned int proc, marshall &req, R &r);
-};
-
-template<class R>
-int rsm_client::call_m(unsigned int proc, marshall &req, R &r) {
-       std::string rep;
-        std::string res;
-       int intret = invoke(proc, req.str(), rep);
-        VERIFY( intret == rsm_client_protocol::OK );
-        unmarshall u(rep);
-       u >> intret;
-       if (intret < 0) return intret;
-        u >> res;
-        if (!u.okdone()) {
-                fprintf(stderr, "rsm_client::call_m: failed to unmarshall the reply.\n"
-                       "You probably forgot to set the reply string in "
-                       "rsm::client_invoke, or you may call RPC 0x%x with wrong return "
-                       "type\n", proc);
-                VERIFY(0);
-               return rpc_const::unmarshal_reply_failure;
-        }
-        unmarshall u1(res);
-        u1 >> r;
-       if(!u1.okdone()) {
-                fprintf(stderr, "rsm_client::call_m: failed to unmarshall the reply.\n"
-                       "You are probably calling RPC 0x%x with wrong return "
-                       "type.\n", proc);
-                VERIFY(0);
-               return rpc_const::unmarshal_reply_failure;
+        template<class P, class R, class ...Args>
+        int call(rpc_protocol::proc_t<P> proc, R & r, const Args & ...a1) {
+            static_assert(is_valid_call<P, R, Args...>::value, "RSM method invoked with incorrect argument types");
+            return call_m(proc.id, r, marshall(a1...));
         }
-       return intret;
-}
-
-template<class R, class A1>
-int rsm_client::call(unsigned int proc, const A1 & a1, R & r) {
-    marshall m;
-    m << a1;
-    return call_m(proc, m, r);
-}
-
-template<class R, class A1, class A2>
-int rsm_client::call(unsigned int proc, const A1 & a1, const A2 & a2, R & r) {
-    marshall m;
-    m << a1;
-    m << a2;
-    return call_m(proc, m, r);
-}
-
-template<class R, class A1, class A2, class A3>
-int rsm_client::call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3, R & r) {
-    marshall m;
-    std::string rep;
-    std::string res;
-    m << a1;
-    m << a2;
-    m << a3;
-    return call_m(proc, m, r);
-}
+};
 
-template<class R, class A1, class A2, class A3, class A4>
-int rsm_client::call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3, const A4 & a4, R & r) {
-    marshall m;
-    std::string rep;
-    std::string res;
-    m << a1;
-    m << a2;
-    m << a3;
-    m << a4;
-    return call_m(proc, m, r);
+inline string hexify(const string & s) {
+    string bytes;
+    for (char ch : s) {
+        bytes.push_back("0123456789abcdef"[(uint8_t)ch >> 4]);
+        bytes.push_back("0123456789abcdef"[(uint8_t)ch & 15]);
+    }
+    return bytes;
 }
 
-template<class R, class A1, class A2, class A3, class A4, class A5>
-int rsm_client::call(unsigned int proc, const A1 & a1, const A2 & a2, const A3 & a3, const A4 & a4, const A5 & a5, R & r) {
-    marshall m;
-    std::string rep;
-    std::string res;
-    m << a1;
-    m << a2;
-    m << a3;
-    m << a4;
-    m << a5;
-    return call_m(proc, m, r);
+template<class R>
+int rsm_client::call_m(unsigned int proc, R & r, const marshall & req) {
+    string rep;
+    int intret = invoke(proc, rep, req.content());
+    VERIFY( intret == rsm_client_protocol::OK );
+    unmarshall u(rep, false, intret);
+    if (intret < 0) return intret;
+    string res;
+    u >> res;
+    if (!u.okdone()) {
+        LOG("failed to unmarshall the reply.");
+        LOG("You probably forgot to set the reply string in " <<
+            "rsm::client_invoke, or you may have called RPC " <<
+            "0x" << hex << proc << " with the wrong return type");
+        LOG("here's what I got: \"" << hexify(rep) << "\"");
+        VERIFY(0);
+        return rpc_protocol::unmarshall_reply_failure;
+    }
+    if(!unmarshall(res, false, r).okdone()) {
+        LOG("failed to unmarshall the reply.");
+        LOG("You are probably calling RPC 0x" << hex << proc <<
+            " with the wrong return type.");
+        LOG("here's what I got: \"" << hexify(res) << "\"");
+        VERIFY(0);
+        return rpc_protocol::unmarshall_reply_failure;
+    }
+    return intret;
 }
 
 #endif