client_thrift.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include "workflow/WFFacilities.h"
  3. #include "srpc/rpc_types.h"
  4. #include "%s.srpc.h"
  5. #include "config/config.h"
  6. using namespace srpc;
  7. static WFFacilities::WaitGroup wait_group(1);
  8. static srpc::RPCConfig config;
  9. void init()
  10. {
  11. if (config.load("./client.conf") == false)
  12. {
  13. perror("Load config failed");
  14. exit(1);
  15. }
  16. }
  17. int main()
  18. {
  19. // 1. load config
  20. init();
  21. // 2. start client
  22. RPCClientParams params = RPC_CLIENT_PARAMS_DEFAULT;
  23. params.host = config.client_host();
  24. params.port = config.client_port();
  25. %s
  26. %s::%sClient client(&params);
  27. config.load_filter(client);
  28. // 3. request with sync api
  29. EchoResult res;
  30. client.Echo(res, "Hello, this is sync request!");
  31. if (client.thrift_last_sync_success())
  32. fprintf(stderr, "sync resp. %%s\n", res.message.c_str());
  33. else
  34. {
  35. const auto& sync_ctx = client.thrift_last_sync_ctx();
  36. fprintf(stderr, "sync status[%%d] error[%%d] errmsg:%%s\n",
  37. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  38. }
  39. // 4. request with async api
  40. %s::EchoRequest req;
  41. req.message = "Hello, this is async request!";
  42. client.Echo(&req, [](%s::EchoResponse *resp, RPCContext *ctx) {
  43. if (ctx->success())
  44. fprintf(stderr, "async resp. %%s\n", resp->result.message.c_str());
  45. else
  46. fprintf(stderr, "async status[%%d] error[%%d] errmsg:%%s\n",
  47. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  48. wait_group.done();
  49. });
  50. wait_group.wait();
  51. return 0;
  52. }