NotifyObserver.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/NotifyObserver.h"
  17. #include "servant/BaseNotify.h"
  18. namespace tars
  19. {
  20. NotifyObserver::NotifyObserver()
  21. {
  22. }
  23. NotifyObserver::~NotifyObserver()
  24. {
  25. }
  26. void NotifyObserver::registerObject(const string& command, BaseNotify* obj, map<string, set<BaseNotify*> >& target)
  27. {
  28. TC_LockT<TC_ThreadRecMutex> lock(*this);
  29. target[command].insert(obj);
  30. }
  31. void NotifyObserver::unregisterObject(const string& command, BaseNotify* obj, map<string, set<BaseNotify*> >& target)
  32. {
  33. TC_LockT<TC_ThreadRecMutex> lock(*this);
  34. map<string, set<BaseNotify*> >::iterator it;
  35. it = target.find(command);
  36. if (it != target.end())
  37. {
  38. set<BaseNotify*>::iterator sit = it->second.find(obj);
  39. if (sit != it->second.end())
  40. {
  41. it->second.erase(sit);
  42. }
  43. if (it->second.empty())
  44. {
  45. target.erase(it);
  46. }
  47. }
  48. }
  49. void NotifyObserver::registerNotify(const string& command, BaseNotify* obj)
  50. {
  51. registerObject(command, obj, _notifys);
  52. }
  53. void NotifyObserver::unregisterNotify(const string& command, BaseNotify* obj)
  54. {
  55. unregisterObject(command, obj, _notifys);
  56. }
  57. void NotifyObserver::registerPrefix(const string& command, BaseNotify* obj)
  58. {
  59. registerObject(command, obj, _prefix);
  60. }
  61. void NotifyObserver::unregisterPrefix(const string& command, BaseNotify* obj)
  62. {
  63. unregisterObject(command, obj, _prefix);
  64. }
  65. string NotifyObserver::notify(const string& command, CurrentPtr current)
  66. {
  67. TC_LockT<TC_ThreadRecMutex> lock(*this);
  68. string str = TC_Common::trim(command);
  69. string name = str;
  70. string params = "";
  71. string::size_type pos = str.find_first_of(" ");
  72. if (pos != string::npos)
  73. {
  74. name = str.substr(0, pos);
  75. params = str.substr(pos + 1);
  76. }
  77. ostringstream os;
  78. map<string, set<BaseNotify*> >::iterator it = _prefix.find(name);
  79. if (it != _prefix.end())
  80. {
  81. set<BaseNotify*>& sbn = it->second;
  82. os << "[notify prefix object num:" << sbn.size() << "]" << endl;
  83. int i = 0;
  84. for (set<BaseNotify*>::iterator sit = sbn.begin(); sit != sbn.end(); ++sit)
  85. {
  86. string result = "";
  87. if ((*sit)->notify(name, params, current, result))
  88. {
  89. os << "[" << ++i << "]:" << result << endl;
  90. }
  91. else
  92. {
  93. os << "[notify break by server]:" << endl;
  94. os << result << endl;
  95. return os.str();
  96. }
  97. }
  98. }
  99. it = _notifys.find(name);
  100. if (it != _notifys.end())
  101. {
  102. set<BaseNotify*>& sbn = it->second;
  103. os << "[notify servant object num:" << sbn.size() << "]" << endl;
  104. int i = 0;
  105. for (set<BaseNotify*>::iterator sit = sbn.begin(); sit != sbn.end(); ++sit)
  106. {
  107. string result = "";
  108. if ((*sit)->notify(name, params, current, result))
  109. {
  110. os << "[" << ++i << "]:" << result << endl;
  111. }
  112. else
  113. {
  114. os << "[notify break.]" << endl;
  115. os << result << endl;
  116. return os.str();
  117. }
  118. }
  119. }
  120. return os.str();
  121. }
  122. string NotifyObserver::viewRegisterCommand()
  123. {
  124. TC_LockT<TC_ThreadRecMutex> lock(*this);
  125. ostringstream os;
  126. map<string, set<BaseNotify*> >::iterator it = _prefix.begin();
  127. set<string> command;
  128. while (it != _prefix.end())
  129. {
  130. command.insert(it->first);
  131. ++it;
  132. }
  133. it = _notifys.begin();
  134. while (it != _notifys.end())
  135. {
  136. command.insert(it->first);
  137. ++it;
  138. }
  139. set<string>::const_iterator p = command.begin();
  140. while(p != command.end())
  141. {
  142. os<<*p<<"\r\n";
  143. ++p;
  144. }
  145. return os.str();
  146. }
  147. ///////////////////////////////////////////////////////////////////////////
  148. }