a06b156acf89df8b6f4fae943293b55476ba678d
[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 "rpc/rpc.h"
27 #include <string>
28
29 using std::string;
30
31 class hinfo;
32
33 class handle {
34     private:
35         hinfo *h;
36     public:
37         handle(const string & m);
38         ~handle();
39         /* safebind will try to bind with the rpc server on the first call.
40          * Since bind may block, the caller probably should not hold a mutex
41          * when calling safebind.
42          *
43          * return: 
44          *   if the first safebind succeeded, all later calls would return
45          *   a rpcc object; otherwise, all later calls would return NULL.
46          *
47          * Example:
48          *   handle h(dst);
49          *   XXX_protocol::status ret;
50          *   if (h.safebind()) {
51          *     ret = h.safebind()->call(...);
52          *   }
53          *   if (!h.safebind() || ret != XXX_protocol::OK) {
54          *     // handle failure
55          *   }
56          */
57         rpcc *safebind();
58 };
59
60 void invalidate_handle(const string & m);
61
62 #endif