BServantImp.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "BServantImp.h"
  17. #include "BServer.h"
  18. #include "servant/Application.h"
  19. #include "servant/Communicator.h"
  20. using namespace std;
  21. using namespace tars;
  22. //////////////////////////////////////////////////////
  23. void BServantImp::initialize()
  24. {
  25. //initialize servant here:
  26. //...
  27. _pPrx = Application::getCommunicator()->stringToProxy<AServantPrx>("TestApp.AServer.AServantObj@tcp -h 127.0.0.1 -p 9000 -t 10000");
  28. }
  29. //////////////////////////////////////////////////////
  30. void BServantImp::destroy()
  31. {
  32. }
  33. class AServantCoroCallback : public AServantCoroPrxCallback
  34. {
  35. public:
  36. virtual ~AServantCoroCallback(){}
  37. virtual void callback_testInt(tars::Int32 ret, tars::Int32 iOut)
  38. {
  39. _iRet = ret;
  40. _iOut = iOut;
  41. }
  42. virtual void callback_testInt_exception(tars::Int32 ret)
  43. {
  44. _iException = ret;
  45. }
  46. virtual void callback_testStr(tars::Int32 ret, const std::string& sOut)
  47. {
  48. _iRet = ret;
  49. _sOut = sOut;
  50. }
  51. virtual void callback_testStr_exception(tars::Int32 ret)
  52. {
  53. _iException = ret;
  54. }
  55. public:
  56. int _iException;
  57. int _iRet;
  58. int _iOut;
  59. string _sOut;
  60. };
  61. typedef tars::TC_AutoPtr<AServantCoroCallback> AServantCoroCallbackPtr;
  62. int BServantImp::test(tars::TarsCurrentPtr current) { return 0;}
  63. tars::Int32 BServantImp::testCoroSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
  64. {
  65. try
  66. {
  67. int iRet = -1;
  68. int iIn = 5;
  69. int iOut = 0;
  70. iRet = _pPrx->testInt(iIn, iOut);
  71. if(iRet == 0)
  72. {
  73. string sRet("");
  74. iRet = _pPrx->testStr(sIn, sRet);
  75. if(iRet == 0)
  76. {
  77. sOut = sRet;
  78. }
  79. }
  80. return iRet;
  81. }
  82. catch(exception &ex)
  83. {
  84. TLOGERROR("BServantImp::testCoroSerial exception:" << ex.what() << endl);
  85. }
  86. return -1;
  87. }
  88. tars::Int32 BServantImp::testCoroParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
  89. {
  90. try
  91. {
  92. int iRet = -1;
  93. int iIn = 5;
  94. CoroParallelBasePtr sharedPtr = new CoroParallelBase(2);
  95. AServantCoroCallbackPtr cb1 = new AServantCoroCallback();
  96. cb1->setCoroParallelBasePtr(sharedPtr);
  97. _pPrx->coro_testInt(cb1, iIn);
  98. AServantCoroCallbackPtr cb2 = new AServantCoroCallback();
  99. cb2->setCoroParallelBasePtr(sharedPtr);
  100. _pPrx->coro_testStr(cb2, sIn);
  101. coroWhenAll(sharedPtr);
  102. if(cb1->_iRet == 0 && cb2->_iRet == 0)
  103. {
  104. sOut = cb2->_sOut;
  105. iRet = 0;
  106. }
  107. return iRet;
  108. }
  109. catch(exception &ex)
  110. {
  111. TLOGERROR("BServantImp::testCoroParallel exception:" << ex.what() << endl);
  112. }
  113. return -1;
  114. }