CommunicatorFactory.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_COMMUNICATOR_FACTORY_H_
  17. #define __TARS_COMMUNICATOR_FACTORY_H_
  18. #include "servant/Global.h"
  19. #include "servant/Communicator.h"
  20. namespace tars
  21. {
  22. //////////////////////////////////////////////////////////////////////////////
  23. /**
  24. * 创建CommunicatorPtr对象
  25. */
  26. class CommunicatorFactory : public TC_Singleton<CommunicatorFactory>, public TC_HandleBase, public TC_ThreadRecMutex
  27. {
  28. public:
  29. /**
  30. * 构造函数
  31. * @param comm
  32. */
  33. CommunicatorFactory(){};
  34. /**
  35. * 析构
  36. */
  37. ~CommunicatorFactory(){};
  38. /**
  39. * 获取CommunicatorPtr对象
  40. * @param name
  41. * @return ServantPrx
  42. */
  43. CommunicatorPtr getCommunicator(const string& name = "default")
  44. {
  45. TC_LockT<TC_ThreadRecMutex> lock(*this);
  46. map<string, CommunicatorPtr>::iterator it = _comms.find(name);
  47. if (it == _comms.end())
  48. {
  49. _comms[name] = new Communicator();
  50. it = _comms.find(name);
  51. }
  52. return it->second;
  53. }
  54. /**
  55. * 获取CommunicatorPtr对象
  56. * @param conf
  57. * @param name
  58. * @return ServantPrx
  59. */
  60. CommunicatorPtr getCommunicator(TC_Config& conf, const string& name = "default")
  61. {
  62. TC_LockT<TC_ThreadRecMutex> lock(*this);
  63. map<string, CommunicatorPtr>::iterator it = _comms.find(name);
  64. if (it == _comms.end())
  65. {
  66. _comms[name] = new Communicator(conf);
  67. it = _comms.find(name);
  68. return it->second;
  69. }
  70. string s = "";
  71. it->second->setProperty(conf);
  72. it->second->reloadProperty(s);
  73. return it->second;
  74. }
  75. private:
  76. /**
  77. * 已创建的对象
  78. */
  79. map<string, CommunicatorPtr> _comms;
  80. };
  81. //////////////////////////////////////////////////////
  82. }
  83. #endif