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