proxy_main.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include "workflow/WFTaskFactory.h"
  4. #include "workflow/WF%sServer.h"
  5. #include "workflow/WFFacilities.h"
  6. #include "config/config.h"
  7. #include "config/util.h"
  8. static WFFacilities::WaitGroup wait_group(1);
  9. static srpc::RPCConfig config;
  10. void sig_handler(int signo)
  11. {
  12. wait_group.done();
  13. }
  14. void init()
  15. {
  16. if (config.load("./proxy.conf") == false)
  17. {
  18. perror("Load config failed");
  19. exit(1);
  20. }
  21. signal(SIGINT, sig_handler);
  22. signal(SIGTERM, sig_handler);
  23. }
  24. void callback(WF%sTask *client_task)
  25. {
  26. int state = client_task->get_state();
  27. int error = client_task->get_error();
  28. SeriesWork *series = series_of(client_task);
  29. protocol::%sResponse *resp = client_task->get_resp();
  30. protocol::%sResponse *proxy_resp = (protocol::%sResponse *)series->get_context();
  31. // Copy the remote server's response, to proxy response.
  32. if (state == WFT_STATE_SUCCESS)%s
  33. fprintf(stderr, "backend server state = %%d error = %%d. response client.\n",
  34. state, error);
  35. }
  36. void process(WF%sTask *server_task)
  37. {
  38. protocol::%sRequest *req = server_task->get_req();
  39. std::string backend_server = config.client_host();
  40. unsigned short backend_server_port = config.client_port();
  41. std::string url = std::string("%s://") + backend_server +
  42. std::string(":") + std::to_string(backend_server_port);
  43. WF%sTask *client_task = WFTaskFactory::create_%s_task(url,%s
  44. config.retry_max(),
  45. callback);
  46. // Copy user's request to the new task's request using std::move()
  47. %s
  48. SeriesWork *series = series_of(server_task);
  49. series->set_context(server_task->get_resp());
  50. series->push_back(client_task);
  51. fprintf(stderr, "proxy get request from client: ");
  52. print_peer_address(server_task);
  53. }
  54. int main()
  55. {
  56. init();
  57. WF%sServer proxy_server(process);
  58. if (proxy_server.start(config.server_port()) == 0)
  59. {
  60. fprintf(stderr, "[%s]-[%s] proxy started, port %%u\n", config.server_port());
  61. wait_group.wait();
  62. proxy_server.stop();
  63. }
  64. else
  65. perror("server start");
  66. return 0;
  67. }