AServantImp.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "AServantImp.h"
  17. #include "AServer.h"
  18. #include "servant/Application.h"
  19. #include "servant/Communicator.h"
  20. using namespace std;
  21. using namespace tars;
  22. //////////////////////////////////////////////////////
  23. class BServantCallback : public BServantPrxCallback
  24. {
  25. public:
  26. BServantCallback(TarsCurrentPtr &current)
  27. : _current(current)
  28. {}
  29. BServantCallback(TarsCurrentPtr &current, const promise::Promise<std::string> &promise)
  30. : _current(current)
  31. , _promise(promise)
  32. {}
  33. void callback_queryResult(tars::Int32 ret, const std::string &sOut)
  34. {
  35. if(ret == 0)
  36. {
  37. _promise.setValue(sOut);
  38. }
  39. else
  40. {
  41. handExp("callback_queryResult", ret);
  42. }
  43. }
  44. void callback_queryResult_exception(tars::Int32 ret)
  45. {
  46. handExp("callback_queryResult_exception", ret);
  47. }
  48. private:
  49. void handExp(const std::string &sFuncName, tars::Int32 ret)
  50. {
  51. string s("sFuncName:");
  52. s += sFuncName;
  53. s += "|ret:";
  54. s += TC_Common::tostr(ret);
  55. _promise.setException(promise::copyException(s));
  56. TLOGDEBUG("ServerPrxCallback handExp:" << s << endl);
  57. }
  58. private:
  59. TarsCurrentPtr _current;
  60. promise::Promise<std::string> _promise;
  61. };
  62. //////////////////////////////////////////////////////
  63. class CServantCallback : public CServantPrxCallback
  64. {
  65. public:
  66. CServantCallback(TarsCurrentPtr &current)
  67. : _current(current)
  68. {}
  69. CServantCallback(TarsCurrentPtr &current, const promise::Promise<std::string> &promise)
  70. : _current(current)
  71. , _promise(promise)
  72. {}
  73. void callback_queryResult(tars::Int32 ret, const std::string &sOut)
  74. {
  75. if(ret == 0)
  76. {
  77. _promise.setValue(sOut);
  78. }
  79. else
  80. {
  81. handExp("callback_queryResult", ret);
  82. }
  83. }
  84. void callback_queryResult_exception(tars::Int32 ret)
  85. {
  86. handExp("callback_queryResult_exception", ret);
  87. }
  88. private:
  89. void handExp(const std::string &sFuncName, tars::Int32 ret)
  90. {
  91. string s("sFuncName:");
  92. s += sFuncName;
  93. s += "|ret:";
  94. s += TC_Common::tostr(ret);
  95. _promise.setException(promise::copyException(s));
  96. TLOGDEBUG("ServerPrxCallback handExp:" << s << endl);
  97. }
  98. private:
  99. TarsCurrentPtr _current;
  100. promise::Promise<std::string> _promise;
  101. };
  102. //////////////////////////////////////////////////////
  103. promise::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
  104. {
  105. promise::Promise<std::string> promise;
  106. Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise);
  107. prx->async_queryResult(cb, sIn);
  108. return promise.getFuture();
  109. }
  110. //////////////////////////////////////////////////////
  111. promise::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
  112. {
  113. promise::Promise<std::string> promise;
  114. Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise);
  115. prx->async_queryResult(cb, sIn);
  116. return promise.getFuture();
  117. }
  118. //////////////////////////////////////////////////////
  119. promise::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future<std::string>& future)
  120. {
  121. std::string sResult("");
  122. std::string sException("");
  123. try
  124. {
  125. sResult = future.get();
  126. return sendCReq(prx, sResult, current);
  127. }
  128. catch (exception& e)
  129. {
  130. TLOGDEBUG("Exception:" << e.what() << endl);
  131. sException = e.what();
  132. }
  133. promise::Promise<std::string> promise;
  134. promise.setValue(sException);
  135. return promise.getFuture();
  136. }
  137. //////////////////////////////////////////////////////
  138. int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<std::string>& future)
  139. {
  140. int ret = 0;
  141. std::string sResult("");
  142. try
  143. {
  144. sResult = future.get();
  145. }
  146. catch (exception& e)
  147. {
  148. ret = -1;
  149. sResult = e.what();
  150. TLOGDEBUG("Exception:" << e.what() << endl);
  151. }
  152. AServant::async_response_queryResultSerial(current, ret, sResult);
  153. return 0;
  154. }
  155. //////////////////////////////////////////////////////
  156. int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > >& allFuture)
  157. {
  158. int ret = 0;
  159. std::string sResult("");
  160. try
  161. {
  162. promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > tupleFuture = allFuture.get();
  163. std::string sResult1 = tupleFuture.get<0>().get();
  164. std::string sResult2 = tupleFuture.get<1>().get();
  165. sResult = sResult1;
  166. sResult += "|";
  167. sResult += sResult2;
  168. }
  169. catch (exception& e)
  170. {
  171. ret = -1;
  172. sResult = e.what();
  173. TLOGDEBUG("Exception:" << e.what() << endl);
  174. }
  175. AServant::async_response_queryResultParallel(current, ret, sResult);
  176. return 0;
  177. }
  178. //////////////////////////////////////////////////////
  179. void AServantImp::initialize()
  180. {
  181. //initialize servant here:
  182. //...
  183. _pPrxB = Application::getCommunicator()->stringToProxy<BServantPrx>("Test.BServer.BServantObj");
  184. _pPrxC = Application::getCommunicator()->stringToProxy<CServantPrx>("Test.CServer.CServantObj");
  185. }
  186. //////////////////////////////////////////////////////
  187. void AServantImp::destroy()
  188. {
  189. }
  190. //////////////////////////////////////////////////////
  191. tars::Int32 AServantImp::queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
  192. {
  193. current->setResponse(false);
  194. promise::Future<std::string> f = sendBReq(_pPrxB, sIn, current);
  195. f.then(tars::TC_Bind(&handleBRspAndSendCReq, _pPrxC, current)).then(tars::TC_Bind(&handleCRspAndReturnClient, current));
  196. return 0;
  197. }
  198. //////////////////////////////////////////////////////
  199. tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
  200. {
  201. current->setResponse(false);
  202. promise::Future<std::string> f1 = sendBReq(_pPrxB, sIn, current);
  203. promise::Future<std::string> f2 = sendCReq(_pPrxC, sIn, current);
  204. promise::Future<promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > > f_all = promise::whenAll(f1, f2);
  205. f_all.then(tars::TC_Bind(&handleBCRspAndReturnClient, current));
  206. return 0;
  207. }