X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e767204dbf56ac466489c142f0acdaebd5d0b935..a89444b19b0ceb41db8db6cfa4e6fa77e26d7759:/projects/stargazer/tariffs_impl.cpp?ds=sidebyside diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index e0ef68e1..dbd3d2e3 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -32,21 +32,25 @@ #include #include +#include "stg/locker.h" +#include "stg/logger.h" +#include "stg/store.h" +#include "stg/admin.h" #include "tariffs_impl.h" -#include "stg_locker.h" -#include "stg_logger.h" -#include "store.h" -#include "admin.h" using namespace std; //----------------------------------------------------------------------------- TARIFFS_IMPL::TARIFFS_IMPL(STORE * st) - : tariffs(), + : TARIFFS(), + tariffs(), store(st), WriteServLog(GetStgLogger()), + mutex(), strError(), - noTariff(NO_TARIFF_NAME) + noTariff(NO_TARIFF_NAME), + onAddNotifiers(), + onDelNotifiers() { pthread_mutex_init(&mutex, NULL); ReadTariffs(); @@ -68,9 +72,9 @@ if (store->GetTariffsList(&tariffsList)) WriteServLog("%s", store->GetStrError().c_str()); } -int tariffsNum = tariffsList.size(); +Tariffs::size_type tariffsNum = tariffsList.size(); -for (int i = 0; i < tariffsNum; i++) +for (Tariffs::size_type i = 0; i < tariffsNum; i++) { TARIFF_DATA td; if (store->RestoreTariff(&td, tariffsList[i])) @@ -85,7 +89,7 @@ for (int i = 0; i < tariffsNum; i++) return 0; } //----------------------------------------------------------------------------- -int TARIFFS_IMPL::GetTariffsNum() const +size_t TARIFFS_IMPL::Count() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); return tariffs.size(); @@ -159,27 +163,40 @@ if (!priv->tariffChg) return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); - -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); +TARIFF_DATA td; -if (ti == tariffs.end()) { - strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); - return -1; + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + + list::iterator ti; + ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); + + if (ti == tariffs.end()) + { + strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + + if (store->DelTariff(name)) + { + WriteServLog("Cannot delete tariff %s.", name.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + + td = ti->GetTariffData(); + + tariffs.erase(ti); } -if (store->DelTariff(name)) +std::set *>::iterator ni = onDelNotifiers.begin(); +while (ni != onDelNotifiers.end()) { - WriteServLog("Cannot delete tariff %s.", name.c_str()); - WriteServLog("%s", store->GetStrError().c_str()); - return -1; + (*ni)->Notify(td); + ++ni; } -tariffs.erase(ti); - WriteServLog("%s Tariff \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); @@ -199,19 +216,21 @@ if (!priv->tariffChg) return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); + list::iterator ti; + ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); -if (ti != tariffs.end()) - { - strError = "Tariff \'" + name + "\' cannot be added. Tariff alredy exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); - return -1; - } + if (ti != tariffs.end()) + { + strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } -tariffs.push_back(TARIFF_IMPL(name)); + tariffs.push_back(TARIFF_IMPL(name)); + } if (store->AddTariff(name) < 0) { @@ -220,6 +239,14 @@ if (store->AddTariff(name) < 0) return -1; } +// Fire all "on add" notifiers +std::set *>::iterator ni = onAddNotifiers.begin(); +while (ni != onAddNotifiers.end()) + { + (*ni)->Notify(tariffs.back().GetTariffData()); + ++ni; + } + WriteServLog("%s Tariff \'%s\' added.", admin->GetLogStr().c_str(), name.c_str()); @@ -231,10 +258,34 @@ void TARIFFS_IMPL::GetTariffsData(std::list * tdl) assert(tdl != NULL && "Tariffs data list is not null"); STG_LOCKER lock(&mutex, __FILE__, __LINE__); -std::list::const_iterator it = tariffs.begin(); +Tariffs::const_iterator it = tariffs.begin(); for (; it != tariffs.end(); ++it) { tdl->push_back(it->GetTariffData()); } } //----------------------------------------------------------------------------- +void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE * n) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +onAddNotifiers.insert(n); +} +//----------------------------------------------------------------------------- +void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE * n) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +onAddNotifiers.erase(n); +} +//----------------------------------------------------------------------------- +void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE * n) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +onDelNotifiers.insert(n); +} +//----------------------------------------------------------------------------- +void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE * n) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +onDelNotifiers.erase(n); +} +//-----------------------------------------------------------------------------