1
0

client.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 server every 1 second.
  18. #include <gflags/gflags.h>
  19. #include <butil/logging.h>
  20. #include <butil/time.h>
  21. #include <brpc/channel.h>
  22. #include <brpc/policy/hulu_pbrpc_controller.h>
  23. #include "echo.pb.h"
  24. DEFINE_string(attachment, "", "Carry this along with requests");
  25. DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
  26. DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
  27. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  28. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  29. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  30. DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");
  31. int main(int argc, char* argv[]) {
  32. // Parse gflags. We recommend you to use gflags as well.
  33. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  34. // A Channel represents a communication line to a Server. Notice that
  35. // Channel is thread-safe and can be shared by all threads in your program.
  36. brpc::Channel channel;
  37. // Initialize the channel, NULL means using default options.
  38. brpc::ChannelOptions options;
  39. options.protocol = "hulu_pbrpc";
  40. options.connection_type = FLAGS_connection_type;
  41. options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
  42. options.max_retry = FLAGS_max_retry;
  43. if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
  44. LOG(ERROR) << "Fail to initialize channel";
  45. return -1;
  46. }
  47. // Normally, you should not call a Channel directly, but instead construct
  48. // a stub Service wrapping it. stub can be shared by all threads as well.
  49. example::EchoService_Stub stub(&channel);
  50. // Send a request and wait for the response every 1 second.
  51. int log_id = 0;
  52. while (!brpc::IsAskedToQuit()) {
  53. // We will receive response synchronously, safe to put variables
  54. // on stack.
  55. example::EchoRequest request;
  56. example::EchoResponse response;
  57. brpc::policy::HuluController cntl;
  58. request.set_message("hello world");
  59. cntl.set_log_id(log_id++); // set by user
  60. cntl.request_attachment().append(FLAGS_attachment);
  61. cntl.set_request_source_addr(log_id++);
  62. cntl.set_request_user_data("client user data");
  63. // Because `done'(last parameter) is NULL, this function waits until
  64. // the response comes back or error occurs(including timedout).
  65. stub.Echo(&cntl, &request, &response, NULL);
  66. if (!cntl.Failed()) {
  67. LOG(INFO) << "Received response from " << cntl.remote_side()
  68. << " to " << cntl.local_side()
  69. << ": " << response.message() << " (attached="
  70. << cntl.response_attachment() << ")"
  71. << " response_source_addr=" << cntl.response_source_addr()
  72. << " response_user_data=\"" << cntl.response_user_data()
  73. << "\" latency=" << cntl.latency_us() << "us";
  74. } else {
  75. LOG(WARNING) << cntl.ErrorText();
  76. }
  77. usleep(FLAGS_interval_ms * 1000L);
  78. }
  79. LOG(INFO) << "EchoClient is going to quit";
  80. return 0;
  81. }