call_rpc.cpp 839 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "call_rpc.h"
  2. #include <stdexcept>
  3. #define RPC_NOT_INIT "Please init call_rpc before use."
  4. #define RPC_PROTOCOL "udp"
  5. #define RPC_CLIENT_INIT_ERROR "RPC client initialization failed."
  6. #define FAILED_CALL_ERROR "call failed"
  7. using std::logic_error;
  8. using std::runtime_error;
  9. call_rpc::call_rpc()
  10. {
  11. clnt = nullptr;
  12. }
  13. call_rpc::~call_rpc()
  14. {
  15. }
  16. void call_rpc::init(char *hostname)
  17. {
  18. clnt = clnt_create(hostname, RPC_PROG, RPC_VERS, RPC_PROTOCOL);
  19. if (clnt == nullptr) {
  20. clnt_pcreateerror(hostname);
  21. throw runtime_error(RPC_CLIENT_INIT_ERROR);
  22. }
  23. }
  24. response *call_rpc::get_response(request req)
  25. {
  26. if (clnt == nullptr)
  27. throw logic_error(RPC_NOT_INIT);
  28. response *res = rpc_call_1(&req, clnt);
  29. if (res == nullptr) {
  30. clnt_perror(clnt, FAILED_CALL_ERROR);
  31. throw runtime_error(FAILED_CALL_ERROR);
  32. }
  33. return res;
  34. }