-
Notifications
You must be signed in to change notification settings - Fork 656
/
simple_rpc_channel_impl.cc
196 lines (177 loc) · 6.06 KB
/
simple_rpc_channel_impl.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <sofa/pbrpc/simple_rpc_channel_impl.h>
#include <sofa/pbrpc/mock_test_helper.h>
#include <sofa/pbrpc/closure.h>
namespace sofa {
namespace pbrpc {
SimpleRpcChannelImpl::SimpleRpcChannelImpl(const RpcClientImplPtr& rpc_client_impl,
const std::string& server_address,
const RpcChannelOptions& options)
: _client_impl(rpc_client_impl)
, _server_address(server_address)
, _options(options)
, _is_mock(false)
, _resolve_address_succeed(false)
{
}
SimpleRpcChannelImpl::~SimpleRpcChannelImpl()
{
}
bool SimpleRpcChannelImpl::Init()
{
if (g_enable_mock && _server_address.find(SOFA_PBRPC_MOCK_CHANNEL_ADDRESS_PREFIX) == 0)
{
#if defined( LOG )
LOG(INFO) << "Init(): use mock channel";
#else
SLOG(INFO, "Init(): use mock channel");
#endif
_is_mock = true;
return true;
}
if (_client_impl->ResolveAddress(_server_address, &_remote_endpoint))
{
#if defined( LOG )
LOG(INFO) << "Init(): resolve address succeed: " << _server_address
<< " [" << RpcEndpointToString(_remote_endpoint) << "]";
#else
SLOG(INFO, "Init(): resolve address succeed: %s [%s]",
_server_address.c_str(), RpcEndpointToString(_remote_endpoint).c_str());
#endif
_resolve_address_succeed = true;
return true;
}
else
{
#if defined( LOG )
LOG(ERROR) << "Init(): resolve address failed: " << _server_address;
#else
SLOG(ERROR, "Init(): resolve address failed: %s",
_server_address.c_str());
#endif
_resolve_address_succeed = false;
return false;
}
}
void SimpleRpcChannelImpl::Stop()
{
#if defined( LOG )
LOG(INFO) << "Stop(): simple rpc channel stopped: " << _server_address;
#else
SLOG(INFO, "Stop(): simple rpc channel stopped: %s", _server_address.c_str());
#endif
}
void SimpleRpcChannelImpl::CallMethod(const ::google::protobuf::MethodDescriptor* method,
::google::protobuf::RpcController* controller,
const ::google::protobuf::Message* request,
::google::protobuf::Message* response,
::google::protobuf::Closure* done)
{
++_wait_count;
// prepare controller
RpcController* sofa_controller = dynamic_cast<RpcController*>(controller);
SCHECK(sofa_controller != NULL); // should be sofa::pbrpc::RpcController
RpcControllerImplPtr cntl = sofa_controller->impl();
cntl->PushDoneCallback(boost::bind(&SimpleRpcChannelImpl::DoneCallback,
shared_from_this(), done, _1));
cntl->FillFromMethodDescriptor(method);
if (done == NULL)
{
cntl->SetSync(); // null done means sync call
cntl->SetWaitEvent(WaitEventPtr(new WaitEvent()));
}
// check if mocked
if (g_enable_mock && _is_mock)
{
MockMethodHookFunction* mock_closure =
MockTestHelper::GlobalInstance()->GetMockMethod(method->full_name());
if (mock_closure)
{
// mock method registered
#if defined( LOG )
LOG(INFO) << "CallMethod(): mock method [" << method->full_name() << "] called";
#else
SLOG(INFO, "CallMethod(): mock method [%s] called", method->full_name().c_str());
#endif
::google::protobuf::Closure* mock_done =
NewClosure(&SimpleRpcChannelImpl::MockDoneCallback, cntl, request, response);
mock_closure->Run(controller, request, response, mock_done);
}
else
{
// mock method not registered, but it is in mock channel
#if defined( LOG )
LOG(ERROR) << "CallMethod(): mock method [" << method->full_name()
<< "] not registered, but used in mock channel";
#else
SLOG(ERROR, "CallMethod(): mock method [%s] not registered"
", but used in mock channel", method->full_name().c_str());
#endif
cntl->Done(RPC_ERROR_FOUND_METHOD, "mock method not registered: "
+ method->full_name());
}
WaitDone(cntl);
return;
}
if (!_resolve_address_succeed)
{
// TODO resolve address failed, retry resolve?
#if defined( LOG )
LOG(ERROR) << "CallMethod(): resolve address failed: " << _server_address;
#else
SLOG(ERROR, "CallMethod(): resolve address failed: %s", _server_address.c_str());
#endif
cntl->Done(RPC_ERROR_RESOLVE_ADDRESS, _server_address);
WaitDone(cntl);
return;
}
// ready, go ahead to do real call
cntl->SetRemoteEndpoint(_remote_endpoint);
cntl->StartTimer();
_client_impl->CallMethod(request, response, cntl);
WaitDone(cntl);
}
uint32 SimpleRpcChannelImpl::WaitCount()
{
return _wait_count;
}
void SimpleRpcChannelImpl::WaitDone(const RpcControllerImplPtr& cntl)
{
// if sync, wait for callback done
if (cntl->IsSync())
{
cntl->WaitEvent()->Wait();
SCHECK(cntl->IsDone());
}
}
void SimpleRpcChannelImpl::DoneCallback(google::protobuf::Closure* done,
const RpcControllerImplPtr& cntl)
{
--_wait_count;
if (cntl->IsSync())
{
SCHECK(done == NULL);
SCHECK(cntl->WaitEvent());
cntl->WaitEvent()->Signal();
}
else
{
SCHECK(done != NULL);
_client_impl->GetCallbackThreadGroup()->post(done);
}
}
void SimpleRpcChannelImpl::MockDoneCallback(RpcControllerImplPtr cntl,
const ::google::protobuf::Message* request,
::google::protobuf::Message* /*response*/)
{
if (!cntl->Failed())
{
cntl->NotifyRequestSent(RpcEndpoint(), request->ByteSize());
}
cntl->Done(cntl->ErrorCode(), cntl->Reason());
}
} // namespace pbrpc
} // namespace sofa
/* vim: set ts=4 sw=4 sts=4 tw=100 */