| 33 | DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests"); |
| 34 | |
| 35 | int main(int argc, char* argv[]) { |
| 36 | // Parse gflags. We recommend you to use gflags as well. |
| 37 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 38 | |
| 39 | // A Channel represents a communication line to a Server. Notice that |
| 40 | // Channel is thread-safe and can be shared by all threads in your program. |
| 41 | brpc::Channel channel; |
| 42 | |
| 43 | // Initialize the channel, NULL means using default options. |
| 44 | brpc::ChannelOptions options; |
| 45 | options.protocol = brpc::PROTOCOL_BAIDU_STD; |
| 46 | options.connection_type = FLAGS_connection_type; |
| 47 | options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; |
| 48 | options.max_retry = FLAGS_max_retry; |
| 49 | if (channel.Init(FLAGS_proxy_address.c_str(), |
| 50 | 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 | example::EchoService_Stub stub(&channel); |
| 58 | |
| 59 | // Send a request and wait for the response every 1 second. |
| 60 | int log_id = 0; |
| 61 | while (!brpc::IsAskedToQuit()) { |
| 62 | // We will receive response synchronously, safe to put variables |
| 63 | // on stack. |
| 64 | example::EchoRequest request; |
| 65 | example::EchoResponse response; |
| 66 | brpc::Controller cntl; |
| 67 | |
| 68 | request.set_message("hello world"); |
| 69 | cntl.set_request_compress_type((brpc::CompressType)FLAGS_compress_type); |
| 70 | |
| 71 | cntl.set_log_id(log_id++); // set by user |
| 72 | // Set attachment which is wired to network directly instead of |
| 73 | // being serialized into protobuf messages. |
| 74 | cntl.request_attachment().append(FLAGS_attachment); |
| 75 | |
| 76 | // Because `done'(last parameter) is NULL, this function waits until |
| 77 | // the response comes back or error occurs(including timedout). |
| 78 | stub.Echo(&cntl, &request, &response, NULL); |
| 79 | if (!cntl.Failed()) { |
| 80 | LOG(INFO) << "Received response from " << cntl.remote_side() |
| 81 | << " to " << cntl.local_side() |
| 82 | << ": " << response.message() |
| 83 | << ", response compress type=" << cntl.response_compress_type() |
| 84 | << ", attached=" << cntl.response_attachment() |
| 85 | << ", latency=" << cntl.latency_us() << "us"; |
| 86 | } else { |
| 87 | LOG(WARNING) << cntl.ErrorText(); |
| 88 | } |
| 89 | usleep(FLAGS_interval_ms * 1000L); |
| 90 | } |
| 91 | |
| 92 | LOG(INFO) << "EchoClient is going to quit"; |
nothing calls this directly
no test coverage detected