| 76 | } |
| 77 | |
| 78 | int main(int argc, char* argv[]) { |
| 79 | // Parse gflags. We recommend you to use gflags as well. |
| 80 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 81 | |
| 82 | // A Channel represents a communication line to a Server. Notice that |
| 83 | // Channel is thread-safe and can be shared by all threads in your program. |
| 84 | brpc::Channel channel; |
| 85 | |
| 86 | // Initialize the channel, NULL means using default options. |
| 87 | brpc::ChannelOptions options; |
| 88 | options.protocol = brpc::PROTOCOL_REDIS; |
| 89 | options.connection_type = FLAGS_connection_type; |
| 90 | options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; |
| 91 | options.max_retry = FLAGS_max_retry; |
| 92 | if (channel.Init(FLAGS_server.c_str(), &options) != 0) { |
| 93 | LOG(ERROR) << "Fail to initialize channel"; |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | if (argc <= 1) { // interactive mode |
| 98 | // We need this dummy signal hander to interrupt getc (and returning |
| 99 | // EINTR), SIG_IGN did not work. |
| 100 | signal(SIGINT, dummy_handler); |
| 101 | |
| 102 | // Hook getc of readline. |
| 103 | rl_getc_function = cli_getc; |
| 104 | |
| 105 | // Print welcome information. |
| 106 | printf("%s\n", brpc::logo()); |
| 107 | printf("This command-line tool mimics the look-n-feel of official " |
| 108 | "redis-cli, as a demostration of brpc's capability of" |
| 109 | " talking to redis-server. The output and behavior is " |
| 110 | "not exactly same with the official one.\n\n"); |
| 111 | |
| 112 | for (;;) { |
| 113 | char prompt[64]; |
| 114 | snprintf(prompt, sizeof(prompt), "redis %s> ", FLAGS_server.c_str()); |
| 115 | std::unique_ptr<char, Freer> command(readline(prompt)); |
| 116 | if (command == NULL || *command == '\0') { |
| 117 | if (g_canceled) { |
| 118 | // No input after the prompt and user pressed Ctrl-C, |
| 119 | // quit the CLI. |
| 120 | return 0; |
| 121 | } |
| 122 | // User entered an empty command by just pressing Enter. |
| 123 | continue; |
| 124 | } |
| 125 | if (g_canceled) { |
| 126 | // User entered sth. and pressed Ctrl-C, start a new prompt. |
| 127 | g_canceled = false; |
| 128 | continue; |
| 129 | } |
| 130 | // Add user's command to history so that it's browse-able by |
| 131 | // UP-key and search-able by Ctrl-R. |
| 132 | add_history(command.get()); |
| 133 | |
| 134 | if (!strcmp(command.get(), "help")) { |
| 135 | printf("This is a redis CLI written in brpc.\n"); |