]> git.stg.codes - stg.git/blobdiff - projects/stargazer/tariffs_impl.cpp
Move projects back into subfolder.
[stg.git] / projects / stargazer / tariffs_impl.cpp
index 1a8ea359b7b471f4e3f3b1f541e4fe2c9cc328d3..9cc829ddb4114fd07ead7f5d916aa6e30e697b98 100644 (file)
 #include "stg/logger.h"
 #include "stg/store.h"
 #include "stg/admin.h"
+#include "stg/admin_conf.h"
 #include "tariffs_impl.h"
 
-using namespace std;
+using STG::TariffsImpl;
 
 //-----------------------------------------------------------------------------
-TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
-    : TARIFFS(),
-      tariffs(),
-      store(st),
-      WriteServLog(GetStgLogger()),
-      mutex(),
-      strError(),
-      noTariff(NO_TARIFF_NAME),
-      onAddNotifiers(),
-      onDelNotifiers()
+TariffsImpl::TariffsImpl(Store * st)
+    : store(st),
+      WriteServLog(Logger::get()),
+      noTariff(NO_TARIFF_NAME)
 {
-pthread_mutex_init(&mutex, NULL);
 ReadTariffs();
 }
 //-----------------------------------------------------------------------------
-TARIFFS_IMPL::~TARIFFS_IMPL()
+int TariffsImpl::ReadTariffs()
 {
-pthread_mutex_destroy(&mutex);
-}
-//-----------------------------------------------------------------------------
-int TARIFFS_IMPL::ReadTariffs()
-{
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 
-vector<string> tariffsList;
+std::vector<std::string> tariffsList;
 if (store->GetTariffsList(&tariffsList))
     {
     WriteServLog("Cannot get tariffs list.");
     WriteServLog("%s", store->GetStrError().c_str());
     }
 
-Tariffs::size_type tariffsNum = tariffsList.size();
+Data::size_type tariffsNum = tariffsList.size();
 
-for (Tariffs::size_type i = 0; i < tariffsNum; i++)
+for (Data::size_type i = 0; i < tariffsNum; i++)
     {
-    TARIFF_DATA td;
+    TariffData td;
     if (store->RestoreTariff(&td, tariffsList[i]))
         {
         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
         WriteServLog("%s", store->GetStrError().c_str());
         return -1;
         }
-    tariffs.push_back(TARIFF_IMPL(td));
+    tariffs.push_back(TariffImpl(td));
     }
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-size_t TARIFFS_IMPL::Count() const
+size_t TariffsImpl::Count() const
 {
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 return tariffs.size();
 }
 //-----------------------------------------------------------------------------
-const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
+const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
 {
 if (name == NO_TARIFF_NAME)
     return &noTariff;
 
-STG_LOCKER lock(&mutex);
-list<TARIFF_IMPL>::const_iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+std::lock_guard<std::mutex> lock(m_mutex);
+const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
 
 if (ti != tariffs.end())
     return &(*ti);
@@ -110,28 +98,27 @@ if (ti != tariffs.end())
 return NULL;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
+int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
 {
-const PRIV * priv = admin->GetPriv();
+const auto& priv = admin->priv();
 
-if (!priv->tariffChg)
+if (!priv.tariffChg)
     {
-    string s = admin->GetLogStr() + " Change tariff \'"
+    std::string s = admin->logStr() + " Change tariff \'"
                + td.tariffConf.name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
     return -1;
     }
 
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 
-list<TARIFF_IMPL>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
+auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
 
 if (ti == tariffs.end())
     {
     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
-    WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
+    WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
     return -1;
     }
 
@@ -139,42 +126,41 @@ if (ti == tariffs.end())
 
 if (store->SaveTariff(td, td.tariffConf.name))
     {
-    string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
+    std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
     WriteServLog(error.c_str());
     return -1;
     }
 
 WriteServLog("%s Tariff \'%s\' changed.",
-             admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
+             admin->logStr().c_str(), td.tariffConf.name.c_str());
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
+int TariffsImpl::Del(const std::string & name, const Admin * admin)
 {
-const PRIV * priv = admin->GetPriv();
+const auto& priv = admin->priv();
 
-if (!priv->tariffChg)
+if (!priv.tariffChg)
     {
-    string s = admin->GetLogStr() + " Delete tariff \'"
+    std::string s = admin->logStr() + " Delete tariff \'"
                + name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
     return -1;
     }
 
-TARIFF_DATA td;
+TariffData td;
 
     {
-    STG_LOCKER lock(&mutex);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
-    list<TARIFF_IMPL>::iterator ti;
-    ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+    const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(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());
+        WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
         return -1;
         }
 
@@ -184,13 +170,13 @@ TARIFF_DATA td;
         WriteServLog("%s", store->GetStrError().c_str());
         return -1;
         }
-    
+
     td = ti->GetTariffData();
 
     tariffs.erase(ti);
     }
 
-std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onDelNotifiers.begin();
+auto ni = onDelNotifiers.begin();
 while (ni != onDelNotifiers.end())
     {
     (*ni)->Notify(td);
@@ -198,18 +184,18 @@ while (ni != onDelNotifiers.end())
     }
 
 WriteServLog("%s Tariff \'%s\' deleted.",
-             admin->GetLogStr().c_str(),
+             admin->logStr().c_str(),
              name.c_str());
 return 0;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
+int TariffsImpl::Add(const std::string & name, const Admin * admin)
 {
-const PRIV * priv = admin->GetPriv();
+const auto& priv = admin->priv();
 
-if (!priv->tariffChg)
+if (!priv.tariffChg)
     {
-    string s = admin->GetLogStr() + " Add tariff \'"
+    std::string s = admin->logStr() + " Add tariff \'"
                + name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
@@ -217,19 +203,18 @@ if (!priv->tariffChg)
     }
 
     {
-    STG_LOCKER lock(&mutex);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
-    list<TARIFF_IMPL>::iterator ti;
-    ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+    const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
 
     if (ti != tariffs.end())
         {
         strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
-        WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
+        WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
         return -1;
         }
 
-    tariffs.push_back(TARIFF_IMPL(name));
+    tariffs.push_back(TariffImpl(name));
     }
 
 if (store->AddTariff(name) < 0)
@@ -240,7 +225,7 @@ if (store->AddTariff(name) < 0)
     }
 
 // Fire all "on add" notifiers
-std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onAddNotifiers.begin();
+auto ni = onAddNotifiers.begin();
 while (ni != onAddNotifiers.end())
     {
     (*ni)->Notify(tariffs.back().GetTariffData());
@@ -248,44 +233,44 @@ while (ni != onAddNotifiers.end())
     }
 
 WriteServLog("%s Tariff \'%s\' added.",
-                 admin->GetLogStr().c_str(), name.c_str());
+                 admin->logStr().c_str(), name.c_str());
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl) const
+void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
 {
 assert(tdl != NULL && "Tariffs data list is not null");
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 
-Tariffs::const_iterator it = tariffs.begin();
+auto it = tariffs.begin();
 for (; it != tariffs.end(); ++it)
     {
     tdl->push_back(it->GetTariffData());
     }
 }
 //-----------------------------------------------------------------------------
-void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
 {
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 onAddNotifiers.insert(n);
 }
 //-----------------------------------------------------------------------------
-void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
 {
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 onAddNotifiers.erase(n);
 }
 //-----------------------------------------------------------------------------
-void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
 {
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 onDelNotifiers.insert(n);
 }
 //-----------------------------------------------------------------------------
-void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
 {
-STG_LOCKER lock(&mutex);
+std::lock_guard<std::mutex> lock(m_mutex);
 onDelNotifiers.erase(n);
 }
 //-----------------------------------------------------------------------------