tutorial-09-client_task.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright (c) 2020 sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <workflow/WFTaskFactory.h>
  15. #include <workflow/WFOperator.h>
  16. #include "echo_pb.srpc.h"
  17. #include "workflow/WFFacilities.h"
  18. using namespace srpc;
  19. static WFFacilities::WaitGroup wait_group(1);
  20. int main()
  21. {
  22. Example::SRPCClient client("127.0.0.1", 1412);
  23. EchoRequest req;
  24. req.set_message("Hello, sogou rpc!");
  25. req.set_name("1412");
  26. auto *rpc_task = client.create_Echo_task([](EchoResponse *response, RPCContext *ctx) {
  27. if (ctx->success())
  28. printf("%s\n", response->DebugString().c_str());
  29. else
  30. printf("status[%d] error[%d] errmsg:%s\n",
  31. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  32. wait_group.done();
  33. });
  34. auto *http_task = WFTaskFactory::create_http_task("https://www.sogou.com", 0, 0, [](WFHttpTask *task) {
  35. if (task->get_state() == WFT_STATE_SUCCESS)
  36. {
  37. std::string body;
  38. const void *data;
  39. size_t len;
  40. task->get_resp()->get_parsed_body(&data, &len);
  41. body.assign((const char *)data, len);
  42. printf("%s\n\n", body.c_str());
  43. }
  44. else
  45. printf("Http request fail\n\n");
  46. });
  47. rpc_task->serialize_input(&req);
  48. (*http_task > rpc_task).start();
  49. wait_group.wait();
  50. return 0;
  51. }