From 1147628fd6aa3e78322a596880c1d46f8e6468df Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 1 Sep 2011 13:58:23 +0300 Subject: [PATCH] Add tables update on user add and del --- .../stargazer/plugins/other/smux/smux.cpp | 53 ++++++++++++++++++- projects/stargazer/plugins/other/smux/smux.h | 29 +++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 99c65b8c..23d5f05f 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -102,7 +102,13 @@ SMUX::SMUX() traffcounter(NULL), running(false), stopped(true), - sock(-1) + sock(-1), + smuxHandlers(), + pdusHandlers(), + sensors(), + tables(), + notifiers(), + addDelNotifier(*this) { pthread_mutex_init(&mutex, NULL); @@ -173,9 +179,12 @@ sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporat sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter); // Table data -tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users); +tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users); UpdateTables(); +SetNotifiers(); +users->AddNotifierUserAdd(&addDelNotifier); +users->AddNotifierUserDel(&addDelNotifier); #ifdef DEBUG Sensors::const_iterator it(sensors.begin()); @@ -206,6 +215,10 @@ int SMUX::Stop() printfd(__FILE__, "SMUX::Stop() - Before\n"); running = false; +users->DelNotifierUserDel(&addDelNotifier); +users->DelNotifierUserAdd(&addDelNotifier); +ResetNotifiers(); + if (!stopped) { //5 seconds to thread stops itself @@ -252,6 +265,7 @@ SendOpenPDU(sock); SendRReqPDU(sock); running = true; stopped = false; + while(running) { if (WaitPackets(sock)) @@ -380,3 +394,38 @@ sensors.insert(newSensors.begin(), newSensors.end()); return true; } + +void SMUX::SetNotifiers() +{ +USER_PTR u; +int h = users->OpenSearch(); +assert(h && "USERS::OpenSearch is always correct"); + +while (!users->SearchNext(h, &u)) + { + notifiers.push_back(CHG_AFTER_NOTIFIER(*this, u)); + u->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back()); + } + +users->CloseSearch(h); +} + +void SMUX::ResetNotifiers() +{ +std::list::iterator it = notifiers.begin(); +while (it != notifiers.end()) + { + it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it)); + ++it; + } +} + +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 03d3d3b6..1b1ee96e 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -5,6 +5,7 @@ #include #include +#include #include "stg/SMUX-PDUs.h" #include "stg/ObjectSyntax.h" @@ -52,6 +53,27 @@ private: std::string password; }; //----------------------------------------------------------------------------- +class CHG_AFTER_NOTIFIER : public PROPERTY_NOTIFIER_BASE { +public: + CHG_AFTER_NOTIFIER(SMUX & s, const USER_PTR & u) : smux(s), userPtr(u) {} + void Notify(const std::string &, const std::string &); + + USER_PTR GetUserPtr() { return userPtr; } + +private: + SMUX & smux; + USER_PTR userPtr; +}; +//----------------------------------------------------------------------------- +class ADD_DEL_USER_NOTIFIER : public NOTIFIER_BASE { +public: + ADD_DEL_USER_NOTIFIER(SMUX & s) : smux(s) {} + void Notify(const USER_PTR &); + +private: + SMUX & smux; +}; +//----------------------------------------------------------------------------- class SMUX : public PLUGIN { public: SMUX(); @@ -76,6 +98,8 @@ public: uint16_t GetStartPosition() const { return 100; } uint16_t GetStopPosition() const { return 100; } + bool UpdateTables(); + private: static void * Runner(void * d); void Run(); @@ -92,7 +116,8 @@ private: bool GetNextRequestHandler(const PDUs_t * pdus); bool SetRequestHandler(const PDUs_t * pdus); - bool UpdateTables(); + void SetNotifiers(); + void ResetNotifiers(); USERS * users; TARIFFS * tariffs; @@ -117,6 +142,8 @@ private: Sensors sensors; Tables tables; + std::list notifiers; + ADD_DEL_USER_NOTIFIER addDelNotifier; }; //----------------------------------------------------------------------------- -- 2.43.2