-
Notifications
You must be signed in to change notification settings - Fork 0
/
userservice.cc
91 lines (78 loc) · 2.93 KB
/
userservice.cc
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
80
81
82
83
84
85
86
87
88
89
90
91
#include "mprpcapplication.hpp"
#include "rpcprovider.hpp"
#include "user.pb.h"
#include <google/protobuf/port_def.inc>
#include <iostream>
#include <string>
// the service implement, callee
class UserService : public kirito::UserServiceRPC {
public:
bool Login(std::string name, std::string pwd) {
std::cout << "You login successfully!" << std::endl;
std::cout << "name: " << name << std::endl
<< "password:" << pwd << std::endl;
return true;
}
/*
These function all is called by the rpc framework, so we need to implement
them. When we finish the implement, we can call the done->Run() to notify
the rpc framework. Finally, the rpc framework will send the response to the
caller.
*/
// override from LoginServiceRPC
void Login(::PROTOBUF_NAMESPACE_ID::RpcController *controller,
const ::kirito::LoginRequest *request,
::kirito::LoginResponse *response,
::google::protobuf::Closure *done) override {
/*
The rpc framework already process the data from caller and put them into
the request. So we can get the data from the request directly.
*/
std::string name = request->name();
std::string pwd = request->password();
// call the real implement
bool login_result = Login(name, pwd);
// write the response to the response
response->set_success(login_result);
kirito::Result *result = response->mutable_result();
result->set_errorcode(200);
result->set_errormessage("Login successfully");
// notify the rpc framework, execute the serilization and send the response
// to the caller
done->Run();
}
// test get the user info:
// the local service:
kirito::UserInfo getUserInfo(const int &userid) {
kirito::UserInfo userinfo;
userinfo.set_name("kirito");
userinfo.set_password("123456");
userinfo.set_email("[email protected]");
userinfo.set_phone("17698266260");
return userinfo;
}
// overide the GetUserInfo function from the UserServiceRPC
void GetUserInfo(::PROTOBUF_NAMESPACE_ID::RpcController *controller,
const ::kirito::UserInforRequest *request,
::kirito::UserInfoResponse *response,
::google::protobuf::Closure *done) override {
int userid = request->userid();
kirito::UserInfo userinfo = getUserInfo(userid);
std::cout << "the userid is " << userid << std::endl;
response->mutable_userinfo()->CopyFrom(userinfo);
response->mutable_result()->set_errorcode(200);
response->mutable_result()->set_errormessage(
"get the User message successfully");
done->Run();
}
};
int main(int argc, char **argv) {
// 0. the rpc framework initialize(just simple load a config file)
MprpcApplication::Initialize(argc, argv);
// 1. register the service
RpcProvider provider;
provider.NotifyService(new UserService());
// 2. run the rpc server and block the server
provider.Start();
return 0;
}