KeepAliveNodeF.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "util/tc_platform.h"
  17. #include "util/tc_port.h"
  18. #include "servant/KeepAliveNodeF.h"
  19. #include "servant/RemoteLogger.h"
  20. #include "servant/Communicator.h"
  21. namespace tars
  22. {
  23. void KeepAliveNodeFHelper::setNodeInfo(const CommunicatorPtr &comm, const string &obj, const string &app, const string &server)
  24. {
  25. _comm = comm;
  26. if(!obj.empty())
  27. {
  28. _nodePrx = _comm->stringToProxy<ServerFPrx>(obj);
  29. }
  30. _si.application = app;
  31. _si.serverName = server;
  32. _si.pid = TC_Port::getpid();
  33. }
  34. void KeepAliveNodeFHelper::keepAlive(const string &adapter)
  35. {
  36. try
  37. {
  38. if(_nodePrx)
  39. {
  40. set<string> s;
  41. {
  42. TC_LockT<TC_ThreadMutex> lock(*this);
  43. _adapterSet.insert(adapter);
  44. //admin心跳来的时候才上报(减小上报次数)
  45. if(!adapter.empty() && adapter != "AdminAdapter")
  46. {
  47. return;
  48. }
  49. s.swap(_adapterSet);
  50. }
  51. ServerInfo si = _si;
  52. set<string>::const_iterator it = s.begin();
  53. while(it != s.end())
  54. {
  55. si.adapter = *it;
  56. _nodePrx->async_keepAlive(NULL,si);
  57. ++it;
  58. }
  59. }
  60. }
  61. catch(exception &ex)
  62. {
  63. TLOGERROR("KeepAliveNodeFHelper::keepAlive error:" << ex.what() << endl);
  64. }
  65. catch(...)
  66. {
  67. TLOGERROR("KeepAliveNodeFHelper::keepAlive unknown error" << endl);
  68. }
  69. }
  70. void KeepAliveNodeFHelper::keepActiving()
  71. {
  72. try
  73. {
  74. if(_nodePrx)
  75. {
  76. _nodePrx->async_keepActiving(NULL, _si);
  77. }
  78. }
  79. catch(exception &ex)
  80. {
  81. TLOGERROR("[KeepAliveNodeFHelper::keepAlive error:" << ex.what() << "]" << endl);
  82. }
  83. catch(...)
  84. {
  85. TLOGERROR("[KeepAliveNodeFHelper::keepAlive unknown error]" << endl);
  86. }
  87. }
  88. void KeepAliveNodeFHelper::reportVersion(const string &version)
  89. {
  90. try
  91. {
  92. if(_nodePrx)
  93. {
  94. _nodePrx->async_reportVersion(NULL, _si.application, _si.serverName, version);
  95. }
  96. }
  97. catch(exception &ex)
  98. {
  99. TLOGERROR("[KeepAliveNodeFHelper::reportVersion error:" << ex.what() << "]" << endl);
  100. }
  101. catch(...)
  102. {
  103. TLOGERROR("[KeepAliveNodeFHelper::reportVersion unknown error" << "]" << endl);
  104. }
  105. }
  106. }