forked from Tencent/Pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_client.cpp
More file actions
317 lines (263 loc) · 9.64 KB
/
Copy pathcontrol_client.cpp
File metadata and controls
317 lines (263 loc) · 9.64 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Tencent is pleased to support the open source community by making Pebble available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
#include <iostream>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "client/pebble_client.h"
#include "common/mutex.h"
#include "common/string_utility.h"
#include "extension/extension.h"
#include "gflags/gflags.h"
#include "pebble_version.inh"
#include "src/server/control__PebbleControl.h"
// 考虑到控制命令客户端是一个比较小的程序,所有实现均写到一个cpp中
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define _CHECK_RESULT(condition, ret, error) \
if ((condition)) { \
snprintf(m_error, sizeof(m_error), "(%s:%d)(%s) ret=%d error=%s\n", \
__FILE__, __LINE__, __FUNCTION__, static_cast<int32_t>(ret), (error)); \
return -1; \
}
DEFINE_string(url, "tcp://127.0.0.1:9000",
"specify server listen address, support tbuspp,http,tcp and udp:\n"
" tbuspp://appid.control/instanceid\n"
" http://127.0.0.1:8880/appid/control\n"
" tcp://127.0.0.1:9000\n"
" udp://127.0.0.1:9001\n");
DEFINE_string(execute, "", "execute command and quit.");
DEFINE_string(options, "", "specify the options of execute command, "
"multi options are separated by commas(,) or space.");
DEFINE_int32(execute_timeout_ms, 1000, "set the timeout time for execution command.");
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 控制命令RPC交互处理类定义
class ControlClient {
public:
ControlClient();
~ControlClient();
int32_t Init(const std::string& url, bool batch);
int32_t RunCommand(const std::string& cmd, const std::vector<std::string>& args);
int32_t Update();
const char* GetLastError() { return m_error; }
private:
void CbRunCommand(const std::string& cmd,
int ret_code, const pebble::ControlResponse& response);
private:
char m_error[256];
bool m_batch;
pebble::PebbleClient* m_pebble_client;
pebble::_PebbleControlClient* m_stub;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 控制命令RPC交互处理类实现
ControlClient::ControlClient() {
m_error[0] = '\0';
m_batch = false;
m_pebble_client = NULL;
m_stub = NULL;
}
ControlClient::~ControlClient() {
delete m_stub;
delete m_pebble_client;
}
int32_t ControlClient::Init(const std::string& url, bool batch) {
m_batch = batch;
// 建立连接
int32_t ret = 0;
bool use_address = true;
if (pebble::StringUtility::StartsWith(url, "tbuspp")
|| pebble::StringUtility::StartsWith(url, "http")
|| pebble::StringUtility::StartsWith(url, "tbus")) {
// tbuspp not opened
// ret = INSTALL_TBUSPP;
ret = -1;
} else if (pebble::StringUtility::StartsWith(url, "tcp")
|| pebble::StringUtility::StartsWith(url, "udp")) {
// do nothing.
} else {
use_address = false;
}
_CHECK_RESULT(ret != 0, ret, "INSTALL TBUSPP failed, check the log for detail reason.");
m_pebble_client = new pebble::PebbleClient();
ret = m_pebble_client->Init();
_CHECK_RESULT(ret != 0, ret, "Pebble init failed, check the log for detail reason.");
if (use_address) {
m_stub = m_pebble_client->NewRpcClientByAddress<pebble::_PebbleControlClient>(
url, pebble::kPEBBLE_RPC_JSON);
} else {
m_stub = m_pebble_client->NewRpcClientByName<pebble::_PebbleControlClient>(
url, pebble::kPEBBLE_RPC_JSON);
}
_CHECK_RESULT(m_stub == NULL, -1, "May connect failed. check the log for detail reason.");
m_stub->SetTimeout(FLAGS_execute_timeout_ms);
return 0;
}
int32_t ControlClient::RunCommand(const std::string& cmd, const std::vector<std::string>& args) {
_CHECK_RESULT(cmd.empty(), -1, "cmd is null");
_CHECK_RESULT(m_stub == NULL, -1, "Control Client not init");
pebble::ControlRequest req;
req.command = cmd;
if (!args.empty()) {
req.__set_options(args);
}
m_stub->RunCommand(req, cxx::bind(&ControlClient::CbRunCommand, this,
cmd, cxx::placeholders::_1, cxx::placeholders::_2));
return 0;
}
int32_t ControlClient::Update() {
if (m_pebble_client) {
return m_pebble_client->Update();
}
return 0;
}
void ControlClient::CbRunCommand(const std::string& cmd,
int ret_code, const pebble::ControlResponse& response) {
int result = ret_code;
if (result != 0) {
std::cout << "Operate failed : " << pebble::GetErrorString(result) << std::endl;
} else {
result = response.ret_code;
std::cout << response.data << std::endl << std::endl;
}
if (m_batch) {
exit(result != 0 ? -1 : 0);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
pebble::Mutex g_mutex;
bool g_quit = false;
void* ControlIOThread(void* arg) {
ControlClient* client = reinterpret_cast<ControlClient*>(arg);
do {
usleep(10000);
pebble::AutoLocker lock(&g_mutex);
if (g_quit) {
break;
}
client->Update();
} while (true);
return NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
// 预处理options参数,支持空格分隔多参数
std::string ex_options;
char option_str[] = "-options";
for (int i = 1; i < argc; i++) {
char* find = strstr(argv[i], option_str);
if (find == NULL) {
continue;
}
// --options "a" "b" "c"
int offset = 2;
if (*(find + strlen(option_str)) == '=') {
// --options="a" "b" "c"
offset = 1;
}
for (int j = i + offset; j < argc; j++) {
if (argv[j][0] == '-') {
break;
}
ex_options.append(",");
ex_options.append(argv[j]);
}
break;
}
// 命令行处理
std::string version(pebble::PebbleVersion::GetVersion());
google::SetVersionString(version);
google::SetUsageMessage("usage:");
google::ParseCommandLineFlags(&argc, &argv, false);
// 获取命令行参数
std::string url(FLAGS_url);
std::string cmd(FLAGS_execute);
std::string options(FLAGS_options);
options.append(ex_options);
std::vector<std::string> params_vector;
// 是否为直接执行命令模式(批处理模式)
bool batch = !cmd.empty();
// 创建ControlClient对象
ControlClient client;
int32_t ret = client.Init(url, batch);
if (ret != 0) {
std::cout << "Init failed : " << client.GetLastError() << std::endl;
return -1;
}
// 命令模式
if (batch) {
pebble::StringUtility::Split(options, ",", ¶ms_vector);
ret = client.RunCommand(cmd, params_vector);
if (ret != 0) {
std::cout << "run cmd failed : " << client.GetLastError() << std::endl;
return ret;
}
do {
client.Update();
usleep(10000);
} while (true);
return 0;
}
// 交互模式
pthread_t io_thread;
ret = pthread_create(&io_thread, NULL, ControlIOThread, reinterpret_cast<void *>(&client));
if (ret != 0) {
std::cout << "pthread_create failed : " << ret << std::endl;
return ret;
}
std::cout << "Welcome to Pebble Control Client " << version << std::endl
<< " \'help\' for help, \'quit\' for quit" << std::endl;
rl_bind_key('\t', rl_insert);
do {
char* line = readline("> ");
if (line == NULL) {
continue;
}
std::string input(line);
free(line);
pebble::StringUtility::Trim(input);
if (input.empty()) {
continue;
}
ret = 0;
cmd.clear();
options.clear();
params_vector.clear();
// 控制端界面只支持quit和help命令,其他命令通过help获取
// 服务端内置reload、stat命令,用户可添加
if (strcasecmp(input.c_str(), "quit") == 0) {
pebble::AutoLocker lock(&g_mutex);
g_quit = true;
break;
} else {
size_t pos = input.find_first_of(' ');
cmd = input.substr(0, pos);
if (pos != std::string::npos) {
options = input.substr(pos + 1);
pebble::StringUtility::Split(options, " ", ¶ms_vector);
pebble::StringUtility::Trim(¶ms_vector);
}
pebble::AutoLocker lock(&g_mutex);
ret = client.RunCommand(cmd, params_vector);
add_history(input.c_str());
if (ret != 0) {
std::cout << "run cmd failed : " << client.GetLastError() << std::endl;
}
}
if (ret == 0) {
usleep(300 * 1000);
}
} while (true);
pthread_join(io_thread, NULL);
}