-#include "handle.h"
-
-class hinfo {
-public:
- unique_ptr<rpcc> client;
- bool valid = true;
- string destination;
- std::mutex client_mutex;
- hinfo(const string & destination_) : destination(destination_) {}
-};
-
-static std::mutex mgr_mutex;
-static std::map<string, shared_ptr<hinfo>> hmap;
-
-void handle::shutdown() {
- lock ml(mgr_mutex);
- LOG_NONMEMBER << "Shutting down handle manager";
- for (auto p : hmap) {
- p.second->valid = false;
- LOG_NONMEMBER << "cl " << p.first << " refcnt " << p.second.use_count();
- }
- hmap.clear();
-}
-
-handle::handle(const string & destination) : destination_(destination) {
- lock ml(mgr_mutex);
- h = hmap[destination];
- if (!h || !h->valid)
- h = (hmap[destination] = std::make_shared<hinfo>(destination));
-}
-
-rpcc * handle::safebind() {
- if (!h)
- return nullptr;
- lock cl(h->client_mutex);
- if (!h->valid)
- return nullptr;
- if (!h->client) {
- unique_ptr<rpcc> client(new rpcc(h->destination));
- LOG << "bind(\"" << h->destination << "\")";
- int ret = client->bind(milliseconds(1000));
- if (ret < 0) {
- LOG << "bind failure! " << h->destination << " " << ret;
- h->valid = false;
- } else {
- LOG << "bind succeeded " << h->destination;
- h->client = std::move(client);
- }
- }
- return h->client.get();
-}
-
-void handle::invalidate() {
- h.reset();
- lock ml(mgr_mutex);
- if (hmap.find(destination_) != hmap.end()) {
- hmap[destination_]->valid = false;
- LOG << "cl " << destination_ << " refcnt " << hmap[destination_].use_count();
- hmap.erase(destination_);
- }
-}