ServantHelper.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #ifndef __TARS_SERVANT_HELPER_H
  17. #define __TARS_SERVANT_HELPER_H
  18. #include <iostream>
  19. #include <map>
  20. #include <vector>
  21. #include "util/tc_autoptr.h"
  22. #include "util/tc_singleton.h"
  23. #include "util/tc_spin_lock.h"
  24. #include "servant/Servant.h"
  25. namespace tars
  26. {
  27. //////////////////////////////////////////////////////////////////////////////
  28. /**
  29. * Servant
  30. */
  31. class ServantHelperCreation : public TC_HandleBase
  32. {
  33. public:
  34. virtual ServantPtr create(const string &s) = 0;
  35. };
  36. typedef TC_AutoPtr<ServantHelperCreation> ServantHelperCreationPtr;
  37. //////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * Servant
  40. */
  41. template<class T>
  42. struct ServantCreation : public ServantHelperCreation
  43. {
  44. ServantCreation(Application *application) : _application(application){}
  45. ServantPtr create(const string &s) { T *p = new T; p->setName(s); p->setApplication(_application); return p; }
  46. Application *_application;
  47. };
  48. /**
  49. * Servant
  50. */
  51. template<class T, class P>
  52. struct ServantCreationWithParams : public ServantHelperCreation
  53. {
  54. ServantCreationWithParams(Application *application, const P &p) : _application(application), _p(p) {}
  55. ServantPtr create(const string &s) { T *p = new T(_p); p->setName(s); p->setApplication(_application); return p; }
  56. Application *_application;
  57. P _p;
  58. };
  59. //////////////////////////////////////////////////////////////////////////////
  60. //
  61. /**
  62. * Servant管理
  63. */
  64. class SVT_DLL_API ServantHelperManager// : public TC_Singleton<ServantHelperManager>
  65. {
  66. public:
  67. /**
  68. * 构造函数
  69. */
  70. ServantHelperManager()
  71. : _isDyeing(false)
  72. {
  73. }
  74. /**
  75. * 添加Servant
  76. * @param T
  77. * @param id
  78. */
  79. template<typename T>
  80. void addServant(const string &id, Application *application, bool check = false)
  81. {
  82. if(check && _servant_adapter.end() == _servant_adapter.find(id))
  83. {
  84. cerr<<"[ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not set conf in the web)]"<<endl;
  85. throw runtime_error("[ServantHelperManager::addServant " + id + " not find adapter.(maybe not set conf in the web)]");
  86. }
  87. _servant_creator[id] = new ServantCreation<T>(application);
  88. }
  89. /**
  90. * 添加Servant
  91. * @param T
  92. * @param id
  93. */
  94. template<typename T, typename P>
  95. void addServant(const string &id, Application *application, const P &p, bool check = false)
  96. {
  97. if(check && _servant_adapter.end() == _servant_adapter.find(id))
  98. {
  99. cerr<<"[TARS]ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not conf in the web)"<<endl;
  100. throw runtime_error("[TARS]ServantHelperManager::addServant " + id + " not find adapter.(maybe not conf in the web)");
  101. }
  102. _servant_creator[id] = new ServantCreationWithParams<T, P>(application, p);
  103. }
  104. /**
  105. * 生成Servant
  106. * @param id
  107. *
  108. * @return ServantPtr
  109. */
  110. ServantPtr create(const string &sAdapter);
  111. /**
  112. * 添加Adapter的Servant
  113. * @param sAdapter
  114. * @param sServant
  115. */
  116. void setAdapterServant(const string &sAdapter, const string &sServant);
  117. /**
  118. * 根据adapter名称获取Servant名称
  119. * @param sAdapter
  120. * @return string
  121. */
  122. const string &getAdapterServant(const string &sAdapter) const
  123. {
  124. static const string s = "(NO TARS PROTOCOL)";
  125. auto it = _adapter_servant.find(sAdapter);
  126. if(it != _adapter_servant.end())
  127. {
  128. return it->second;
  129. }
  130. return s;
  131. }
  132. /**
  133. * 根据servant名称获取adapter名称
  134. * @param sServant
  135. * @return string
  136. */
  137. const string &getServantAdapter(const string& sServant) const
  138. {
  139. static const string s = "";
  140. auto it = _servant_adapter.find(sServant);
  141. if(it != _servant_adapter.end())
  142. {
  143. return it->second;
  144. }
  145. return s;
  146. }
  147. /**
  148. * 获取Adapter/Servant对应表
  149. * @return map<string, string>
  150. */
  151. const map<string, string> &getAdapterServant() const {return _adapter_servant;}
  152. /**
  153. * 设置染色信息
  154. * @param params: notify的输入参数
  155. * @param sDyeingKey: 用户号码
  156. * @param sDyeingServant: 对象名称
  157. * @param sDyeingInterface:接口名称
  158. * @return string: 设置结果
  159. */
  160. bool setDyeing(const string & sDyeingKey, const string & sDyeingServant, const string & sDyeingInterface);
  161. /**
  162. * 是否是染色的请求
  163. * @param sKey: 用户号码
  164. * @param sServant: 对象名称
  165. * @param sInterface:接口名称
  166. * @return string: 设置结果
  167. */
  168. bool isDyeingReq(const string & sKey, const string & sServant, const string & sInterface) const;
  169. /**
  170. * 是否是已经被染色
  171. */
  172. bool isDyeing() const {return _isDyeing;}
  173. protected:
  174. /**
  175. * Servant生成类
  176. */
  177. map<string, ServantHelperCreationPtr> _servant_creator;
  178. /**
  179. * Adapter包含的Servant(Adapter名称:servant名称)
  180. */
  181. map<string, string> _adapter_servant;
  182. /**
  183. * Adapter包含的Servant(Servant名称:Adapter名称)
  184. */
  185. map<string, string> _servant_adapter;
  186. protected:
  187. /**
  188. * 锁
  189. */
  190. TC_SpinLock _mutex;
  191. /**
  192. * 是否染色
  193. */
  194. bool _isDyeing;
  195. /**
  196. * 染色用户号码
  197. */
  198. string _dyeingKey;
  199. /**
  200. * 染色的servant对象
  201. */
  202. string _dyeingServant;
  203. /**
  204. * 染色的接口
  205. */
  206. string _dyeingInterface;
  207. };
  208. }
  209. #endif