From 58024dfd7ad59626cc0c4beaf82f5c27c93ea0e3 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 1 Sep 2011 16:29:44 +0300 Subject: [PATCH] Fix tariff and user subscription in SMUX --- .../stargazer/plugins/other/smux/smux.cpp | 53 +++++++++++++------ projects/stargazer/plugins/other/smux/smux.h | 50 +++++++++++++++-- 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 23d5f05f..cc69aa81 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -108,7 +108,9 @@ SMUX::SMUX() sensors(), tables(), notifiers(), - addDelNotifier(*this) + addUserNotifier(*this), + delUserNotifier(*this), + addDelTariffNotifier(*this) { pthread_mutex_init(&mutex, NULL); @@ -183,8 +185,6 @@ tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1. UpdateTables(); SetNotifiers(); -users->AddNotifierUserAdd(&addDelNotifier); -users->AddNotifierUserDel(&addDelNotifier); #ifdef DEBUG Sensors::const_iterator it(sensors.begin()); @@ -215,8 +215,6 @@ int SMUX::Stop() printfd(__FILE__, "SMUX::Stop() - Before\n"); running = false; -users->DelNotifierUserDel(&addDelNotifier); -users->DelNotifierUserAdd(&addDelNotifier); ResetNotifiers(); if (!stopped) @@ -395,23 +393,53 @@ sensors.insert(newSensors.begin(), newSensors.end()); return true; } +void SMUX::SetNotifier(USER_PTR userPtr) +{ +notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr)); +userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back()); +} + +void SMUX::UnsetNotifier(USER_PTR userPtr) +{ +std::list::iterator it = notifiers.begin(); +while (it != notifiers.end()) + { + if (it->GetUserPtr() == userPtr) + { + userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it)); + notifiers.erase(it); + break; + } + ++it; + } +} + void SMUX::SetNotifiers() { -USER_PTR u; int h = users->OpenSearch(); assert(h && "USERS::OpenSearch is always correct"); +USER_PTR u; while (!users->SearchNext(h, &u)) - { - notifiers.push_back(CHG_AFTER_NOTIFIER(*this, u)); - u->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back()); - } + SetNotifier(u); users->CloseSearch(h); + +users->AddNotifierUserAdd(&addUserNotifier); +users->AddNotifierUserDel(&delUserNotifier); + +tariffs->AddNotifierAdd(&addDelTariffNotifier); +tariffs->AddNotifierDel(&addDelTariffNotifier); } void SMUX::ResetNotifiers() { +tariffs->DelNotifierDel(&addDelTariffNotifier); +tariffs->DelNotifierAdd(&addDelTariffNotifier); + +users->DelNotifierUserDel(&delUserNotifier); +users->DelNotifierUserAdd(&addUserNotifier); + std::list::iterator it = notifiers.begin(); while (it != notifiers.end()) { @@ -424,8 +452,3 @@ void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &) { smux.UpdateTables(); } - -void ADD_DEL_USER_NOTIFIER::Notify(const USER_PTR &) -{ -smux.UpdateTables(); -} diff --git a/projects/stargazer/plugins/other/smux/smux.h b/projects/stargazer/plugins/other/smux/smux.h index 1b1ee96e..45f16ea6 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -13,6 +13,7 @@ #include "stg/os_int.h" #include "stg/plugin.h" #include "stg/module_settings.h" +#include "stg/notifer.h" #include "sensors.h" #include "tables.h" @@ -65,9 +66,27 @@ private: USER_PTR userPtr; }; //----------------------------------------------------------------------------- -class ADD_DEL_USER_NOTIFIER : public NOTIFIER_BASE { +class ADD_DEL_TARIFF_NOTIFIER : public NOTIFIER_BASE { public: - ADD_DEL_USER_NOTIFIER(SMUX & s) : smux(s) {} + ADD_DEL_TARIFF_NOTIFIER(SMUX & s) : smux(s) {} + void Notify(const TARIFF_DATA &); + +private: + SMUX & smux; +}; +//----------------------------------------------------------------------------- +class ADD_USER_NOTIFIER : public NOTIFIER_BASE { +public: + ADD_USER_NOTIFIER(SMUX & s) : smux(s) {} + void Notify(const USER_PTR &); + +private: + SMUX & smux; +}; +//----------------------------------------------------------------------------- +class DEL_USER_NOTIFIER : public NOTIFIER_BASE { +public: + DEL_USER_NOTIFIER(SMUX & s) : smux(s) {} void Notify(const USER_PTR &); private: @@ -100,6 +119,9 @@ public: bool UpdateTables(); + void SetNotifier(USER_PTR userPtr); + void UnsetNotifier(USER_PTR userPtr); + private: static void * Runner(void * d); void Run(); @@ -143,10 +165,32 @@ private: Tables tables; std::list notifiers; - ADD_DEL_USER_NOTIFIER addDelNotifier; + ADD_USER_NOTIFIER addUserNotifier; + DEL_USER_NOTIFIER delUserNotifier; + ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier; }; //----------------------------------------------------------------------------- +inline +void ADD_DEL_TARIFF_NOTIFIER::Notify(const TARIFF_DATA &) +{ +smux.UpdateTables(); +} + +inline +void ADD_USER_NOTIFIER::Notify(const USER_PTR & userPtr) +{ +smux.SetNotifier(userPtr); +smux.UpdateTables(); +} + +inline +void DEL_USER_NOTIFIER::Notify(const USER_PTR & userPtr) +{ +smux.UnsetNotifier(userPtr); +smux.UpdateTables(); +} + extern "C" PLUGIN * GetPlugin(); #endif -- 2.44.2