X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/eeab3e6cade87c1fe0a5f3d93522e12ccb9ec2ab..5bb7eb131773abd9e642f83629528b4c4e706b2e:/rsm_client.cc diff --git a/rsm_client.cc b/rsm_client.cc index b68ef0c..310d1ad 100644 --- a/rsm_client.cc +++ b/rsm_client.cc @@ -1,53 +1,40 @@ +#include "types.h" #include "rsm_client.h" -#include #include -#include #include #include -#include "lang/verify.h" -#include "lock.h" -#include "tprintf.h" -rsm_client::rsm_client(std::string dst) { +rsm_client::rsm_client(string dst) : primary(dst) { LOG("create rsm_client"); - std::vector mems; - - sockaddr_in dstsock; - make_sockaddr(dst.c_str(), &dstsock); - primary = dst; - - { - lock ml(rsm_client_mutex); - VERIFY (init_members()); - } + lock ml(rsm_client_mutex); + VERIFY (init_members(ml)); LOG("rsm_client: done"); } -// Assumes caller holds rsm_client_mutex -void rsm_client::primary_failure() { +void rsm_client::primary_failure(lock &) { primary = known_mems.back(); known_mems.pop_back(); } -rsm_protocol::status rsm_client::invoke(unsigned int proc, std::string &rep, const std::string &req) { - int ret = 0; +rsm_protocol::status rsm_client::invoke(unsigned int proc, string &rep, const string &req) { lock ml(rsm_client_mutex); while (1) { - LOG("rsm_client::invoke proc " << std::hex << proc << " primary " << primary); + LOG("rsm_client::invoke proc " << hex << proc << " primary " << primary); handle h(primary); ml.unlock(); rpcc *cl = h.safebind(); + auto ret = rsm_client_protocol::OK; if (cl) - ret = cl->call_timeout(rsm_client_protocol::invoke, rpcc::to(5000), rep, proc, req); + ret = (rsm_client_protocol::status)cl->call_timeout(rsm_client_protocol::invoke, rpcc::to(5000), rep, proc, req); ml.lock(); if (!cl) goto prim_fail; - LOG("rsm_client::invoke proc " << std::hex << proc << " primary " << primary << " ret " << std::dec << ret); + LOG("rsm_client::invoke proc " << hex << proc << " primary " << primary << " ret " << dec << ret); if (ret == rsm_client_protocol::OK) - break; + return rsm_protocol::OK; if (ret == rsm_client_protocol::BUSY) { LOG("rsm is busy " << primary); sleep(3); @@ -55,29 +42,27 @@ rsm_protocol::status rsm_client::invoke(unsigned int proc, std::string &rep, con } if (ret == rsm_client_protocol::NOTPRIMARY) { LOG("primary " << primary << " isn't the primary--let's get a complete list of mems"); - if (init_members()) + if (init_members(ml)) continue; } prim_fail: - LOG("primary " << primary << " failed ret " << std::dec << ret); - primary_failure(); + LOG("primary " << primary << " failed ret " << dec << ret); + primary_failure(ml); LOG("rsm_client::invoke: retry new primary " << primary); } - return ret; } -bool rsm_client::init_members() { +bool rsm_client::init_members(lock & rsm_client_mutex_lock) { LOG("rsm_client::init_members get members!"); handle h(primary); int ret = rsm_client_protocol::ERR; rpcc *cl; { - adopt_lock ml(rsm_client_mutex); - ml.unlock(); + rsm_client_mutex_lock.unlock(); cl = h.safebind(); if (cl) ret = cl->call_timeout(rsm_client_protocol::members, rpcc::to(1000), known_mems, 0); - ml.lock(); + rsm_client_mutex_lock.lock(); } if (cl == 0 || ret != rsm_protocol::OK) return false;