Servant.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "servant/Servant.h"
  17. #include "servant/BaseF.h"
  18. #include "servant/Application.h"
  19. #include "servant/AppCache.h"
  20. #include "servant/RemoteLogger.h"
  21. #include <cerrno>
  22. namespace tars
  23. {
  24. thread_local shared_ptr<CallbackThreadData> CallbackThreadData::g_sp;
  25. Servant::Servant():_handle(NULL)
  26. {
  27. }
  28. Servant::~Servant()
  29. {
  30. }
  31. void Servant::setName(const string &name)
  32. {
  33. _name = name;
  34. }
  35. string Servant::getName() const
  36. {
  37. return _name;
  38. }
  39. void Servant::setApplication(Application *application)
  40. {
  41. _application = application;
  42. setNotifyObserver(application->getNotifyObserver());
  43. }
  44. Application* Servant::getApplication() const
  45. {
  46. return _application;
  47. }
  48. void Servant::setHandle(TC_EpollServer::Handle* handle)
  49. {
  50. _handle = handle;
  51. }
  52. TC_EpollServer::Handle* Servant::getHandle()
  53. {
  54. return _handle;
  55. }
  56. int Servant::dispatch(CurrentPtr current, vector<char> &buffer)
  57. {
  58. int ret = TARSSERVERUNKNOWNERR;
  59. if (current->getFuncName() == "tars_ping")
  60. {
  61. TLOGTARS("[Servant::dispatch] tars_ping ok from [" << current->getIp() << ":" << current->getPort() << "]" << endl);
  62. ret = TARSSERVERSUCCESS;
  63. }
  64. else if (!current->getBindAdapter()->isTarsProtocol())
  65. {
  66. TC_LockT<TC_ThreadRecMutex> lock(*this);
  67. ret = doRequest(current, buffer);
  68. }
  69. else
  70. {
  71. TC_LockT<TC_ThreadRecMutex> lock(*this);
  72. ret = onDispatch(current, buffer);
  73. }
  74. return ret;
  75. }
  76. TC_CasQueue<ReqMessagePtr>& Servant::getResponseQueue()
  77. {
  78. return _asyncResponseQueue;
  79. }
  80. ///////////////////////////////////////////////////////////////////////////
  81. ServantCallback::ServantCallback(const string& type, const ServantPtr& servant, const CurrentPtr& current)
  82. : _servant(servant)
  83. , _current(current)
  84. {
  85. ServantProxyCallback::setType(type);
  86. }
  87. int ServantCallback::onDispatch(ReqMessagePtr msg)
  88. {
  89. _servant->getResponseQueue().push_back(msg);
  90. _servant->getHandle()->notifyFilter();
  91. return 0;
  92. }
  93. const ServantPtr& ServantCallback::getServant()
  94. {
  95. return _servant;
  96. }
  97. const CurrentPtr& ServantCallback::getCurrent()
  98. {
  99. return _current;
  100. }
  101. ///////////////////////////////////////////////////////////////////////////
  102. CallbackThreadData::CallbackThreadData():_contextValid(false)
  103. {
  104. }
  105. // void CallbackThreadData::destructor(void* p)
  106. // {
  107. // CallbackThreadData * pCbtd = (CallbackThreadData*)p;
  108. // if(pCbtd)
  109. // delete pCbtd;
  110. // }
  111. CallbackThreadData * CallbackThreadData::getData()
  112. {
  113. if(!g_sp)
  114. {
  115. g_sp.reset(new CallbackThreadData());
  116. }
  117. return g_sp.get();
  118. }
  119. void CallbackThreadData::setResponseContext(const map<std::string, std::string> & context)
  120. {
  121. _contextValid = true;
  122. if(context.empty())
  123. {
  124. _responseContext.clear();
  125. }
  126. else
  127. {
  128. _responseContext = context;
  129. }
  130. }
  131. map<std::string, std::string> & CallbackThreadData::getResponseContext()
  132. {
  133. return _responseContext;
  134. }
  135. void CallbackThreadData::delResponseContext()
  136. {
  137. _contextValid = false;
  138. _responseContext.clear();
  139. }
  140. ////////////////////////////////////////////////////////////////////////
  141. }