123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #include "servant/NotifyObserver.h"
- #include "servant/BaseNotify.h"
- namespace tars
- {
- NotifyObserver::NotifyObserver()
- {
- }
- NotifyObserver::~NotifyObserver()
- {
- }
- void NotifyObserver::registerObject(const string& command, BaseNotify* obj, map<string, set<BaseNotify*> >& target)
- {
- TC_LockT<TC_ThreadRecMutex> lock(*this);
- target[command].insert(obj);
- }
- void NotifyObserver::unregisterObject(const string& command, BaseNotify* obj, map<string, set<BaseNotify*> >& target)
- {
- TC_LockT<TC_ThreadRecMutex> lock(*this);
- map<string, set<BaseNotify*> >::iterator it;
- it = target.find(command);
- if (it != target.end())
- {
- set<BaseNotify*>::iterator sit = it->second.find(obj);
- if (sit != it->second.end())
- {
- it->second.erase(sit);
- }
- if (it->second.empty())
- {
- target.erase(it);
- }
- }
- }
- void NotifyObserver::registerNotify(const string& command, BaseNotify* obj)
- {
- registerObject(command, obj, _notifys);
- }
- void NotifyObserver::unregisterNotify(const string& command, BaseNotify* obj)
- {
- unregisterObject(command, obj, _notifys);
- }
- void NotifyObserver::registerPrefix(const string& command, BaseNotify* obj)
- {
- registerObject(command, obj, _prefix);
- }
- void NotifyObserver::unregisterPrefix(const string& command, BaseNotify* obj)
- {
- unregisterObject(command, obj, _prefix);
- }
- string NotifyObserver::notify(const string& command, CurrentPtr current)
- {
- TC_LockT<TC_ThreadRecMutex> lock(*this);
- string str = TC_Common::trim(command);
- string name = str;
- string params = "";
- string::size_type pos = str.find_first_of(" ");
- if (pos != string::npos)
- {
- name = str.substr(0, pos);
- params = str.substr(pos + 1);
- }
- ostringstream os;
- map<string, set<BaseNotify*> >::iterator it = _prefix.find(name);
- if (it != _prefix.end())
- {
- set<BaseNotify*>& sbn = it->second;
- os << "[notify prefix object num:" << sbn.size() << "]" << endl;
- int i = 0;
- for (set<BaseNotify*>::iterator sit = sbn.begin(); sit != sbn.end(); ++sit)
- {
- string result = "";
- if ((*sit)->notify(name, params, current, result))
- {
- os << "[" << ++i << "]:" << result << endl;
- }
- else
- {
- os << "[notify break by server]:" << endl;
- os << result << endl;
- return os.str();
- }
- }
- }
- it = _notifys.find(name);
- if (it != _notifys.end())
- {
- set<BaseNotify*>& sbn = it->second;
- os << "[notify servant object num:" << sbn.size() << "]" << endl;
- int i = 0;
- for (set<BaseNotify*>::iterator sit = sbn.begin(); sit != sbn.end(); ++sit)
- {
- string result = "";
- if ((*sit)->notify(name, params, current, result))
- {
- os << "[" << ++i << "]:" << result << endl;
- }
- else
- {
- os << "[notify break.]" << endl;
- os << result << endl;
- return os.str();
- }
- }
- }
- return os.str();
- }
- string NotifyObserver::viewRegisterCommand()
- {
- TC_LockT<TC_ThreadRecMutex> lock(*this);
- ostringstream os;
- map<string, set<BaseNotify*> >::iterator it = _prefix.begin();
- set<string> command;
- while (it != _prefix.end())
- {
- command.insert(it->first);
- ++it;
- }
- it = _notifys.begin();
- while (it != _notifys.end())
- {
- command.insert(it->first);
- ++it;
- }
- set<string>::const_iterator p = command.begin();
- while(p != command.end())
- {
- os<<*p<<"\r\n";
- ++p;
- }
- return os.str();
- }
- ///////////////////////////////////////////////////////////////////////////
- }
|