mirror of
https://gitee.com/sogou/srpc.git
synced 2024-12-21 16:14:54 +08:00
srpc-ctl: support new command [redis]
This commit is contained in:
parent
7a769161ab
commit
c236fbc319
@ -25,6 +25,6 @@ set(srpc_ctl_code
|
||||
"srpc_proxy_controller.cc")
|
||||
|
||||
include_directories("../src/")
|
||||
add_executable(srpc-ctl ${srpc_ctl_code} ${generator_code})
|
||||
target_link_libraries(srpc-ctl ${LIBRARY_NAME})
|
||||
add_executable(srpc ${srpc_ctl_code} ${generator_code})
|
||||
target_link_libraries(srpc ${LIBRARY_NAME})
|
||||
|
||||
|
@ -25,6 +25,7 @@ Usage:
|
||||
|
||||
Available Commands:
|
||||
"http" - create project with both client and server
|
||||
"redis" - create project with both client and server
|
||||
"rpc" - create project with both client and server
|
||||
"proxy" - create proxy for some client and server protocol
|
||||
"file" - create project with file service
|
||||
@ -164,7 +165,59 @@ Execute:
|
||||
./client
|
||||
```
|
||||
|
||||
#### 6. PROXY COMMANDS
|
||||
#### 6. REDIS COMMANDS
|
||||
|
||||
commands for REDIS:
|
||||
|
||||
```
|
||||
./srpc redis
|
||||
```
|
||||
|
||||
will get the following instructions:
|
||||
|
||||
```
|
||||
Missing: PROJECT_NAME
|
||||
|
||||
Usage:
|
||||
./srpc redis <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Available Flags:
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
-d : path of dependencies (default: COMPILE_PATH)
|
||||
```
|
||||
|
||||
Make a project with the instructions, we can get the simple redis server and client. The client will send a basic command `SET k1 v1`, and the server will reply `OK` for every request.
|
||||
|
||||
```
|
||||
./server
|
||||
|
||||
Redis server start, port 6379
|
||||
redis server get cmd: [SET] from peer address: 127.0.0.1:60665, seq: 0.
|
||||
```
|
||||
|
||||
```
|
||||
./client
|
||||
|
||||
Redis client state = 0 error = 0
|
||||
response: OK
|
||||
```
|
||||
|
||||
If there is user name and password for redis server, client may fill them into client.conf:
|
||||
|
||||
```
|
||||
1 {
|
||||
2 "client":
|
||||
3 {
|
||||
4 "remote_host": "127.0.0.1",
|
||||
5 "remote_port": 6379,
|
||||
6 "retry_max": 2,
|
||||
7 "user_name": "root",
|
||||
8 "password": ""
|
||||
9 }
|
||||
10 }
|
||||
```
|
||||
|
||||
#### 7. PROXY COMMANDS
|
||||
|
||||
commands for RPCs:
|
||||
|
||||
|
@ -26,6 +26,8 @@
|
||||
|
||||
#include "srpc_controller.h"
|
||||
|
||||
using DEFAULT_FILES = std::vector<struct CommandController::file_info>;
|
||||
|
||||
static std::string server_process_codes(uint8_t type)
|
||||
{
|
||||
if (type == PROTOCOL_TYPE_HTTP)
|
||||
@ -69,28 +71,28 @@ static std::string client_redirect_codes(uint8_t type)
|
||||
static std::string client_task_callback_codes(uint8_t type)
|
||||
{
|
||||
if (type == PROTOCOL_TYPE_HTTP)
|
||||
return std::string(R"(
|
||||
if (state == WFT_STATE_SUCCESS) // print server response body
|
||||
{
|
||||
const void *body;
|
||||
size_t body_len;
|
||||
return std::string(R"(
|
||||
if (state == WFT_STATE_SUCCESS) // print server response body
|
||||
{
|
||||
const void *body;
|
||||
size_t body_len;
|
||||
|
||||
task->get_resp()->get_parsed_body(&body, &body_len);
|
||||
fwrite(body, 1, body_len, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
task->get_resp()->get_parsed_body(&body, &body_len);
|
||||
fwrite(body, 1, body_len, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
)");
|
||||
|
||||
if (type == PROTOCOL_TYPE_REDIS)
|
||||
return std::string(R"(
|
||||
protocol::RedisResponse *resp = task->get_resp();
|
||||
protocol::RedisValue val;
|
||||
protocol::RedisResponse *resp = task->get_resp();
|
||||
protocol::RedisValue val;
|
||||
|
||||
if (state == WFT_STATE_SUCCESS && resp->parse_success() == true)
|
||||
{
|
||||
resp->get_result(val);
|
||||
fprintf(stderr, "response: %s\n", val.string_value().c_str());
|
||||
}
|
||||
if (state == WFT_STATE_SUCCESS && resp->parse_success() == true)
|
||||
{
|
||||
resp->get_result(val);
|
||||
fprintf(stderr, "response: %s\n", val.string_value().c_str());
|
||||
}
|
||||
)");
|
||||
|
||||
return std::string("Unknown type");
|
||||
@ -112,6 +114,16 @@ static std::string client_set_request_codes(uint8_t type)
|
||||
return std::string("Unknown type");
|
||||
}
|
||||
|
||||
static std::string username_passwd_codes(uint8_t type)
|
||||
{
|
||||
if (type == PROTOCOL_TYPE_REDIS || type == PROTOCOL_TYPE_MYSQL)
|
||||
return std::string(R"(config.client_user_name() +
|
||||
std::string(":") + config.client_password() +
|
||||
std::string("@") +)");
|
||||
|
||||
return std::string("");
|
||||
}
|
||||
|
||||
static uint8_t get_protocol_type(const struct srpc_config *config, uint8_t type)
|
||||
{
|
||||
if (config->type == COMMAND_HTTP ||
|
||||
@ -163,15 +175,21 @@ bool basic_client_config_transform(const std::string& format, FILE *out,
|
||||
{
|
||||
unsigned short port;
|
||||
std::string redirect_code;
|
||||
std::string user_and_passwd;
|
||||
|
||||
if (get_client_protocol_type(config) == PROTOCOL_TYPE_HTTP)
|
||||
{
|
||||
port = 80;
|
||||
redirect_code = R"( "redirect_max": 2,
|
||||
)";
|
||||
redirect_code = R"(
|
||||
"redirect_max": 2,)";
|
||||
}
|
||||
else if (get_client_protocol_type(config) == PROTOCOL_TYPE_REDIS)
|
||||
{
|
||||
port = 6379;
|
||||
user_and_passwd = R"(,
|
||||
"user_name": "root",
|
||||
"password": "")";
|
||||
}
|
||||
else if (get_client_protocol_type(config) == PROTOCOL_TYPE_MYSQL)
|
||||
port = 3306;
|
||||
else
|
||||
@ -181,7 +199,8 @@ bool basic_client_config_transform(const std::string& format, FILE *out,
|
||||
if (config->type == COMMAND_PROXY)
|
||||
port = port - 1;
|
||||
|
||||
size_t len = fprintf(out, format.c_str(), port, redirect_code.c_str());
|
||||
size_t len = fprintf(out, format.c_str(), port,
|
||||
redirect_code.c_str(), user_and_passwd.c_str());
|
||||
|
||||
return len > 0;
|
||||
}
|
||||
@ -209,68 +228,58 @@ bool basic_client_transform(const std::string& format, FILE *out,
|
||||
|
||||
size_t len = fprintf(out, format.c_str(), type, type, type,
|
||||
client_task_callback_codes(client_type).c_str(),
|
||||
client_lower.c_str(), type, client_lower.c_str(),
|
||||
client_lower.c_str(),
|
||||
username_passwd_codes(client_type).c_str(),
|
||||
type, client_lower.c_str(),
|
||||
client_redirect_codes(client_type).c_str(),
|
||||
client_set_request_codes(client_type).c_str());
|
||||
|
||||
return len > 0;
|
||||
}
|
||||
|
||||
HttpController::HttpController()
|
||||
static void basic_default_file_initialize(DEFAULT_FILES& files)
|
||||
{
|
||||
this->config.type = COMMAND_HTTP;
|
||||
struct file_info info;
|
||||
struct CommandController::file_info info;
|
||||
|
||||
info = { "basic/server.conf", "server.conf", basic_server_config_transform };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "basic/client.conf", "client.conf", basic_client_config_transform };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "basic/server_main.cc", "server_main.cc", basic_server_transform };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "basic/client_main.cc", "client_main.cc", basic_client_transform };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "common/config.json", "example.conf", nullptr };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "common/util.h", "config/util.h", nullptr };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "common/CMakeLists.txt", "CMakeLists.txt", common_cmake_transform };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = {"common/GNUmakefile", "GNUmakefile", nullptr };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "config/Json.h", "config/Json.h", nullptr };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = { "config/Json.cc", "config/Json.cc", nullptr };
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = {"config/config_simple.h", "config/config.h", nullptr};
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
|
||||
info = {"config/config_simple.cc", "config/config.cc", nullptr};
|
||||
this->default_files.push_back(info);
|
||||
files.push_back(info);
|
||||
}
|
||||
|
||||
void HttpController::print_usage(const char *name) const
|
||||
static bool basic_get_opt(int argc, const char **argv, struct srpc_config *config)
|
||||
{
|
||||
printf("Usage:\n"
|
||||
" %s http <PROJECT_NAME> [FLAGS]\n\n"
|
||||
"Available Flags:\n"
|
||||
" -o : project output path (default: CURRENT_PATH)\n"
|
||||
" -d : path of dependencies (default: COMPILE_PATH)\n"
|
||||
, name);
|
||||
}
|
||||
|
||||
bool HttpController::get_opt(int argc, const char **argv)
|
||||
{
|
||||
struct srpc_config *config = &this->config;
|
||||
char c;
|
||||
optind = 3;
|
||||
|
||||
@ -300,3 +309,45 @@ bool HttpController::get_opt(int argc, const char **argv)
|
||||
return true;
|
||||
}
|
||||
|
||||
static void basic_print_usage(const char *name, const char *command)
|
||||
{
|
||||
printf("Usage:\n"
|
||||
" %s %s <PROJECT_NAME> [FLAGS]\n\n"
|
||||
"Available Flags:\n"
|
||||
" -o : project output path (default: CURRENT_PATH)\n"
|
||||
" -d : path of dependencies (default: COMPILE_PATH)\n"
|
||||
, name, command);
|
||||
}
|
||||
|
||||
HttpController::HttpController()
|
||||
{
|
||||
this->config.type = COMMAND_HTTP;
|
||||
basic_default_file_initialize(this->default_files);
|
||||
}
|
||||
|
||||
void HttpController::print_usage(const char *name) const
|
||||
{
|
||||
basic_print_usage(name, "http");
|
||||
}
|
||||
|
||||
bool HttpController::get_opt(int argc, const char **argv)
|
||||
{
|
||||
return basic_get_opt(argc, argv, &this->config);
|
||||
}
|
||||
|
||||
RedisController::RedisController()
|
||||
{
|
||||
this->config.type = COMMAND_REDIS;
|
||||
basic_default_file_initialize(this->default_files);
|
||||
}
|
||||
|
||||
void RedisController::print_usage(const char *name) const
|
||||
{
|
||||
basic_print_usage(name, "redis");
|
||||
}
|
||||
|
||||
bool RedisController::get_opt(int argc, const char **argv)
|
||||
{
|
||||
return basic_get_opt(argc, argv, &this->config);
|
||||
}
|
||||
|
||||
|
@ -31,34 +31,6 @@ std::vector<std::string> RPC_THRIFT_SKIP_FILES =
|
||||
{ "config_simple.h", "config_simple.cc",
|
||||
"rpc.proto", "client_protobuf.cc", "server_protobuf.cc" };
|
||||
|
||||
void usage_db(int argc, const char *argv[], const struct srpc_config *config)
|
||||
{
|
||||
const char *name;
|
||||
unsigned short port;
|
||||
|
||||
switch (config->type)
|
||||
{
|
||||
case COMMAND_REDIS:
|
||||
name = "redis";
|
||||
port = 6379;
|
||||
break;
|
||||
case COMMAND_MYSQL:
|
||||
name = "mysql";
|
||||
port = 3306;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Usage:\n"
|
||||
" %s %s <PROJECT_NAME> [FLAGS]\n\n"
|
||||
"Available Flags:\n"
|
||||
" -o : project output path (default: CURRENT_PATH)\n"
|
||||
" -h : %s server url (default: %s://127.0.0.1:%u)\n"
|
||||
" -d : path of dependencies (default: COMPILE_PATH)\n"
|
||||
, argv[0], name, name, name, port);
|
||||
}
|
||||
|
||||
void usage_kafka(int argc, const char *argv[])
|
||||
{
|
||||
printf("Usage:\n"
|
||||
|
@ -38,7 +38,6 @@ public:
|
||||
CommandController() { }
|
||||
virtual ~CommandController() { }
|
||||
|
||||
protected:
|
||||
using transform_function_t = bool (*)(const std::string&, FILE *,
|
||||
const struct srpc_config *);
|
||||
struct file_info
|
||||
@ -48,6 +47,7 @@ protected:
|
||||
transform_function_t transform;
|
||||
};
|
||||
|
||||
protected:
|
||||
std::vector<struct file_info> default_files;
|
||||
struct srpc_config config;
|
||||
|
||||
@ -72,6 +72,19 @@ public:
|
||||
~HttpController() { }
|
||||
};
|
||||
|
||||
class RedisController : public CommandController
|
||||
{
|
||||
public:
|
||||
void print_usage(const char *name) const override;
|
||||
|
||||
private:
|
||||
bool get_opt(int argc, const char **argv) override;
|
||||
|
||||
public:
|
||||
RedisController();
|
||||
~RedisController() { }
|
||||
};
|
||||
|
||||
class RPCController : public CommandController
|
||||
{
|
||||
public:
|
||||
|
@ -25,6 +25,7 @@ static void usage(const char *name)
|
||||
" %s <COMMAND> <PROJECT_NAME> [FLAGS]\n\n"
|
||||
"Available Commands:\n"
|
||||
" \"http\" - create project with both client and server\n"
|
||||
" \"redis\" - create project with both client and server\n"
|
||||
" \"rpc\" - create project with both client and server\n"
|
||||
" \"proxy\" - create proxy for some client and server protocol\n"
|
||||
" \"file\" - create project with file service\n"
|
||||
@ -45,6 +46,10 @@ int main(int argc, const char *argv[])
|
||||
{
|
||||
ctl = new HttpController;
|
||||
}
|
||||
else if (strcasecmp(argv[1], "redis") == 0)
|
||||
{
|
||||
ctl = new RedisController;
|
||||
}
|
||||
else if (strcasecmp(argv[1], "rpc") == 0)
|
||||
{
|
||||
ctl = new RPCController;
|
||||
|
@ -3,7 +3,7 @@
|
||||
{
|
||||
"remote_host": "127.0.0.1",
|
||||
"remote_port": %u,%s
|
||||
"retry_max": 2
|
||||
"retry_max": 2%s
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,10 +38,9 @@ int main()
|
||||
{
|
||||
init();
|
||||
|
||||
std::string url = std::string("%s://") + config.client_host() +
|
||||
std::string url = std::string("%s://") + %sconfig.client_host() +
|
||||
std::string(":") + std::to_string(config.client_port());
|
||||
|
||||
|
||||
WF%sTask *task = WFTaskFactory::create_%s_task(url,%s
|
||||
config.retry_max(),
|
||||
callback);
|
||||
|
@ -11,6 +11,8 @@
|
||||
"is_ssl" : false,
|
||||
"redirect_max": 2,
|
||||
"retry_max": 1,
|
||||
"user_name": "root",
|
||||
"password": "",
|
||||
"callee" : "rpc_client"
|
||||
},
|
||||
|
||||
|
@ -227,6 +227,12 @@ void RPCConfig::load_client()
|
||||
|
||||
if (this->data["client"].has("retry_max"))
|
||||
this->c_retry_max = this->data["client"]["retry_max"];
|
||||
|
||||
if (this->data["client"].has("user_name"))
|
||||
this->c_user_name = this->data["client"]["user_name"].get<std::string>();
|
||||
|
||||
if (this->data["client"].has("password"))
|
||||
this->c_password = this->data["client"]["password"].get<std::string>();
|
||||
}
|
||||
|
||||
bool RPCConfig::load(const char *file)
|
||||
|
@ -43,6 +43,8 @@ public:
|
||||
int redirect_max() const { return this->c_redirect_max; }
|
||||
int retry_max() const { return this->c_retry_max; }
|
||||
const char *client_caller() const { return this->c_caller.c_str(); }
|
||||
const char *client_user_name() const { return this->c_user_name.c_str(); }
|
||||
const char *client_password() const { return this->c_password.c_str(); }
|
||||
|
||||
public:
|
||||
RPCConfig() :
|
||||
@ -66,6 +68,8 @@ private:
|
||||
int c_redirect_max;
|
||||
int c_retry_max;
|
||||
std::string c_caller;
|
||||
std::string c_user_name;
|
||||
std::string c_password;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -225,6 +225,12 @@ void RPCConfig::load_client()
|
||||
|
||||
if (this->data["client"].has("retry_max"))
|
||||
this->c_retry_max = this->data["client"]["retry_max"];
|
||||
|
||||
if (this->data["client"].has("user_name"))
|
||||
this->c_user_name = this->data["client"]["user_name"].get<std::string>();
|
||||
|
||||
if (this->data["client"].has("password"))
|
||||
this->c_password = this->data["client"]["password"].get<std::string>();
|
||||
}
|
||||
|
||||
bool RPCConfig::load(const char *file)
|
||||
|
@ -16,6 +16,8 @@ public:
|
||||
const char *client_host() const { return this->c_host.c_str(); }
|
||||
int redirect_max() const { return this->c_redirect_max; }
|
||||
int retry_max() const { return this->c_retry_max; }
|
||||
const char *client_user_name() const { return this->c_user_name.c_str(); }
|
||||
const char *client_password() const { return this->c_password.c_str(); }
|
||||
|
||||
public:
|
||||
RPCConfig() : s_port(0), c_port(0), c_redirect_max(0), c_retry_max(0) { }
|
||||
@ -31,6 +33,8 @@ private:
|
||||
unsigned short c_port;
|
||||
int c_redirect_max;
|
||||
int c_retry_max;
|
||||
std::string c_user_name;
|
||||
std::string c_password;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user