client.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // A client sending requests to ubrpc server every 1 second.
  18. // This client can access the server in public/baidu-rpc-ub/example/echo_c++_compack_ubrpc as well.
  19. #include <gflags/gflags.h>
  20. #include <butil/logging.h>
  21. #include <butil/time.h>
  22. #include <brpc/channel.h>
  23. #include "echo.pb.h"
  24. DEFINE_string(server, "0.0.0.0:8500", "IP Address of server");
  25. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  26. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  27. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  28. DEFINE_bool(multi_args, false,
  29. "The ubrpc to be accessed has more than one request and response");
  30. int main(int argc, char* argv[]) {
  31. // Parse gflags. We recommend you to use gflags as well.
  32. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  33. // A Channel represents a communication line to a Server. Notice that
  34. // Channel is thread-safe and can be shared by all threads in your program.
  35. brpc::Channel channel;
  36. // Initialize the channel, NULL means using default options.
  37. brpc::ChannelOptions options;
  38. options.protocol = "ubrpc_compack";
  39. options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
  40. options.max_retry = FLAGS_max_retry;
  41. if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
  42. LOG(ERROR) << "Fail to initialize channel";
  43. return -1;
  44. }
  45. example::EchoService_Stub stub(&channel);
  46. example::EchoRequest request;
  47. example::EchoResponse response;
  48. example::MultiRequests multi_requests;
  49. example::MultiResponses multi_responses;
  50. brpc::Controller cntl;
  51. // Send a request and wait for the response every 1 second.
  52. int log_id = 0;
  53. while (!brpc::IsAskedToQuit()) {
  54. // Reset before reuse.
  55. cntl.Reset();
  56. if (!FLAGS_multi_args) {
  57. request.Clear();
  58. response.Clear();
  59. request.set_message("hello world");
  60. for (int i = (log_id % 7); i > 0; --i) {
  61. example::Object* obj = request.add_objects();
  62. obj->set_id(log_id);
  63. if (log_id % 2 == 0) {
  64. obj->set_value(log_id);
  65. }
  66. if (log_id % 3 == 0) {
  67. obj->set_note("foo");
  68. }
  69. if (log_id % 5 == 0) {
  70. for (int j = (log_id % 3); j > 0; --j) {
  71. example::Parameter* param = obj->add_params();
  72. if (log_id % 2 == 0) {
  73. param->set_x(log_id);
  74. }
  75. if (log_id % 3 == 0) {
  76. param->set_y("bar");
  77. }
  78. if (log_id % 5 == 0) {
  79. param->set_z(log_id);
  80. }
  81. }
  82. }
  83. }
  84. } else {
  85. multi_requests.Clear();
  86. multi_responses.Clear();
  87. multi_requests.mutable_req1()->set_message("hello");
  88. multi_requests.mutable_req2()->set_message("world");
  89. cntl.set_idl_names(brpc::idl_multi_req_multi_res);
  90. }
  91. cntl.set_log_id(log_id ++); // set by user
  92. // Because `done'(last parameter) is NULL, this function waits until
  93. // the response comes back or error occurs(including timedout).
  94. if (!FLAGS_multi_args) {
  95. // [idl] void Echo(EchoRequest req, out EchoResponse res);
  96. stub.Echo(&cntl, &request, &response, NULL);
  97. } else {
  98. // [idl] uint32_t EchoWithMultiArgs(EchoRequest req1, EchoRequest req2,
  99. // out EchoResponse res1, out EchoResponse res2);
  100. stub.EchoWithMultiArgs(&cntl, &multi_requests, &multi_responses, NULL);
  101. }
  102. if (!cntl.Failed()) {
  103. if (!FLAGS_multi_args) {
  104. LOG(INFO) << "Received response from " << cntl.remote_side()
  105. << ": " << response.message()
  106. << " latency=" << cntl.latency_us() << "us";
  107. } else {
  108. LOG(INFO) << "Received response from " << cntl.remote_side()
  109. << ": res1=" << multi_responses.res1().message()
  110. << " res2=" << multi_responses.res2().message()
  111. << " result=" << cntl.idl_result()
  112. << " latency=" << cntl.latency_us() << "us";
  113. }
  114. } else {
  115. LOG(ERROR) << "Fail to send request, " << cntl.ErrorText();
  116. }
  117. sleep(1);
  118. }
  119. LOG(INFO) << "EchoClient is going to quit";
  120. return 0;
  121. }