| 32 | DEFINE_bool(gzip, false, "compress body using gzip"); |
| 33 | |
| 34 | int main(int argc, char* argv[]) { |
| 35 | // Parse gflags. We recommend you to use gflags as well. |
| 36 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 37 | if (FLAGS_gzip) { |
| 38 | GFLAGS_NAMESPACE::SetCommandLineOption("http_body_compress_threshold", 0); |
| 39 | } |
| 40 | |
| 41 | // A Channel represents a communication line to a Server. Notice that |
| 42 | // Channel is thread-safe and can be shared by all threads in your program. |
| 43 | brpc::Channel channel; |
| 44 | |
| 45 | // Initialize the channel, NULL means using default options. |
| 46 | brpc::ChannelOptions options; |
| 47 | options.protocol = FLAGS_protocol; |
| 48 | options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; |
| 49 | options.max_retry = FLAGS_max_retry; |
| 50 | if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) { |
| 51 | LOG(ERROR) << "Fail to initialize channel"; |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | // Normally, you should not call a Channel directly, but instead construct |
| 56 | // a stub Service wrapping it. stub can be shared by all threads as well. |
| 57 | helloworld::Greeter_Stub stub(&channel); |
| 58 | |
| 59 | // Send a request and wait for the response every 1 second. |
| 60 | while (!brpc::IsAskedToQuit()) { |
| 61 | // We will receive response synchronously, safe to put variables |
| 62 | // on stack. |
| 63 | helloworld::HelloRequest request; |
| 64 | helloworld::HelloReply response; |
| 65 | brpc::Controller cntl; |
| 66 | |
| 67 | request.set_name("grpc_req_from_brpc"); |
| 68 | if (FLAGS_gzip) { |
| 69 | cntl.set_request_compress_type(brpc::COMPRESS_TYPE_GZIP); |
| 70 | } |
| 71 | // Because `done'(last parameter) is NULL, this function waits until |
| 72 | // the response comes back or error occurs(including timedout). |
| 73 | stub.SayHello(&cntl, &request, &response, NULL); |
| 74 | if (!cntl.Failed()) { |
| 75 | LOG(INFO) << "Received response from " << cntl.remote_side() |
| 76 | << " to " << cntl.local_side() |
| 77 | << ": " << response.message() |
| 78 | << " latency=" << cntl.latency_us() << "us"; |
| 79 | } else { |
| 80 | LOG(WARNING) << cntl.ErrorText(); |
| 81 | } |
| 82 | usleep(FLAGS_interval_ms * 1000L); |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
nothing calls this directly
no test coverage detected