]> git.stg.codes - stg.git/blobdiff - projects/stargazer/tariffs_impl.cpp
Simplified STG_LOCKER.
[stg.git] / projects / stargazer / tariffs_impl.cpp
index 32de49716d960ac152f3d4c63a2652a1b0dfe977..aee486d76fdb03b5e20a2147926d51f5f0cb923a 100644 (file)
 #include <algorithm>
 #include <vector>
 
+#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();
@@ -59,7 +63,7 @@ pthread_mutex_destroy(&mutex);
 //-----------------------------------------------------------------------------
 int TARIFFS_IMPL::ReadTariffs()
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 vector<string> tariffsList;
 if (store->GetTariffsList(&tariffsList))
@@ -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,9 +89,9 @@ 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__);
+STG_LOCKER lock(&mutex);
 return tariffs.size();
 }
 //-----------------------------------------------------------------------------
@@ -96,7 +100,7 @@ const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
 if (name == NO_TARIFF_NAME)
     return &noTariff;
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 list<TARIFF_IMPL>::const_iterator ti;
 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
 
@@ -106,20 +110,20 @@ if (ti != tariffs.end())
 return NULL;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN & admin)
+int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
 {
-const PRIV * priv = admin.GetPriv();
+const PRIV * priv = admin->GetPriv();
 
 if (!priv->tariffChg)
     {
-    string s = admin.GetLogStr() + " Change tariff \'"
+    string s = admin->GetLogStr() + " Change tariff \'"
                + td.tariffConf.name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 list<TARIFF_IMPL>::iterator ti;
 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
@@ -127,7 +131,7 @@ ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(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->GetLogStr().c_str(), strError.c_str());
     return -1;
     }
 
@@ -141,77 +145,92 @@ if (store->SaveTariff(td, td.tariffConf.name))
     }
 
 WriteServLog("%s Tariff \'%s\' changed.",
-             admin.GetLogStr().c_str(), td.tariffConf.name.c_str());
+             admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Del(const string & name, const ADMIN & admin)
+int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
 {
-const PRIV * priv = admin.GetPriv();
+const PRIV * priv = admin->GetPriv();
 
 if (!priv->tariffChg)
     {
-    string s = admin.GetLogStr() + " Delete tariff \'"
+    string s = admin->GetLogStr() + " Delete tariff \'"
                + name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+TARIFF_DATA td;
 
-list<TARIFF_IMPL>::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;
+    STG_LOCKER lock(&mutex);
+
+    list<TARIFF_IMPL>::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<NOTIFIER_BASE<TARIFF_DATA> *>::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(),
+             admin->GetLogStr().c_str(),
              name.c_str());
 return 0;
 }
 //-----------------------------------------------------------------------------
-int TARIFFS_IMPL::Add(const string & name, const ADMIN & admin)
+int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
 {
-const PRIV * priv = admin.GetPriv();
+const PRIV * priv = admin->GetPriv();
 
 if (!priv->tariffChg)
     {
-    string s = admin.GetLogStr() + " Add tariff \'"
+    string s = admin->GetLogStr() + " Add tariff \'"
                + name + "\'. Access denied.";
     strError = "Access denied.";
     WriteServLog(s.c_str());
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    {
+    STG_LOCKER lock(&mutex);
 
-list<TARIFF_IMPL>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+    list<TARIFF_IMPL>::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,8 +239,16 @@ if (store->AddTariff(name) < 0)
     return -1;
     }
 
+// Fire all "on add" notifiers
+std::set<NOTIFIER_BASE<TARIFF_DATA> *>::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());
+                 admin->GetLogStr().c_str(), name.c_str());
 
 return 0;
 }
@@ -229,12 +256,36 @@ return 0;
 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
 {
 assert(tdl != NULL && "Tariffs data list is not null");
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
-std::list<TARIFF_IMPL>::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<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex);
+onAddNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex);
+onAddNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex);
+onDelNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex);
+onDelNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------