]> git.stg.codes - stg.git/blobdiff - projects/stargazer/tariffs_impl.cpp
Add subscription for add/del tariffs
[stg.git] / projects / stargazer / tariffs_impl.cpp
index a912c3387e8eb14f141b48d8cabb49e1fa7b8039..ea3632cb376cf437b4f2d932cdc903fccb3069a1 100644 (file)
@@ -47,7 +47,9 @@ TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
       store(st),
       WriteServLog(GetStgLogger()),
       strError(),
-      noTariff(NO_TARIFF_NAME)
+      noTariff(NO_TARIFF_NAME),
+      onAddNotifiers(),
+      onDelNotifiers()
 {
 pthread_mutex_init(&mutex, NULL);
 ReadTariffs();
@@ -160,27 +162,40 @@ if (!priv->tariffChg)
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-list<TARIFF_IMPL>::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<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(),
              name.c_str());
@@ -200,19 +215,21 @@ if (!priv->tariffChg)
     return -1;
     }
 
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    {
+    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
-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 already 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)
     {
@@ -221,6 +238,14 @@ 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());
 
@@ -239,3 +264,27 @@ for (; it != tariffs.end(); ++it)
     }
 }
 //-----------------------------------------------------------------------------
+void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onAddNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onAddNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onDelNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onDelNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------