tutorial-07-thrift_thrift_server.cc 1.5 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 <signal.h>
  14. #include "echo_thrift.srpc.h"
  15. #include "workflow/WFFacilities.h"
  16. using namespace srpc;
  17. static WFFacilities::WaitGroup wait_group(1);
  18. class ExampleServiceImpl : public Example::Service
  19. {
  20. public:
  21. void Echo(EchoResult& _return, const std::string& message,
  22. const std::string& name) override
  23. {
  24. _return.message = "Hi back, " + name;
  25. printf("Server Echo()\nreq_message:\n%s\nresp_message:\n%s\n",
  26. message.c_str(), _return.message.c_str());
  27. }
  28. };
  29. static void sig_handler(int signo)
  30. {
  31. wait_group.done();
  32. }
  33. int main()
  34. {
  35. signal(SIGINT, sig_handler);
  36. signal(SIGTERM, sig_handler);
  37. ThriftServer server;
  38. ExampleServiceImpl impl;
  39. server.add_service(&impl);
  40. if (server.start(1412) == 0)
  41. {
  42. printf("SRPC framework Thrift protocol server with thrift IDL is running on 1412\n"
  43. "Try ./thrift_thrift_client to send requests.\n\n");
  44. wait_group.wait();
  45. server.stop();
  46. }
  47. else
  48. perror("server start");
  49. return 0;
  50. }