server_protobuf.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 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("./server.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 ServiceImpl : public %s::Service
  25. {
  26. public:
  27. void Echo(EchoRequest *req, EchoResponse *resp, RPCContext *ctx) override
  28. {
  29. %s// 4. delete the following codes and fill your logic
  30. fprintf(stderr, "get req. %%s\n", req->DebugString().c_str());
  31. resp->set_message("Hi back");
  32. }
  33. };
  34. int main()
  35. {
  36. // 1. load config
  37. init();
  38. // 2. start server
  39. %sServer server;
  40. ServiceImpl impl;
  41. server.add_service(&impl);
  42. config.load_filter(server);
  43. if (server.start(config.server_port()) == 0)
  44. {
  45. // 3. success and wait
  46. fprintf(stderr, "%s %s server started, port %%u\n", config.server_port());
  47. wait_group.wait();
  48. server.stop();
  49. }
  50. else
  51. perror("server start");
  52. return 0;
  53. }