RemoteNotify.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/RemoteNotify.h"
  17. #include "servant/Communicator.h"
  18. #include "servant/RemoteLogger.h"
  19. #include "servant/Application.h"
  20. namespace tars
  21. {
  22. int RemoteNotify::setNotifyInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string &sSetName, const string &nodeName)
  23. {
  24. _comm = comm;
  25. if(!obj.empty())
  26. {
  27. _notifyPrx = _comm->stringToProxy<NotifyPrx>(obj);
  28. _notifyPrx->tars_timeout(500);
  29. }
  30. _setName = sSetName;
  31. _app = app;
  32. _serverName = serverName;
  33. _nodeName = nodeName;
  34. return 0;
  35. }
  36. void RemoteNotify::report(const string &sResult, bool bSync)
  37. {
  38. try
  39. {
  40. if(_notifyPrx)
  41. {
  42. ReportInfo info;
  43. info.eType = REPORT;
  44. info.sApp = _app;
  45. info.sServer = _serverName;
  46. info.sSet = _setName;
  47. info.sThreadId = TC_Common::tostr(std::this_thread::get_id());
  48. info.sMessage = sResult;
  49. info.sNodeName = _nodeName;
  50. if(!bSync)
  51. {
  52. _notifyPrx->async_reportNotifyInfo(NULL, info, ServerConfig::Context);
  53. }
  54. else
  55. {
  56. _notifyPrx->reportNotifyInfo(info, ServerConfig::Context);
  57. }
  58. }
  59. }
  60. catch(exception &ex)
  61. {
  62. TLOGERROR("[RemoteNotify::report error:" << ex.what() << "]" << endl);
  63. }
  64. catch(...)
  65. {
  66. TLOGERROR("[RemoteNotify::report unknown error" << "]" << endl);
  67. }
  68. }
  69. void RemoteNotify::notify(NOTIFYLEVEL level, const string &sMessage)
  70. {
  71. try
  72. {
  73. if(_notifyPrx)
  74. {
  75. ReportInfo info;
  76. info.eType = NOTIFY;
  77. info.sApp = _app;
  78. info.sServer = _serverName;
  79. info.sSet = _setName;
  80. info.sThreadId = TC_Common::tostr(std::this_thread::get_id());
  81. info.sMessage = sMessage;
  82. info.eLevel = level;
  83. info.sNodeName = _nodeName;
  84. _notifyPrx->async_reportNotifyInfo(NULL, info, ServerConfig::Context);
  85. }
  86. }
  87. catch(exception &ex)
  88. {
  89. TLOGERROR("[RemoteNotify::notify error:" << ex.what() << "]" << endl);
  90. }
  91. catch(...)
  92. {
  93. TLOGERROR("[RemoteNotify::notify unknown error" << "]" << endl);
  94. }
  95. }
  96. void RemoteNotify::report(const string &sMessage, const string & app, const string &serverName, const string &sNodeName)
  97. {
  98. try
  99. {
  100. if(_notifyPrx)
  101. {
  102. ReportInfo info;
  103. info.eType = REPORT;
  104. info.sApp = app;
  105. info.sServer = serverName;
  106. info.sSet = "";
  107. info.sMessage = sMessage;
  108. info.sNodeName = sNodeName;
  109. _notifyPrx->async_reportNotifyInfo(NULL, info, ServerConfig::Context);
  110. }
  111. }
  112. catch(exception &ex)
  113. {
  114. TLOGERROR("[RemoteNotify::notify error:" << ex.what() << "]" << endl);
  115. }
  116. catch(...)
  117. {
  118. TLOGERROR("[RemoteNotify::notify unknown error" << "]" << endl);
  119. }
  120. }
  121. }