proxy_main_proto.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include "workflow/WFFacilities.h"
  4. #include "srpc/rpc_types.h"
  5. #include "config/config.h"
  6. #include "%s.srpc.h"
  7. using namespace srpc;
  8. static srpc::RPCConfig config;
  9. static WFFacilities::WaitGroup wait_group(1);
  10. static void sig_handler(int signo)
  11. {
  12. wait_group.done();
  13. }
  14. static 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. class ProxyServiceImpl : public %s::Service
  25. {
  26. public:
  27. void Echo(EchoRequest *request, EchoResponse *response,
  28. RPCContext *context) override
  29. {
  30. fprintf(stderr, "%s proxy get request from client. ip : %%s\n%%s\n",
  31. context->get_remote_ip().c_str(), request->DebugString().c_str());
  32. // 5. process() : get request from client and send to remote server
  33. auto *task = this->client.create_Echo_task([response](EchoResponse *resp,
  34. RPCContext *ctx) {
  35. // 6. callback() : fill remote server response to client
  36. if (ctx->success())
  37. *response = std::move(*resp);
  38. });
  39. task->serialize_input(request);
  40. context->get_series()->push_back(task);
  41. }
  42. public:
  43. ProxyServiceImpl(RPCClientParams *params) :
  44. client(params)
  45. {
  46. }
  47. private:
  48. %s::%sClient client;
  49. };
  50. int main()
  51. {
  52. // 1. load config
  53. init();
  54. // 2. make client for remote server
  55. RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
  56. client_params.host = config.client_host();
  57. client_params.port = config.client_port();
  58. // 3. start proxy server
  59. %sServer server;
  60. ProxyServiceImpl impl(&client_params);
  61. server.add_service(&impl);
  62. config.load_filter(server);
  63. if (server.start(config.server_port()) == 0)
  64. {
  65. // 4. main thread success and wait
  66. fprintf(stderr, "%s [%s]-[%s] proxy started, port %%u\n",
  67. config.server_port());
  68. wait_group.wait();
  69. server.stop();
  70. }
  71. else
  72. perror("server start");
  73. return 0;
  74. }