Cleanups
[invirt/third/libt4.git] / handle.h
1 // manage a cache of RPC connections.
2 // assuming cid is a std::string holding the
3 // host:port of the RPC server you want
4 // to talk to:
5 //
6 // handle h(cid);
7 // rpcc *cl = h.safebind();
8 // if(cl){
9 //   ret = cl->call(...);
10 // } else {
11 //   bind() failed
12 // }
13 //
14 // if the calling program has not contacted
15 // cid before, safebind() will create a new
16 // connection, call bind(), and return
17 // an rpcc*, or 0 if bind() failed. if the
18 // program has previously contacted cid,
19 // safebind() just returns the previously
20 // created rpcc*. best not to hold any
21 // mutexes while calling safebind().
22
23 #ifndef handle_h
24 #define handle_h
25
26 #include <string>
27 #include <vector>
28 #include "rpc/rpc.h"
29
30 struct hinfo {
31   rpcc *cl;
32   int refcnt;
33   bool del;
34   std::string m;
35   std::mutex cl_mutex;
36 };
37
38 class handle {
39     private:
40         struct hinfo *h;
41     public:
42         handle(std::string m);
43         ~handle();
44         /* safebind will try to bind with the rpc server on the first call.
45          * Since bind may block, the caller probably should not hold a mutex
46          * when calling safebind.
47          *
48          * return: 
49          *   if the first safebind succeeded, all later calls would return
50          *   a rpcc object; otherwise, all later calls would return NULL.
51          *
52          * Example:
53          *   handle h(dst);
54          *   XXX_protocol::status ret;
55          *   if (h.safebind()) {
56          *     ret = h.safebind()->call(...);
57          *   }
58          *   if (!h.safebind() || ret != XXX_protocol::OK) {
59          *     // handle failure
60          *   }
61          */
62         rpcc *safebind();
63 };
64
65 class handle_mgr {
66     private:
67         std::mutex handle_mutex;
68         std::map<std::string, struct hinfo *> hmap;
69     public:
70         handle_mgr();
71         struct hinfo *get_handle(std::string m);
72         void done_handle(struct hinfo *h);
73         void delete_handle(std::string m);
74         void delete_handle_wo(std::string m);
75 };
76
77 extern class handle_mgr mgr;
78
79 #endif