rpc.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/wrapper.h"
  19. #include "common/util.h"
  20. #include "rpc/rpc_engine.h"
  21. #include <asio/ip/tcp.hpp>
  22. using namespace ::hdfs;
  23. using ::asio::ip::tcp;
  24. RpcEngine * RpcEngine_create(IoServiceImpl * io_service,
  25. std::string client_name, std::string protocol,
  26. int version) {
  27. RpcEngine *engine = new RpcEngine(&io_service->io_service(), client_name,
  28. protocol.c_str(), version);
  29. return engine;
  30. }
  31. std::string RpcEngine_connect(RpcEngine* self, std::string host, int port) {
  32. tcp::endpoint ep(asio::ip::address::from_string(host), port);
  33. std::vector<tcp::endpoint> server(1, ep);
  34. Status status = self->Connect(server);
  35. return status.ToString();
  36. }
  37. void RpcEngine_start(RpcEngine *self) {
  38. self->Start();
  39. }
  40. std::string RpcEngine_rpc(RpcEngine *self, std::string method,
  41. std::string request, std::string status) {
  42. auto response = std::make_shared<std::string>();
  43. Status stat = self->RawRpc(method, std::move(request), response);
  44. if (!stat.ok()) {
  45. printf(("[ERR]" + stat.ToString()).c_str());
  46. return nullptr;
  47. }
  48. std::string resp = response.get()->c_str();
  49. return resp;
  50. }
  51. void RpcEngine_destroy(RpcEngine * handle) {
  52. delete handle;
  53. }